In: Computer Science
Consider the following two-letter acronym of the following states in the United States:
MD - Maryland, FL - Florida, PA - Pennsylvania, Ga - Georgia, CA - California, NY - New York.
Imagine that some students from the above-listed states are at a conference. Using a switch control structure write a C++ program that prompts a student at the conference to enter the two-letter acronym of his/her state and let the program print the student's state and welcome the student to the conference. For example, if the student entered MD then the program should print "Your state is Maryland. Welcome."
C++ code:
#include <iostream>
using namespace std;
int main()
{
//initializing two charcters to store the letters
char ch1,ch2;
//asking for acronym
cout<<"Enter the acronym: ";
//accepting both characters
cin>>ch1>>ch2;
//switching on value of 1st character minus the second
switch(ch1-ch2){
//if the 2 characters are M and D
case 'M'-'D':
//printing the state
cout<<"Your state is Maryland. Welcome."<<endl;
//exiting the switch case
break;
//if the 2 characters are F and L
case 'F'-'L':
//printing the state
cout<<"Your state is Florida. Welcome."<<endl;
//exiting the switch case
break;
//if the 2 characters are P and A
case 'P'-'A':
//printing the state
cout<<"Your state is Pennsylvania.
Welcome."<<endl;
//exiting the switch case
break;
//if the 2 characters are G and A
case 'G'-'A':
//printing the state
cout<<"Your state is Georgia. Welcome."<<endl;
//exiting the switch case
break;
//if the 2 characters are C and A
case 'C'-'A':
//printing the state
cout<<"Your state is California. Welcome."<<endl;
//exiting the switch case
break;
//if the 2 characters are N and Y
case 'N'-'Y':
//printing the state
cout<<"Your state is New York. Welcome."<<endl;
//exiting the switch case
break;
}
return 0;
}
Screenshot:
Input and Output: