944,193 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4274
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 4th, 2007
0

Only accept an alphabetical character

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Dec 4th, 2007
0

Re: Only accept an alphabetical character

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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Dec 4th, 2007
0

Re: Only accept an alphabetical character

Click to Expand / Collapse  Quote originally posted by vmanes ...
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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Dec 4th, 2007
0

Re: Only accept an alphabetical character

You can also let 'c','p' & 't' fall through to 'g'

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Dec 4th, 2007
0

Re: Only accept an alphabetical character

You can also let 'c','p' & 't' fall through to 'g'
Thanks, very much appreciated.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Dec 4th, 2007
0

Re: Only accept an alphabetical character

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
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Dec 4th, 2007
0

Re: Only accept an alphabetical character

Click to Expand / Collapse  Quote originally posted by WaltP ...
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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Dec 4th, 2007
0

Re: Only accept an alphabetical character

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?

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.

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 .
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,753 posts
since May 2006
Dec 4th, 2007
0

Re: Only accept an alphabetical character

Click to Expand / Collapse  Quote originally posted by WaltP ...
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.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Apr 13th, 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?
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