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?
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?
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?
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.
> 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.
> 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
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):
#include <cstring>
#include <iostream>
usingnamespace 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;
}