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

Only accept an alphabetical character

 
0
  #1
Dec 4th, 2007
I've just completed an exercise from a book wherein the task is to display a menu of choices each associated to an alphabetical character. The program is to reject invalid characters and only finishing when a valid choice is made. Below is my solution, it achieves the task but I'd like to know if there is a completely easier approach.
  1. // chap05q03.cpp
  2. // C++ Primer Plus, Fifth Edition
  3. // Chapter 6, Page 276.
  4. // Programming Exercise # 3
  5. // 4 Dec, 2007.
  6.  
  7. #include <iostream>
  8.  
  9. int main()
  10. {
  11. using namespace std;
  12. char ch;
  13. char msg[] = "\nPlease enter c, p, t or g: ";
  14. bool invalid = true;
  15.  
  16. cout << "Please enter on of the following choices:\n\n";
  17. cout << "c) carnivore p) pianist\n";
  18. cout << "t) tree g) game\n";
  19.  
  20. while (invalid)
  21. {
  22. cin >> ch;
  23. if ((ch != 'c') && (ch != 'p') && (ch != 't') && (ch != 'g'))
  24. {
  25. cin.clear();
  26. while (cin.get() != '\n')
  27. continue;
  28. cout << msg;
  29. }
  30. else
  31. invalid = false; // we've got a correct entry, exit loop.
  32. }
  33. switch (ch)
  34. {
  35. case 'c' : cout << "Option 'c' selected"; break;
  36. case 'p' : cout << "Option 'p' selectd"; break;
  37. case 't' : cout << "Option 't' selected"; break;
  38. case 'g' : cout << "Option 'g' selected"; break;
  39. default : cout << "It will never get her"; break;
  40. }
  41. system("PAUSE");
  42. return 0;
  43. }

Any advice welcome.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Only accept an alphabetical character

 
0
  #2
Dec 4th, 2007
You could put the switch inside the loop (better as a do...while), let the default case handle the erroneous inputs. What is your if( ) condition becomes the while( ) condition.

Val
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
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
  #3
Dec 4th, 2007
Originally Posted by vmanes View Post
You could put the switch inside the loop (better as a do...while), let the default case handle the erroneous inputs. What is your if( ) condition becomes the while( ) condition.

Val
Thanks for the advice, very much appreciated. I've altered the code as suggested and is a lot 'cleaner' looking. Below is the result.
  1. // chap05q03-a.cpp
  2. // C++ Primer Plus, Fifth Edition
  3. // Chapter 6, Page 276.
  4. // Programming Exercise # 3
  5. // 4 Dec, 2007.
  6.  
  7. // modified after advice from DaniWeb C++ forum.
  8.  
  9. #include <iostream>
  10.  
  11. int main()
  12. {
  13. using namespace std;
  14. char ch;
  15. char msg[] = "\nPlease enter c, p, t or g: ";
  16. bool invalid = false;
  17.  
  18. cout << "Please enter on of the following choices:\n\n";
  19. cout << "c) carnivore p) pianist\n";
  20. cout << "t) tree g) game\n";
  21.  
  22. do
  23. {
  24. cin >> ch;
  25. invalid = false;
  26. switch (ch)
  27. {
  28. case 'c': cout << "Option 'c' selected\n"; break;
  29. case 'p': cout << "Option 'p' selectd\n"; break;
  30. case 't': cout << "Option 't' selected\n"; break;
  31. case 'g': cout << "Option 'g' selected\n"; break;
  32. default : cout << msg;
  33. cin.clear();
  34. while (cin.get() != '\n')
  35. continue;
  36. invalid = true;
  37. }
  38. } while (invalid);
  39. system("PAUSE");
  40. return 0;
  41. }
Last edited by superjacent; Dec 4th, 2007 at 3:41 am. Reason: Formatting was all out of whack.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Only accept an alphabetical character

 
0
  #4
Dec 4th, 2007
You can also let 'c','p' & 't' fall through to 'g'

Originally Posted by superjacent View Post
  1. switch (ch)
  2. {
  3. case 'c':
  4. case 'p':
  5. case 't':
  6. case 'g': cout << "Option '" << ch << "' selected\n";
  7. break;
  8.  
  9. default : cout << msg;
  10. cin.clear();
  11. while (cin.get() != '\n')
  12. continue;
  13. invalid = true;
  14. }
Reply With Quote Quick reply to this message  
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
  #5
Dec 4th, 2007
Originally Posted by Tight_Coder_Ex View Post
You can also let 'c','p' & 't' fall through to 'g'
Thanks, very much appreciated.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Only accept an alphabetical character

 
0
  #6
Dec 4th, 2007
Originally Posted by superjacent View Post
Thanks for the advice, very much appreciated. I've altered the code as suggested and is a lot 'cleaner' looking.
Not bad, although it's still not formatted properly.
By the way, what does cin.clear(); do? You might want to look it up.
Another thing to look at is system("PAUSE"); See this
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
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
  #7
Dec 4th, 2007
Originally Posted by WaltP View Post
Not bad, although it's still not formatted properly.
By the way, what does cin.clear(); do? You might want to look it up.
Another thing to look at is system("PAUSE"); See this
My understanding re cin.clear() is that if the cin fails (eof or mismatch of data) then further access or use of cin is prevented; to avoid this use the clear() method.

I guess you're probably suggesting that in this particular case it's not needed. Looking at the code I can't see how a mis-match of data cin >> ch could occur and I don't think EOF is an issue either. I hope I haven't embarrassed myself with this answer or response.

Taking this a step further in relation to cin.clear(). If the receiving variable is of a numeric type is that when cin.clear() would be considered in fixing up erroneous data entries.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Only accept an alphabetical character

 
0
  #8
Dec 4th, 2007
Originally Posted by superjacent View Post
My understanding re cin.clear() is that if the cin fails (eof or mismatch of data) then further access or use of cin is prevented; to avoid this use the clear() method.
True. So what error are you expecting that needs to be cleared at this point?

Originally Posted by superjacent View Post
I guess you're probably suggesting that in this particular case it's not needed. Looking at the code I can't see how a mis-match of data cin >> ch could occur and I don't think EOF is an issue either. I hope I haven't embarrassed myself with this answer or response.
That's what I'm suggesting, yes. And you have definitely NOT embarrassed yourself. It's a correct analysis.

Originally Posted by superjacent View Post
Taking this a step further in relation to cin.clear(). If the receiving variable is of a numeric type is that when cin.clear() would be considered in fixing up erroneous data entries.
Actually, no. All cin.clear() does is clear the error flags. If they were set, it does not fix any data entries. The way you're using it, the error is simply cleared and ignored. You never test for one, so you don't even know it happened.

What you suggested above is "If the receiving variable is of a numeric type" and you type in a letter instead, cin.clear() magically changes the letter to a digit. What in fact happens is the error flags are set, nothing is loaded into the variable (previous value remains unchanged), and the letter stays in the input stream for the next cin .
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
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
  #9
Dec 4th, 2007
Originally Posted by WaltP View Post
Actually, no. All cin.clear() does is clear the error flags. If they were set, it does not fix any data entries. The way you're using it, the error is simply cleared and ignored. You never test for one, so you don't even know it happened.

What you suggested above is "If the receiving variable is of a numeric type" and you type in a letter instead, cin.clear() magically changes the letter to a digit. What in fact happens is the error flags are set, nothing is loaded into the variable (previous value remains unchanged), and the letter stays in the input stream for the next cin .
The above makes sense and is 100% consistent with what I've read though I know I wasn't too clear with my explanation.

Thanks for the advice, and everyone else as well, it's really helped.
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
  #10
Apr 13th, 2008
the code actually has a bug, for example you've entered something like this: c2,
the program will behave unexpectedly, how to remedy this?
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC