Switches with variables

Switches have never been a thing for me but my instructor assigned my class some programs that we have to make and 2 of them involve using switch cases. I am trying to do a nationality check where it checks what letter you put and it'll display the nationality the user has put in.

Ex.

User puts in U

program spits out

U - United States



U - United States
K - United Kingdom
C - Canada
M - Mexico
S - Spain
H - China
J - Japan
Any other codes - Unknown
Last edited on
So what don't you understand about a switch statement?

You can find a description of the switch statement here (near the bottom):
http://www.cplusplus.com/doc/tutorial/control/

Make an attempt to solve the problem and post what you have.
We will try to help with specific problems.

thanks man but I got it working after a few minutes of looking at some very old code I made few months back. The only problem i'm having now that I dont know how picky my professor would be is this.
1
2
3
4
5
6
switch (x)
	{
		case 'U':
			cout << "United States \n";
                        break;
          }


I am curious if there is a way to make it that it'll accept both lowercase and uppercase letters.

Edit: nvm, i got it working with both upper and lower case. Case solved!!! haha i made a funny.
Last edited on
Not sure what approach you took, but one way (probably the best way) is to run `x` through `std::toupper` or `std::tolower` before your switch block.
Topic archived. No new replies allowed.