Thread: Char in a Switch()

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Char in a Switch()

    Another question by me!

    How can I use a Char in a switch()?

    I tried it like this:

    Code:
    char x[2];
    cout<<"Write A or B";
    cin>>x;
    
    
    switch((char)x);
    {
    case("A"):
    cout<<"You wrote <<x<<"!";
    break;
    case("B"):
    cout<<"You wrote <<x<<"!";
    break;
    default:
    cout<<"Bad input!";
    }
    But I get an error message saying:" case label "A" does not reduce to an integer constant "

    I know it's easy to fix.. but how?

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    For char you would test it as 'A' not "A". The first is the intrigral value. The second is the string representation.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: Char in a Switch()

    Code:
    char x[2];
    cout<<"Write A or B";
    cin>>x;
    
    
    switch(x[0]);
    {
    case('A'):
    cout<<"You wrote <<x<<"!";
    break;
    case('B'):
    cout<<"You wrote <<x<<"!";
    break;
    default:
    cout<<"Bad input!";
    }
    Fixed
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    Thx alot! That was all I needed to know!

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    FillYourBrain Reply!

    OW ! One more thing about your post, FillYourBrain, the code only works when I write it like

    switch((char)IC) and not like switch(IC[0])

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    PS

    I used the char name ' IC ' in my own script

  7. #7
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    what is the type if IC?
    char IC[2];
    or what??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM