fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. int main(){
  8.  
  9. string str; //Store a number
  10. int i=0; //Spit out the number
  11.  
  12. cout << "Enter a number: ";
  13. getline(cin, str);
  14.  
  15. while(true){
  16. stringstream ss(str);
  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.  
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 2864KB
stdin
egseigieguieguin
giuwregiuerbgseng
ragieurbgeigng
123
stdout
Enter a number: 
Please enter a valid number: 
Please enter a valid number: 
Please enter a valid number: 
The value stored in string str was: 123