|
From: Alfred P. R. <al...@ya...> - 2005-04-23 02:11:08
|
Hi Ryan,
If I understand correctly, you only want to output the error prompt once? It looks like it's written to prompt you every time a wrong character is pressed. One suggestion would be to place a boolean flag in it. Another would be to put the whole section from Menu: to the end of the if statement in a while or do loop until it exits with the proper selection.
Not knowing what menuSelection is cast as, one has to assume char, as that is the default istream element. So at the else you set a flag, test for it (why? it will always be true!), prompt, and then get a character. Then you loop with the goto MENU. Goto's that resolve to a location outside of a loop are bad form, IMHO.
Try this:
bool onepass = false;
system("CLS");
cout << "Student Pack \t\t\t\t\t\t\t\t v1.0" << endl << endl;
cout << "Please Make A Selection " << endl;
cout << "------------------------" << endl;
cout << "(M)oney Counter" << endl << endl;
cout << "(T)emperature Converter" << endl << endl;
cout << "(C)alculator" << endl << endl;
cout << "C(r)edits" << endl << endl;
cout << "(B)ug Reporting" << endl << endl;
cout << "(E)xit" << endl << endl;
do {
cin >> menuSelection;
// It may be better to use getche if menuSelection is an int.
// menuSelection = getche();
if ( (menuSelection == 'm') || (menuSelection == 't') || (menuSelection == 'c') || (menuSelection == 'r') || (menuSelection == 'b') || (menuSelection == 'e') ) {
menuSelection = (menuSelection - 32);
break;
}
else {
// To only do one pass here add the boolean flag as follows
if (!onepass) {
cout << "Please make a proper slection. " << endl;
onepass = true;
}
}
} while ( true );
Hope that helped!
Ryan Turney <sk8...@gm...> wrote:
Hello,
I am writing a small program that has many different options in it and
when you start it, it will show you a menu. But the problem is that
you can only push in a few letters to use the menu and if you put a
wrong one it will say please select a better option. But if you type
in more than one wrong character it will run the error catching lines
again. How do I get this to run once even if there are 6 wrong
characters inputted? The error catching code I have now look like:
int main(int nNumberofArgs, char* pszArgs[])
{
Error = 0;
MENU:
cout << "Student Pack \t\t\t\t\t\t\t\t v1.0" << endl << endl;
cout << "Please Make A Selection " << endl;
cout << "------------------------" << endl;
cout << "(M)oney Counter" << endl << endl;
cout << "(T)emperature Converter" << endl << endl;
cout << "(C)alculator" << endl << endl;
cout << "C(r)edits" << endl << endl;
cout << "(B)ug Reporting" << endl << endl;
cout << "(E)xit" << endl << endl;
cin >> menuSelection;
if ((menuSelection == 'm') || (menuSelection == 't') || (menuSelection
== 'c') || (menuSelection == 'r') || (menuSelection == 'b') ||
(menuSelection == 'e'))
{
menuSelection = (menuSelection - 32);
}
else
{
Error = Error + 1;
system("CLS");
while (Error == 1)
{
Error = Error - 1;
cout << "Please make a proper slection. " << endl;
getch();
system("CLS");
goto MENU;
}
}
Notice: This is not even close to all the coding but this is where the
problem is taking place.
Please help me if you can.
Thank you.
--
-Ryan
Alfred P. Reaud
|