Click to See Complete Forum and Search --> : C++ question
nothing
01-10-2004, 12:08 AM
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.
fishybawb
01-10-2004, 07:26 AM
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:
#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 :D
:t
nothing
01-10-2004, 10:04 AM
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!
fishybawb
01-10-2004, 03:17 PM
You're welcome :)
nothing
01-11-2004, 04:29 PM
Hey fishy, what compiler do you use?
fishybawb
01-11-2004, 04:33 PM
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?
nothing
01-11-2004, 04:46 PM
Ok. Could you please compile and run this program using your compile and tell me what happens?
#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;
}
fishybawb
01-11-2004, 04:58 PM
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...
nothing
01-11-2004, 05:00 PM
I am really sorry to say but you're wrong, my dear friend.
http://www.cs.drexel.edu/~mcs171/Sp03/extras/getline_Fixer/instructions.html
Read that ;)
fishybawb
01-11-2004, 05:21 PM
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! :D
nothing
01-11-2004, 05:27 PM
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 :D
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.