|
-
January 27th, 2011, 11:41 PM
#1
switch statement
Hi all, can someone tell me why I get errors with this code?
Code:
#include <iostream>
using namespace std;
int main(){
char buf;
bool finished=false;
cout << "Enter something " << endl;
cin >> buf;
while(!finished){
switch(buf){
case "sloppyjoe":
cout << "Some good stuff! " << endl;
finished=true;
break;
case "cheeseburger":
cout << "Got french fries? " << endl;
finished=true;
break;
default:
cout << "That isn't on the list " << endl;
finished=true;
break;}}
return 0;
}
When I set case to something like this ...
It compiles and works fine, except when I make it a string. What's the problem? Here's the error I get..
case label does not reduce to an integer constant
Thanx.
-
January 27th, 2011, 11:50 PM
#2
Re: switch statement
case "sloppyjoe":
what is this ?.. you are putting char array as case label. In your code you should use if-else with strcmp with char array instead of char only.
And why do you posted a simply C/C++ query in VC++ forum ?. There is separate forum for this.
-
January 27th, 2011, 11:56 PM
#3
Re: switch statement
I've not had a lot of experience with the switch statement so I suppose that case "sloppyjoe" is weird. But I need to learn how to implement a switch statement that allows this type of behavior. Is there any example you can give me while I continue my research to point me in the right direction? Thanx.
-
January 28th, 2011, 12:44 AM
#4
Re: switch statement
you cant use switch statement to match through array of character. use if-else with char array as I suggested earlier.
-
January 28th, 2011, 04:26 AM
#5
Re: switch statement
 Originally Posted by dellthinker
Hi all, can someone tell me why I get errors with this code?
Code:
#include <iostream>
using namespace std;
int main(){
char buf;
bool finished=false;
cout << "Enter something " << endl;
cin >> buf;
while(!finished){
switch(buf){
case "sloppyjoe":
cout << "Some good stuff! " << endl;
finished=true;
break;
case "cheeseburger":
cout << "Got french fries? " << endl;
finished=true;
break;
default:
cout << "That isn't on the list " << endl;
finished=true;
break;}}
return 0;
}
When I set case to something like this ...
It compiles and works fine, except when I make it a string. What's the problem? Here's the error I get..
Thanx.
why are you only taking one char of the user input? And then in your switch you compre that single char to a char array. Why?
change 'char buff;' to 'std::string buf;'. you will need to #include <string>. then you may use syntax like
Code:
std::string a("aaa"), b("bbb");
if (a == b)
{
// do something
}
else
{
// do something else
}
which is easier to read than a whole load of strcmps
-
January 28th, 2011, 07:50 AM
#6
Re: switch statement
switch in C++ is only allowed for integral types (numeric values without decimal part) ! So what you put behind switch and case should be of integral type or convertible without question to integral type. As 'A' is actually 65 (ascii code) the compiler allows this. However, "sloppyjoe" is an array of characters and therefor not convertible to an integral value. The way you should go, as already mentioned here, is checking the user input string with an if/else/else/else ...
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
|
Click Here to Expand Forum to Full Width
|