Click to See Complete Forum and Search --> : basic C++ help
Vengance_01
09-27-2005, 01:45 AM
I am trying to write a Program that can Take an upper case and lower case State Aberviation and display the full name. Using a switch. Here is my code
// Jeff Flogerzi
// September 12
// CIS-17A
// HW_CHP4 State Abbreviations
#include <iostream>
using namespace std;
int main()
{
char state[2];
cout << "Please Enter one of the Following State Abbrevations:\n";
cout << "NC, SC, GA, FL, or AL.\n\n";
cin >> state;
switch(state)
{
case 'nc':
case 'NC':
cout << "The corresponsing State for the Abbreviation for NC is North Carolina\n";
break;
case 'SC':
case 'sc':
cout << "The corresponsing State for the Abbreviation for SC is South Carolina\n";
break;
case 'GA':
case 'ga':
cout << "The corresponsing State for the Abbreviation for GA is Georgia\n";
break;
case 'FL':
case 'fl':
cout << "The corresponsing State for the Abbreviation for FL is Florida\n";
break;
case 'AL':
case 'al':
cout << "The corresponsing State for the Abbreviation for AL is Alabama\n";
break;
default:
cout << "Input Error, Please enter NC,SC,GA,FL,AL.\n";
}
return 0;
}
eshbach
09-27-2005, 01:54 AM
do you have to use a switch?
i think the fastest way to do this is with a two-dimensional array and a for loop.
Vengance_01
09-27-2005, 05:10 AM
Have not learned Loops or arrays. I can do switch or IF statements, and I think switch is faster of the two.
Murder
09-27-2005, 07:37 AM
1) I really dont think case statements are that much faster than Else if's, they are just a little neater :P
2) You cant do switches on STRINGS and you definitely cant make a char that has 2 umm chars :P... items like that are strings not characters!! (double quotes)
For example, state should be a char array of 3 "NC\0" and you should do ifs with a strcmp.
Hope this helps
CPUmelter
09-28-2005, 03:12 AM
do you have to use an array for this assignment?
Murder
09-28-2005, 06:45 PM
The suspense is killing me!!! Hows it going!?!?!?
Switches only work on types with discrete values. In real life you'd use and if-then-else block using string comparisons. If this is a school assignment and requires doing it using switches, then you can do something like this:
switch (state[0])
{
case 'A':
{
switch (state[1])
{
case 'L':return "Alabama";
case 'K':return "Alaska";
...
etc
}
break;
}
}
Nick_B
09-28-2005, 10:19 PM
Switches only work on types with discrete values. In real life you'd use and if-then-else block using string comparisons. If this is a school assignment and requires doing it using switches, then you can do something like this:
...
And if you only have to do the do the few states you have listed, you can just do a case statement based on the first letter (since they are unique).
flutie98
09-29-2005, 03:38 PM
man i miss c++, my colleges comp sci dep. decided to switch to java...(they are morons)
import java.util.Scanner;
Scanner in=new Scanner(System.in);
double x=0;
System.out.println("Please enter a number");
x = nextDouble();
all that can be done with a cin cout....(and thats the new "easy" way) not to mention java virtual machine is a pos...
Eh, Java isn't that bad. And yes, while console i/o is easier with cin/cout, I cannot recall a single program that I've written outside of school that needed console input. And believe me, GUIs are much easier to write in Java.
Eh, Java isn't that bad. And yes, while console i/o is easier with cin/cout, I cannot recall a single program that I've written outside of school that needed console input. And believe me, GUIs are much easier to write in Java.
Ever use .Net? Extremely easy to make GUI apps in C++ or C#, even if you have been touched by His noodly appendage.
Ever use .Net? Extremely easy to make GUI apps in C++ or C#, even if you have been touched by His noodly appendage.
Actually, I like .NET quite a bit, and C# comes naturally with Java experience. The 'managed extensions for c++' leave something to be desired, but I would consider C++ programming for Win32/MFC much different than C++.NET. The base class libraries differ considerably, even for non-GUI stuff.
And you just can't hear the phrase 'noodly appendage' enough as far as I'm concerned :D