-
Switch function problem
Hi all
I've only just started using the C++ language recently and I'm stuck on something which should be pretty easy but I can't solve.
option is defined as char option[80]. My main problem is that if I enter an option of 9, "PLEASE TRY AGAIN" comes up. I want this to happen if say 22 is entered as well but currently Case 2 is selected, not the default. Why is it doing this and how would I sort it?
Code:
do {
cout << "\n Products\n";
cout << " 1. New\n";
cout << " 2. Old\n";
cout << " 3. Discontinued\n";
cout << " q. End menu\n";
cout << "\n\n";
cout << "\n Press appropriate key to select > ";
gets(option);
switch (option[0]) {
case '1':
new(items);
key();
break;
case '2':
old(items);
key();
break;
case '3':
discontinued(items);
key();
break;
case 'q':
cout <<("\n\t\t Returning to main.\n");
key();
break;
default:
cout <<("\t\t PLEASE TRY AGAIN\n");
key();
break;
} //end switch
}//end do
while (option[0] != 'q' );
break;
-
Very simply it's because the switch statement is only looking
at the first element of your string. The second digit in "22" is
strored at the element option [1]. It would be better to use an
integer for this task, and change your 'q' case to 0.
Also, see my sig - use cin.getline to read strings in C++. Also,
new is a reserved word in C++, so your function called new won't
be allowed.
-
I've read through some of the pages in your sig but I still don't get it.
The 'q' part works fine, it does it when its supposed to. Problem is how would I get around the fact the switch statement only looks in the first element and not the entire string?
-
>>I've read through some of the pages in your sig but I still don't
get it.
I didn't put a link for cin.getline in my sig, but do look at the link
about gets - its an old function that can easily crash your program
if a person enters too much data. The reason I have an fgets link
is because people don't tend to use gets in C++, its a C function
and fgets is the more modern version.
>>The 'q' part works fine
I'm sure it does, i wasn't contesting that it wouldn't. The reason
i suggested using integers is to get around my next point (in
using integers, entering q would obviously be invalid).
>>how would I get around the fact the switch statement only
looks in the first element and not the entire string?
You cant - at least not with a switch statement. You could make
this program work using an if/else block and using strcmp.
Code:
if (!strcmp(option, "1"))
{
//do stuff
}
else if (!strcmp(option, "2"))
{
//do stuff
}
else if (!strcmp(option, "3"))
{
//do stuff
}
else if (!strcmp(option, "q"))
{
//do stuff
}
else
{
//do default stuff
}
-
Hey thanks, that bit of code worked a charm.
Another question though, because that bit of code I posted at the start is a part of a nested function, I need the while (option[0] != 'q' ); after your bit of code. I can get that to work, but I was wondering is there any way possible to say option[0] != 'q' and 'Q'?
I'm understand now why you said it was best to use intergers :rolleyes:
-
Yes, There is a toupper() and tolower function ...
You can say:
Code:
while ( tolower( variable ) != 'q' )
{
// insert things here
}
of you could always do:
Code:
while ( variable != 'q' || variable != 'Q')
{
// insert things here
}
You can look here ... of the tolower thing there, or is it toupper? I can't remember. Also, the || thing in the while statement means or, so it psedo code reads:
while (variable doesn't equal small q) OR (variable doesn't equal capital Q) etc.
>> Also, see my sig - use cin.getline to read strings in C++.
haha :p
-
its working as I want it now, thanks for all your help, that last bit solved ma problem