fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void doIt(string datainput)
  6. {
  7. cout << "Input: " << datainput << endl;
  8.  
  9. string str3;
  10.  
  11. int myray[10];
  12. int raycount = 0;
  13.  
  14. string::size_type start = datainput.find_first_of("0123456789");
  15. string::size_type end;
  16.  
  17. while (start != string::npos)
  18. {
  19. end = datainput.find_first_not_of("0123456789", start+1);
  20. if (end == string::npos)
  21. {
  22. str3 = datainput.substr(start);
  23. myray[raycount] = stoi(str3);
  24. raycount++;
  25. break;
  26. }
  27.  
  28. str3 = datainput.substr(start, end-start);
  29. myray[raycount] = stoi(str3);
  30. raycount++;
  31. if (raycount == 10) break;
  32.  
  33. start = datainput.find_first_of("0123456789", end+1);
  34. }
  35.  
  36. for (int n = 0; n < raycount; ++n)
  37. cout << "myray[" << n << "] = " << myray[n] << endl;
  38. }
  39.  
  40. int main()
  41. {
  42. doIt("12, 13, 15");
  43. cout << endl;
  44.  
  45. doIt("12 13 15");
  46. cout << endl;
  47.  
  48. doIt("12ab13cd15ef");
  49. cout << endl;
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Input: 12, 13, 15
myray[0] = 12
myray[1] = 13
myray[2] = 15

Input: 12 13 15
myray[0] = 12
myray[1] = 13
myray[2] = 15

Input: 12ab13cd15ef
myray[0] = 12
myray[1] = 13
myray[2] = 15