// This program displays the name of a state once its two letter abbreviation is entered.
#include <iostream>
usingnamespace 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?
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;
}
elseif (abbrev == "SC")
{
cout << "This state is South Carolina." << endl;
}
//......... And so on ......
else{
cout << "Invalid Entry." << endl;
}
// This program displays the name of a state once its two letter abbreviation is entered.
#include <iostream>
usingnamespace 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;
}
elseif (abbrev == "SC"){
cout << "This state is South Carolina." << endl;
}
elseif (abbrev == "GA"){
cout << "This state is Georgia." << endl;
}
elseif (abbrev == "FL"){
cout << "This state is Florida." << endl;
}
elseif (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.
// This program displays the name of a state once its two letter abbreviation is entered.
#include <iostream>
#include <string.h>
usingnamespace 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.";
}
elseif (strcmp(abbrev, "SC") == 0){
cout << "This state is South Carolina.";
}
elseif (strcmp(abbrev, "GA") == 0){
cout << "This state is Georgia.";
}
elseif (strcmp(abbrev, "FL") == 0){
cout << "This state is Florida.";
}
elseif (strcmp(abbrev, "AL") == 0){
cout << "This state is Alabama.";
}
elseif (strcmp(abbrev, "nc") == 0){
cout << "This state is North Carolina.";
}
elseif (strcmp(abbrev, "sc") == 0){
cout << "This state is South Carolina.";
}
elseif (strcmp(abbrev, "ga") == 0){
cout << "This state is Georgia.";
}
elseif (strcmp(abbrev, "fl") == 0){
cout << "This state is Florida.";
}
elseif (strcmp(abbrev, "al") == 0){
cout << "This state is Alabama.";
}
else{
cout << "Invalid Entry.";
}
cout << endl;
return 0;
}