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