|
-
Great White Shark
basic C++ help
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;
}
Updated 12/27/05
A64 Desktop Rig
DC Opteron [email protected]
1GB UTT@245FSB 2/2/2/7
X1800XT 512MB @PE Speeds
19" Viewsonic WS LCD
2x160GB ATA HDs
NEC 3550 DL-DVD Burner
12.1 Ibook G3
256MB Ram
30GB
-
Really Cold Shark
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.
-
Great White Shark
Have not learned Loops or arrays. I can do switch or IF statements, and I think switch is faster of the two.
Updated 12/27/05
A64 Desktop Rig
DC Opteron [email protected]
1GB UTT@245FSB 2/2/2/7
X1800XT 512MB @PE Speeds
19" Viewsonic WS LCD
2x160GB ATA HDs
NEC 3550 DL-DVD Burner
12.1 Ibook G3
256MB Ram
30GB
-
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
-
do you have to use an array for this assignment?
-
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:
Code:
switch (state[0])
{
case 'A':
{
switch (state[1])
{
case 'L':return "Alabama";
case 'K':return "Alaska";
...
etc
}
break;
}
}
System specs:
| Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
| Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
| Windows 7 Home 64-bit |
-
 Originally Posted by Zoma
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).
-
Engineer credit to *****!
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...
|Core i5 2500K @ 4.6GHz|MSI Z77-GD65|8.0GB G.skill DDR3 1600|XFX AMD 6870|
|Samsung 830 SSD|HP LP2465 24" LCD|Antec P280|Seasonic X750 PSU|H2O Cooling|Windows 7 x64|
|Core i3 2100|ASRock H61-GM|8.0GB DDR3|AMD Radeon 6450|Kingston V100+ SSD|
|Antec Fusion V2|Epson 8350 1080P 3LCD on 100" EliteScreen|
|ThinkPad T420|Core i5 2520M|8.0GB DDR3|OCZ Vertex 4|
Audio: Radeon 6450 (HDMI)->Onkyo TX-NR509 ->Polk M60/M30/CSII + Velodyne VX10
-
NullPointerException
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.
Open Source is free like a puppy is free.
It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.
Understanding Evolution
-
 Originally Posted by rock
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.
System specs:
| Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
| Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
| Windows 7 Home 64-bit |
-
NullPointerException
 Originally Posted by Zoma
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
Open Source is free like a puppy is free.
It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.
Understanding Evolution
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|