943,747 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4273
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Apr 15th, 2008
0

Re: Only accept an alphabetical character

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:

cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Apr 15th, 2008
0

Re: Only accept an alphabetical character

[code]well, thanks. it is a great help to me. I really find it hard before. thanks[code/]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jade09091990 is offline Offline
3 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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:
Previous Thread in C++ Forum Timeline: Need Help with Address Book Assignment
Next Thread in C++ Forum Timeline: MCI (Multimedia Control Interface) in C++ HELP PLEASE!





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


Follow us on Twitter


© 2011 DaniWeb® LLC