-
Ultimate Member
C++ question
Let's say I have a menu like this
1.Start
2.Help
3.Exit
and the user, instead of typing a number and pressing enter, he/she just presses enter. How do I make my program display an error message when that happens? Thanks.
-
Hired Geek
Using cin won't work for this, as you've probably found out - it waits for a value, and ignores "empty" (linefeed) input. Here's one way of doing it:
Code:
#include <iostream>
using namespace std;
int main()
{
// cin.get needs a char variable - they can store ints too!
char choice = 0;
// while the input is "bad" ie. less than 1 OR greater than 3
while(choice < 1 || choice > 3)
{
// prompt the user for a number
cout << endl << "Enter a choice (1-3): ";
// get the number they typed
cin.get(choice);
// clear the stream queue to ignore "enter" presses
cin.sync();
// convert the keycode to actual number (1 = 49 in ascii)
choice = choice - 48;
}
switch(choice)
{
case 1:
// user chose 1
break;
// etc...
}
return 0;
}
cin.get() grabs the input as an ASCII code, so to convert it to a number, we have to subtract 48 from it - 1 is 49 in ASCII, 2 is 50 and so on. It's a bit messy and there's probably a better way of doing it, but I can't think of one 
-
Ultimate Member
Well, that is very nice fishybawb. You have no idea how long I've been trying to do something like that. Thank you very much!
-
Hired Geek
You're welcome
-
Ultimate Member
Hey fishy, what compiler do you use?
-
Hired Geek
I used to use Visual Studio .NET 2003, but it's pretty slow and clunky, so I've gone back to 6.0 - I lost interest in .NET fairly quickly anyway. How about you?
-
Ultimate Member
Ok. Could you please compile and run this program using your compile and tell me what happens?
Code:
#include <iostream>
using std::cout;
using std::cin;
#include <string>
using std::string;
int main()
{
string choice;
do {
cout << "Enter a choice: ";
std::getline (cin, choice);
} while(choice[0] < '1' || choice[0] > '3');
return 0;
}
-
Hired Geek
It behaves badly. Entering valid numbers (ie. 1-3) works, but you have to press enter twice. Enter an INvalid number and you still have to press enter twice, but repeated bad attempts only need one enter press to get the next prompt. Entering a valid number at this point still needs two enters, but after the first you get and "Enter a choice:" prompt, even though pressing enter alone is enough to end the program. I hope that makes sense I'm fairly sure that's the result you'd get on any compiler...
-
Ultimate Member
I am really sorry to say but you're wrong, my dear friend.
http://www.cs.drexel.edu/~mcs171/Sp0...tructions.html
Read that
-
Hired Geek
Learn something new every day I'm not sure I've ever used getline, but if I do it's now fixed thanks to you. Looking at the code I thought it was a combination of the do...while and getline leaving newline codes in the keyboard buffer... D'oh!
-
Ultimate Member
I was looking for information on how to use the getline function and that page was among the results. This must be my lucky day
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke
|
Bookmarks