How to make the 'switch' function hold 2 characters?

There is this problem I am trying to do, the instructor asked us to do in both "if" and "switch" ways, the "if" was easy but how exactly do you make a switch hold something other than an integer or single character??

Write a program that asks the user to enter one of the following state abbreviations: NC, SC, GA, FL, or AL. the program should then display the name of the state that corresponds with abbreviation entered (North Carolina, South Carolina, Georgia, Florida, or Alabama).

Input Validation: Accept abbreviations with both letters in uppercase or both in lower case. Display an error message if an abbreviation other than one of these five is entered.


This is what I got so far, and obviously it doesn't seem to work:

#include <iostream>
using namespace std;

int main()
{
char state[2];

cout<<"Enter the state abbreviation: ";
cin.get(state,2);

switch(state)
{
case "NC":
case "nc": cout<<"North Carolina";
break;

case "SC":
case "sc": cout<<"South Carolina";
break;

case "GA":
case "ga": cout<<"Georgia";
break;

case "FL":
case "fl": cout<<"Florida";
break;

case "AL":
case "al": cout<<"Alabama";
break;

default: cout<<"You have entered an invalid abbreviation.";
}

cin.get();
return 0;
}

Is there any way I can modify this so it can go through?
Switch case statments can only have cases that represent a single integer or character.

If you look closely, each state is uniquely specified by its first letter. I think that's why your prof chose those specific abbreviations.
Last edited on
But it also says to display the default message if any other abbreviation is entered, such as one from another state. How would it distinguish between AL and AR?
will it be okay if i post a different set of codes? i'll try to create a running program but i am working it in dev c++ compiler. will it be fine?
Sure, I just want to see how such a program would work

Edit: Yea the if/else was simple to do with this problem, just trying to figure out how to do this switch one. Is there a way to use the AND logical operator within a case?
Last edited on
ok. because i am more familiar in using if-else statement..
This is one way to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string state ;
std::cout << "state? " && std::cin >> state ;
if( state.size() != 2 ) { /* error */ }

state[0] = std::toupper( state[0] ) ;
state[1] = std::toupper( state[1] ) ;

// assumes that the max value that a char can hold is less than 10000
enum { MULTIPLIER = 10000 } ; 
const long long key = state[0] * MULTIPLIER + state[1] ;

switch(key)
{
     case 'N' * MULTIPLIER + 'C' : /* North Carolina */ break ;
     case 'S' * MULTIPLIER + 'C' : /* South Carolina */ break ;
     // etc ...
     default : /* error */
}
I'm always seeing those : : things and other terms such as "std" (I have only used std in the namespace) I that I am clueless about. Are there different ways of learning C++ or why is it I haven't even seen any : : in my book so far? Is it an older style?

Also, I'd use that code but I don't understand half of it
You can nest the switch case statements for simplicity. First check the case for N then if it's true check if the second letter is C by using another switch statement inside the N case if not print an error. It's more long winded but I think that might be the point of the assignment. To compare and contrast if vs switch statements depending on what you want to check.
Doesn't seem to be working, tried nesting the case of 'N' with another switch that had the case 'C' and nothing
> I'm always seeing those : : things and other terms such as "std" ...
> ... why is it I haven't even seen any : : in my book so far?

Your book may be dumping a using namespace std ; right t the top of every code snippet.

There was a recent discussion here about the pros and cons of doing that:
http://www.cplusplus.com/forum/general/72248/


> I'd use that code but I don't understand half of it

Once you realize that std::cin or std::toupper() can be written as just cin or toupper() with a using directive in place, you should be able to figure out how a unique integer key is made for each two character sequence.

Seems to work, if you don't mind though, would you explain what these lines are actually doing?

enum { MULTIPLIER = 10000 } ;
const long long key = state[0] * MULTIPLIER + state[1] ;

And what is the toupper function for? And why did you put a 0 and 1 in brackets there?
Last edited on
> would you explain what these lines are actually doing?
>
1
2
enum { MULTIPLIER = 10000 } ; 
const long long key = state[0] * MULTIPLIER + state[1] ;

> And why did you put a 0 and 1 in brackets there?

state[0] is the first char in the string, the one at at position 0,
state[1] is the second char in the string, the one at at position 1.

if the string state holds "AB", state[0] is 'A' and state[1] is 'B'
Assuming that 'A' has a value of 65, and 'B' has a value of 66,
state[0] * MULTIPLIER + state[1] is 65 * 1000 + 66, or 65066

> And what is the toupper function for

http://en.cppreference.com/w/cpp/string/byte/toupper
We just convert each lower case character in the input to upper case first.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
switch (state[0]) {
case 'N':
case 'n':
        switch (state[1]) {
        case 'C':
        case 'c':
                 cout << "North Carolina" << endl;
                 break;

        default:
                  cout << "Invalid state" << endl;
                  break;
        }
        break;

// other cases

default:
     cout << "Invalid State" << endl;
     break;
}
BTW, the reason a switch doesn't work for this is (basically) the same reason that you can't compare C-strings with the == operator, (although that will at least compile.)

Here's an example of what I mean, along with some output from a run on my machine; (the output will be machine and runtime specific):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstring>
#include <iostream>

using namespace std;

/**
 * Invoke the program like this:
 * %>program_name.exe Test
 *
 * argc: the number of arguments passed into the program + 1
 * argv: the value of those arguments (starting at index 1)
 */
int main(int argc, char *argv[]) {
	//check that there is an argument
	if( argc <= 1 ) {
		cerr << "Please supply an argument.\n";
		return 1;
	}

	//doesn't even compile
	/*
	switch( argv[1] ) {
	 case "Test":
		cout << "Yea!\n";
		break;
	 default:
		cout << "Nea!\n";
	}
	*/

	//doesn't work like you might expect
	if( argv[1] == "Test" )
		cout << "Yea!\n";
	else
		cout << "Nea!\n";

	//this is why
	cout << (void*)"Test" << endl;
	cout << (void*)argv[1] << endl;
	
	//one way you could do it correctly
	if( strcmp(argv[1], "Test") == 0 )
		cout << "Yea!\n";
	else
		cout << "Nea!\n";
	
	return 0;
}
Nea!
000000013FB9D394
00000000004B38AE
Yea!
Last edited on
Topic archived. No new replies allowed.