basic C++ help

Sharky Forums


Results 1 to 12 of 12

Thread: basic C++ help

  1. #1
    Great White Shark Vengance_01's Avatar
    Join Date
    Oct 2001
    Location
    Moreno valley,CA
    Posts
    5,911

    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

  2. #2
    Really Cold Shark eshbach's Avatar
    Join Date
    May 2003
    Location
    San Luis Obispo, CA
    Posts
    5,740
    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.

  3. #3
    Great White Shark Vengance_01's Avatar
    Join Date
    Oct 2001
    Location
    Moreno valley,CA
    Posts
    5,911
    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

  4. #4
    Catfish
    Join Date
    Apr 2001
    Location
    PA
    Posts
    218
    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

  5. #5
    Reef Shark
    Join Date
    Nov 2004
    Posts
    352
    do you have to use an array for this assignment?

  6. #6
    Catfish
    Join Date
    Apr 2001
    Location
    PA
    Posts
    218
    The suspense is killing me!!! Hows it going!?!?!?

  7. #7
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615
    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 |

  8. #8
    Hammerhead Shark
    Join Date
    Mar 2001
    Location
    Littleton, CO
    Posts
    2,704
    Quote 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:

    Code:
    ...
    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).
    Nick_B
    Currently running Ubuntu and Windows 7.

  9. #9
    Engineer credit to *****! flutie98's Avatar
    Join Date
    Mar 2002
    Location
    CNSE UAlbany
    Posts
    4,111
    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

  10. #10
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    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

  11. #11
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615
    Quote 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 |

  12. #12
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    Quote 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
  •