switch statment problem and default logic error

i keep getting the same problem with the switch statement and the default, the problem should work like this: the user enters one of the abbreviated states Al,NC and so on but when i enter, it just says "This is not one of the Abbreviation listed" where did i go wrong?




#include<iostream>
using namespace std;
int main ()

{

system("TITLE idono Sate De-Abbreviator");
system("Color 7");

char choice;

cout <<"Enter a State of theses States to be De-Abbreviated:"<<endl;
cout <<" NC,SC,GA,FL and Al :";
cin >>choice;

switch (choice)
{


case 'nc':
case 'Nc':
case 'NC':cout<<"De-Abbreviation complete: North Carolina"<< endl;
break;


case 'sc':
case 'Sc':
case 'SC':cout <<"De-Abbreviation complete: South Carolina" <<endl;
break;


case 'ga':
case 'Ga':
case 'GA':cout <<"De-Abbreviation complete: Georgia" << endl;
break;


case 'fl':
case 'Fl':
case 'FL': cout <<"De-Abbreviation complete: Florida" << endl;
break;


case 'al':
case 'Al':
case 'AL': cout <<"De-Abbreviation complete: Alabama" << endl;
break;


default: cout <<" This is not one of the Abbreviation listed"<<endl;





}

return 0;

}
Last edited on
choice is defined as 1 char. So it'll only take the first char in. Your better off declaring that as a string.

You case is also using ' ' which will only test for 1 char.
Hey u kiddin zaita
the swithch is designed for numeric data and not strings
u can use a single char as it is also a value b/w 0-255.

so modify it to choose from " n,s,g,f or a"
It will work
all the best.
thanks eshwar it was that easy it works!!
Yeah, that'll work until you try to add both NJ and NY to your switch. Or WA and WI. Or any of the eight states that start with M. What about territories and possessions?
Yea sorry I should've clarified that a switch also won't work for strings. I had assumed the OP knew this.
Topic archived. No new replies allowed.