Only accept an alphabetical character

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: Only accept an alphabetical character

 
0
  #11
Apr 15th, 2008
Originally Posted by jade09091990 View Post
the code actually has a bug, for example you've entered something like this: c2,
the program will behave unexpectedly, how to remedy this?
Good pick up. It appears that the variable ch needs to be checked prior to the switch statement. I've modified my original program that I posted at the top of this thread. What needed to be done is simply make sure that the next character was a newline, if it was then only a single character was input. If the next character wasn't a newline then something like c2 was entered.

Here's the code:

  1. // chap06q03.cpp
  2. // C++ Primer Plus, Fifth Edition
  3. // Chapter 6, Page 276.
  4. // Programming Exercise # 3
  5. // 4 Dec, 2007.
  6. // 15 Apr, 2008 - modified - disregarding c2 etc.
  7.  
  8. #include <iostream>
  9.  
  10. int main()
  11. {
  12. using namespace std;
  13. char ch;
  14. char msg[] = "\nPlease enter c, p, t or g: ";
  15. bool invalid = true;
  16.  
  17. cout << "Please enter on of the following choices:\n\n";
  18. cout << "c) carnivore p) pianist\n";
  19. cout << "t) tree g) game\n";
  20.  
  21. while (invalid)
  22. {
  23. cin >> ch;
  24. if ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g'))
  25. {
  26. while (cin.get() != '\n')
  27. continue;
  28. cout << msg;
  29. }
  30. else
  31. if (cin.get() == '\n')
  32. invalid = false;
  33. else
  34. {
  35. while (cin.get() != '\n')
  36. continue;
  37. cout << msg;
  38. }
  39. }
  40. switch (ch)
  41. {
  42. case 'c' : cout << "Option 'c' selected"; break;
  43. case 'p': cout << "Option 'p' selectd";break;
  44. case 't' : cout << "Option 't' selected";break;
  45. case 'g': cout << "Option 'g' selected"; break;
  46. default : cout << "It will never get her"; break;
  47. }
  48. // exit routine
  49. cout << "\n\n...Press ENTER to Exit System...";
  50. cin.get();
  51. return 0;
  52. }
Given some time I'm sure the above program can be smoothed out, but at least it demonstrates only accepting a single character (not using strings and other methods). As it stands now, clearly there are a couple of snippets which are candidates to be functions.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 3
Reputation: jade09091990 is an unknown quantity at this point 
Solved Threads: 0
jade09091990 jade09091990 is offline Offline
Newbie Poster

Re: Only accept an alphabetical character

 
0
  #12
Apr 15th, 2008
[code]well, thanks. it is a great help to me. I really find it hard before. thanks[code/]
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC