Switch Statement Errors

This is a discussion on Switch Statement Errors within the C Programming forums, part of the General Programming Boards category; Hi, I'm having a strange error in my program. I my program has multiple function prototypes so I need a ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30

    Switch Statement Errors

    Hi,

    I'm having a strange error in my program. I my program has multiple function prototypes so I need a menu for the user to choice which function they need to use. When I use integers in my menu the switch statements work but when I use letters (char) instead I get errors and my program will not compile. I cannot figure out why it likes int better then char. Here is a few parts of the program with one prototype for example:





    Code:
    /* Function Prototypes */
    
    char MenuChoice();
    void GetTotalDays(FILE*inFile);
    void clean (void);
    
    
    #define QUIT q
    #define GETTOTALDAYS a
    #define EVENTSGREATER5 b
    
    int main()
    {
    
    FILE *inFile;
    
    inFile=fopen(EventFile, "r");
    
    if (inFile == NULL)
    {
       printf("Error in opening events.txt file\n");
    }  
    else
    {
        char choice; /*store choice for which function to test*/
        
        do 
        {    
        choice = MenuChoice();
         
        switch (choice)
        {     
                case GETTOTALDAYS:
                  
                    printf("Running the program GetTotalDays...\n");
                    GetTotalDays(inFile);
         }
                    break;
             
               
    
           default:
           if (choice!=QUIT)
           printf("\n That is not a valid test case, try again...\n");
           break;
    }               
    
    /* End Switch Statements*/
    
          }
          while (choice !=QUIT);
          printf("Finishing the program.\n");
          system("PAUSE");
           return 0;
    }
    
    /*************************************/
    So when I try to compile the c compiler complains that " 'a' is undeclared (first use in this function)" as well as 'q', and any other char

    but 'a' and 'q' etc. has been declared up at the beginning of the function and when I experiment and define "GETTOTALDAYS 1" it works.

    Here is the rest of the code for the Menu:

    Code:
    char MenuChoice()
    {
         char choice;
         
        
         printf(" Please Enter the letter corresponding to the function you wish to use:\n");
         
         printf("%c: Quit the program.\n", QUIT);
         printf("%c: Use GetTotalDays(FILE *inFile)\n", GETTOTALDAYS);
         printf("%c: Use EventsGreater5(FILE *inFile)\n", EVENTSGREATER5);
         
         scanf("%c",&choice);
         clean();
         printf("\n\n");
         
         return choice;
    }

    The same error saying that 'q', 'a', etc. is undeclared in the function prototype GetChoice


    I would apprectiate any input at all please,


    Thanks a ton,


    melodia

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    You need to put a single quote around GETTOTALDAYS. That line is being compiled as "case a:" but it should be "case 'a':"

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    Quote Originally Posted by BdON003 View Post
    You need to put a single quote around GETTOTALDAYS. That line is being compiled as "case a:" but it should be "case 'a':"
    sorry do you mean a single quote around

    switch (choice)
    {
    case GETTOTALDAYS:


    changing to:

    switch (choice)

    {
    case 'GETTOTALDAYS'

    I tried this but then I get more errors:


    "character constant too long for it's type"
    " [warning] overflow in implicit constant conversion"

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Oops, put a single quote in the macro.

    #define GETTOTALDAYS 'a'

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    sweet thank you!

    Why do you need to use single quotes for char but not when you use int when you declare?

    Could I bother you with one more question?

    I've used default previously in switch statements with other programs and when I use it here it says that default "case label is not within a switch statment"


    Thanks again!! This has been frusterating ;-)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because we want to get the value of the letter, not the letter itself. a is not a number, but 'a' is (based on the internal encoding of your computer, probably ASCII). You can just as well do
    Code:
    char a = 45;
    int b = 'b';
    And your default case needs to be inside your switch statement. Find the closing curlicue for your switch, and put default before it, and not after it.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    British Columbia, B.C.
    Posts
    30
    Thank you!
    now to figure out the rest :-D

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    48
    Did you figure out why it thought you have default outside of the switch statement? You have a mismatch of curly braces

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. Switch Statement
    By DarkDot in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2007, 10:11 PM
  4. switch statement that uses loop
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21