954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to display wrong input if i using switch??

i need help with this very simple question....
the default in this program will just display the wrong input
if the input entered by user is different with the data type of the variable which is char..
but not if the user entered the the word which is the character is located in front of the word...

in my program, if the user entered apple instead of 'a',this program will still print that i'm in the red team...

I want this program to display the input error..

this is my code :

#include <iostream>
using namespace std;

int main () {

char answer;

cout<<"Enter Your Team :";
cin>>answer;

switch (answer) {
    case 'a' :
     cout<<"You in the Red Team";
     break;

    case 'b' :
     cout<<"You in the blue team";
     break;

    default :
     cout<<"wrong input";
     break;
}
}
samsons17
Posting Whiz in Training
233 posts since Oct 2009
Reputation Points: 6
Solved Threads: 1
 

Then you should take the input as a string. Then if the size of the string is
greater than 1 then display an error message, else compare it with a or b.
Doing this way you would have to use if/else. This isn't that bad if you
don't have many cases.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You