943,983 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3017
  • C++ RSS
Dec 19th, 2004
0

Keyboard Error Handling

Expand Post »
I am doing a program for school that lets the user guess the number. I am to be tested for all situations which include the user pressing a "letter" on the keyboard. I believe that I have all of the other errors handled, except for if the user enters a letter, and then the program will not execute anymore. Any help is greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xelitex is offline Offline
2 posts
since Dec 2004
Dec 20th, 2004
0

Re: Keyboard Error Handling

There is a standard function IsAlpha() that i have seen before in a previous post i will look it up for you
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 20th, 2004
0

Re: Keyboard Error Handling

i believe its in <cctype> and it returns 0 if it is numerical

alternatively there is the isdigit() function which can do all the error testing at once (it returns 0 if the character is a digit)

C++ Syntax (Toggle Plain Text)
  1. #include <cctype>
  2. using namespace std;
  3.  
  4. char input;
  5.  
  6. cout << "Type in a number";
  7. cin >> input;
  8.  
  9. if(!isdigit(input))
  10. {
  11. // process the number
  12. }
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 20th, 2004
0

Re: Keyboard Error Handling

example testing code from a site i found

http://www.cse.msu.edu/~cse231/Examp.../Example04.htm
C++ Syntax (Toggle Plain Text)
  1. /**********************************************************************
  2.  
  3.  Example #4 -- Boolean expressions
  4.  
  5. **********************************************************************/
  6.  
  7.  
  8.  
  9. #include <iostream>
  10.  
  11. #include <cctype>
  12.  
  13. using namespace std;
  14.  
  15.  
  16.  
  17. int main()
  18.  
  19. {
  20.  
  21. const bool A = true, B = false;
  22.  
  23.  
  24.  
  25. const int C = 3, D = 8;
  26.  
  27.  
  28.  
  29. const char E = 'z', F = '5';
  30.  
  31.  
  32.  
  33. cout << endl;
  34.  
  35. cout << "bool A: " << A << endl;
  36.  
  37. cout << "bool B: " << B << endl << endl;
  38.  
  39.  
  40.  
  41. cout << boolalpha;
  42.  
  43. cout << "bool A: " << A << endl;
  44.  
  45. cout << "bool B: " << B << endl << endl;
  46.  
  47.  
  48.  
  49. cout << "int C: " << C << endl;
  50.  
  51. cout << "int D: " << D << endl << endl;
  52.  
  53.  
  54.  
  55. cout << "C == D: " << (C == D) << endl;
  56.  
  57. cout << "C != D: " << (C != D) << endl;
  58.  
  59. cout << "C < 3: " << (C < 3) << endl;
  60.  
  61. cout << "C <= 3: " << (C <= 3) << endl;
  62.  
  63. cout << "C > 3: " << (C > 3) << endl;
  64.  
  65. cout << "C >= 3: " << (C >= 3) << endl << endl;
  66.  
  67.  
  68.  
  69. cout << "C >= 5 || D <= 9: " << (C >= 5 || D <= 9) << endl;
  70.  
  71. cout << "C >= 0 && D >= 4: " << (C >= 0 && D >= 4) << endl << endl;
  72.  
  73.  
  74.  
  75. cout << "C != D || C < -6: " << (C != D || C < -6) << endl;
  76.  
  77. cout << "C <= 0 && D > -6: " << (C <= 0 && D > -6) << endl << endl;
  78.  
  79.  
  80.  
  81. cout << "1 <= C && C <= 5: " << (1 <= C && C <= 5) << endl;
  82.  
  83. cout << "1 <= D && D <= 5: " << (1 <= D && D <= 5) << endl << endl;
  84.  
  85.  
  86.  
  87. cout << "char E: " << E << endl;
  88.  
  89. cout << "char F: " << F << endl << endl;
  90.  
  91.  
  92.  
  93. cout << "isalnum( E ): " << isalnum( E ) << endl;
  94.  
  95. cout << "isalpha( E ): " << isalpha( E ) << endl;
  96.  
  97. cout << "islower( E ): " << islower( E ) << endl;
  98.  
  99. cout << "isdigit( E ): " << isdigit( E ) << endl;
  100.  
  101. cout << "toupper( E ): " << static_cast<char>( toupper( E ) )
  102.  
  103. << endl << endl;
  104.  
  105.  
  106.  
  107. cout << "isalnum( F ): " << isalnum( F ) << endl;
  108.  
  109. cout << "isalpha( F ): " << isalpha( F ) << endl;
  110.  
  111. cout << "islower( F ): " << islower( F ) << endl;
  112.  
  113. cout << "isdigit( F ): " << isdigit( F ) << endl << endl;
  114.  
  115.  
  116.  
  117. return 0;
  118.  
  119. }
Last edited by 1o0oBhP; Dec 20th, 2004 at 8:50 pm. Reason: Put in code tags
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 21st, 2004
0

Re: Keyboard Error Handling

Thank you for all of your help everybody, it is greatly appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xelitex is offline Offline
2 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC