fork download
  1. int main(){
  2. string str; //Store a number
  3. int i=0; //Spit out the number
  4.  
  5. cout << "Enter a number: ";
  6. getline(cin, str);
  7.  
  8. while(true){
  9. stringstream ss(str);
  10. for(int i=0; i<str.size(); i++){
  11. if(str[i]==' '){
  12. cout <<"\nPlease enter a valid number: ";
  13. getline(cin,str);
  14. i=0;
  15. }
  16. }
  17. if (ss >> i){
  18. break;
  19. }
  20. else{
  21. cout <<"\nPlease enter a valid number: ";
  22. getline(cin,str);
  23. }
  24. }
  25.  
  26. cout << "\nThe value stored in string str was: " << i;
  27. cin.get();
  28. return 0;
  29. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:2: error: ‘string’ was not declared in this scope
prog.cpp:2: error: expected `;' before ‘str’
prog.cpp:5: error: ‘cout’ was not declared in this scope
prog.cpp:6: error: ‘cin’ was not declared in this scope
prog.cpp:6: error: ‘str’ was not declared in this scope
prog.cpp:6: error: ‘getline’ was not declared in this scope
prog.cpp:9: error: ‘stringstream’ was not declared in this scope
prog.cpp:9: error: expected `;' before ‘ss’
prog.cpp:17: error: ‘ss’ was not declared in this scope
prog.cpp:17: warning: name lookup of ‘i’ changed
prog.cpp:3: warning:   matches this ‘i’ under ISO standard rules
prog.cpp:10: warning:   matches this ‘i’ under old rules
stdout
Standard output is empty