Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: switch case

  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts

    switch case

    i have this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void main() {
            char c;
            printf("Please make your treat selection:\n");
            scanf("%s",&c);
            c=toupper(c);
            switch(c)
            {
                    case 'H':
                            printf("Beverage\nThat will be $2\n");
                            break;
            }
    }
    but if i changed 'H' to 'HE' my gcc compiler give me an error. could someone explain?
    Last edited by chrismiceli; 01-24-2003 at 02:25 AM.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Probably because you are switching on a variable of type character which can only hold one character. You will probably want to use the String datatype.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    Louisiana
    Posts
    576
    Thanks
    0
    Thanked 0 Times in 0 Posts
    edited original post. still errors.

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, switch case allows you to use only integers, and character (which again are processed as integers internally) datatypes. 'H' is a character and "HE" is a string, so it wont work. You have to use if (strcmp(somevar, "HE") == 0) to check if the somvar contains "HE"


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •