Switch and integer constants

I am working on this program out of my book. I am supposed to:

1. Ask the user to enter one of five state abbreviations.
2. Display the name of the state that abbreviation refers to.

This is my program.
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
// This program displays the name of a state once its two letter abbreviation is entered.

#include <iostream>

using namespace std;

int main(){
    char abbrev[3];

    cout << "What is the abbreviation for the state?" << endl;
    cin >> abbrev;
    cout << endl;

    switch(abbrev){
        case 'NC':    cout << "This state is North Carolina." << endl;
                    break;
        case 'SC':    cout << "This state is South Carolina." << endl;
                    break;
        case 'GA':    cout << "This state is Georgia." << endl;
                    break;
        case 'FL':    cout << "This state is Florida." << endl;
                    break;
        case 'AL':    cout << "This state is Alabamba." << endl;
                    break;
        default:    cout << "Invalid Entry." << endl;
                    break;
    }

    return 0;
}


I know I have read that switch needs an integer, but I have also seen an example which uses a char. What do I have to do to allow it to use 2 letters? or do i have to use if, else if, else if, and so on?
Last edited on
Just replace
'NC'
with
"NC"

The single quotes tell the compiler that you are working with a single character. The double quotes tell the compiler that you are working with a char* or char array.

The reason you need an array of 3 is because to find the end of the array the compiler reads until it hits a "special" character which is automatically introduced at the end of a string.
"NC" has three characters: 'N', 'C'. '\0'. Where '\0' is the null terminating character.
I believe an if() else if() arrangement would work better for this purpose.
1
2
3
4
5
6
7
8
9
10
11
12
if (abbrev == "NC")// Note the double quotes, single quotes are used for single chars
	{
	cout << "This state is South Carolina." << endl;
	}
else if (abbrev == "SC")
	{
	cout << "This state is South Carolina." << endl;
	}
//......... And so on ...... 
else{
	cout << "Invalid Entry." << endl;
	}

Last edited on
Thanks. It is at least running now, but it always skips to the else and does not recognize the NC, SC, GA, FL, AL entries.

This is what I have now:
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
// This program displays the name of a state once its two letter abbreviation is entered.

#include <iostream>

using namespace std;

int main(){
    char abbrev[3];

    cout << "What is the abbreviation for the state?" << endl;
    cin >> abbrev;
    cout << endl;

    if (abbrev == "NC"){
        cout << "This state is North Carolina." << endl;
    }
    else if (abbrev == "SC"){
        cout << "This state is South Carolina." << endl;
    }
    else if (abbrev == "GA"){
        cout << "This state is Georgia." << endl;
    }
    else if (abbrev == "FL"){
        cout << "This state is Florida." << endl;
    }
    else if (abbrev == "AL"){
        cout << "This state is Alabama." << endl;
    }
    else{
        cout << "Invalid Entry." << endl;
    }

    return 0;
}


I have tried cin.getline(abbrev, 3); instead of just cin >> abbrev; but that didn't seem to change anything.
Must you have to use char!?
If you can use string, you just need to change the declaration and it'll work
I guess I haven't learned enough about strings yet, so I instead probably made a lot more work for myself using strcmp.

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
49
50
51
52
// This program displays the name of a state once its two letter abbreviation is entered.

#include <iostream>
#include <string.h>

using namespace std;

int main(){
    char abbrev[3];

    cout << "What is the abbreviation for the state?" << endl;
    cin >> abbrev;
    cout << endl;

    if (strcmp(abbrev, "NC") == 0){
        cout << "This state is North Carolina.";
    }
    else if (strcmp(abbrev, "SC") == 0){
        cout << "This state is South Carolina.";
    }
    else if (strcmp(abbrev, "GA") == 0){
        cout << "This state is Georgia.";
    }
    else if (strcmp(abbrev, "FL") == 0){
        cout << "This state is Florida.";
    }
    else if (strcmp(abbrev, "AL") == 0){
        cout << "This state is Alabama.";
    }
    else if (strcmp(abbrev, "nc") == 0){
        cout << "This state is North Carolina.";
    }
    else if (strcmp(abbrev, "sc") == 0){
        cout << "This state is South Carolina.";
    }
    else if (strcmp(abbrev, "ga") == 0){
        cout << "This state is Georgia.";
    }
    else if (strcmp(abbrev, "fl") == 0){
        cout << "This state is Florida.";
    }
    else if (strcmp(abbrev, "al") == 0){
        cout << "This state is Alabama.";
    }
    else{
        cout << "Invalid Entry.";
    }

    cout << endl;

    return 0;
}


This works now. Thanks. :)
Topic archived. No new replies allowed.