Switch Statement Errors
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:
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
/* Function Prototypes */
void clean (void);
#define QUIT q
#define GETTOTALDAYS a
#define EVENTSGREATER5 b
if (inFile == NULL)
printf("Error in opening events.txt file\n");
char choice; /*store choice for which function to test*/
choice = MenuChoice();
printf("Running the program GetTotalDays...\n");
printf("\n That is not a valid test case, try again...\n");
/* End Switch Statements*/
while (choice !=QUIT);
printf("Finishing the program.\n");
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:
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);
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,
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
Originally Posted by BdON003
I tried this but then I get more errors:
"character constant too long for it's type"
" [warning] overflow in implicit constant conversion"
Oops, put a single quote in the macro.
#define GETTOTALDAYS 'a'
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 ;-)
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
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.
char a = 45;
int b = 'b';
now to figure out the rest :-D
Did you figure out why it thought you have default outside of the switch statement? You have a mismatch of curly braces