Guest User

Untitled

a guest
Sep 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::string;
  9.  
  10. string isItANumber(string input="")
  11. {
  12.  
  13. int count = 0;
  14. bool runAgain = true;
  15.  
  16. while(runAgain)
  17. {
  18. if(count == 0)
  19. {
  20. for (int i = 0; i < input.length(); ++i) {
  21. if(!(isdigit(input[i])))
  22. {
  23. // Let runAgain stay true
  24. cout << "One of the characters is not a digit" << endl;
  25. break;
  26. }
  27. else
  28. {
  29. runAgain = false;
  30. }
  31. }
  32. count++;
  33. }
  34. else
  35. {
  36. cout << "Error! Please enter a number: " << endl;
  37.  
  38. // Put the input in input
  39. getline(cin, input);
  40.  
  41. // Check again
  42. for (int i = 0; i < input.length(); ++i) {
  43. if(!(isdigit(input[i])))
  44. {
  45. // Let runAgain stay true
  46. break;
  47. }
  48. else
  49. {
  50. runAgain = false;
  51. }
  52. }
  53.  
  54. }
  55.  
  56.  
  57. }
  58.  
  59. return input;
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66. int main() {
  67.  
  68. string input;
  69.  
  70. cout << "Please enter a number: " << endl;
  71. getline(cin, input);
  72.  
  73. isItANumber(input);
  74.  
  75.  
  76. return 0;
  77. }
Add Comment
Please, Sign In to add comment