-
switch/case problem?
Hello, in part of the program I'm working on, I have it so the user inputs either y, Y, n, or N for an answer using cin.getline. Though in my switch function, my compiler tells me "error C2450: switch expression of type 'char [2]' is illegal"
here is my sample code:
Code:
void botguy(){
clear_screen();
cout<<"Is Botguy On Your Side? (y/n): ";
cin.getline(bot_an,2);
switch(bot_an){
case "y":
Any help?
~Wiiplayer12
-
You can't use a character array to control a switch. You have to use an integral type.
However, because you only want the user to type a single character, you can use the char type here. (A char is just an integer code for a character, which is why it works in a switch.)
So instead of getline, use get or operator>> to read into a single char variable, and not a char[2]. Then make the case statements use chars by putting single quotes instead of double quotes.
-
wow, that was fast! Thanks alot, I'm new at C++ so yeah. Thanks again for the help, do you want the ful code? i kinda doubt you'll need it thoguh, but if you want to...
anyways, thanks
~Wiiplayer12