fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main(){
  7. string str; //Store a number
  8. int i=0; //Spit out the number
  9.  
  10. cout << "Enter a number: ";
  11.  
  12. while(getline(cin, str)){
  13. stringstream ss(str);
  14. if (ss >> i && ss.eof())
  15. break;
  16. else
  17. cout <<"\nPlease enter a valid number: ";
  18. }
  19.  
  20. cout << "\nThe value stored in string str was: " << i;
  21. cin.get();
  22. return 0;
  23. }
Success #stdin #stdout 0.02s 2688KB
stdin
Standard input is empty
stdout
Enter a number: 
The value stored in string str was: 0