Thread: Getting illegal case error

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Getting illegal case error

    I am getting an illegal case error when compiling the following code listed below. I am a novice, I have reread the section on switch statements in the 4 books I have, and cannot figure out what I am doing wrong. Please steer me in the right direction.

    Code:
    /* This program is the ATM machine */
    
    #include <stdio.h>
    
    /* Declare Global Variables */
    
    /* Line 20 */
    float checking_balance = 5325.72;
    float savings_balance = 20456.98;
    int menu_choice;
    
    /* Print the ATM machine menu for the customer */
    
    void print_menu(void)
    {
    		printf("USA Bank \n");
    /* Line 30 */
    		printf("1 - Withdrawls \n");
    		printf("2 - Deposits \n");
    		printf("3 - Transfers \n");
    		printf("4 - Balances \n");
    		printf("5 - Quit \n");
    }
    
    
    
    /* Line 40 */
    void get_menu_choice(void)
    {
    		printf("Enter you selection:");
    		scanf("%i", &menu_choice);
    }
    
    
    /* defining functions */
    
    /* Line 50 */
    int checking_or_savings(void) /* Needed to add two underscores for this to work */
    {
    	int n;
    
    		printf("Enter 1 for checking or a 0 for savings \n");
    		scanf("%i", &n);
    		return (n);
    }
    
    /* Line 60 */
    
    
    void balances(void)
    {
    		printf("checking: %.2f\n", checking_balance);
    		printf("savings: %.2f\n", savings_balance);
    }
    
    
    /* Line 70 */
    /* need to add some error checking to this function */
    void withdrawls(void)
    {
    	int type;
    	float w;
    
    		printf("Which account type? \n");
    		type = checking_or_savings();
    
    /* Line 80 */
    		printf("Withdrawl amount:");
    		scanf("%f", &w);
    
    		if(type)
    			checking_balance = checking_balance - w;
    		else
    			savings_balance = savings_balance - w;
    }
    
    /* Line 90 */
    
    
    void deposits(void)
    {
    	int type;
    	float d;
    
    		printf("Which account type? \n");
    		type = checking_or_savings();
    /* Line 100 */
    		printf("Deposit amount:");
    		scanf("%f", &d);
    
    		if(type)
    			checking_balance = checking_balance + d;
    		else
    			savings_balance = savings_balance + d;
    }
    
    /* Line 110 */
    
    /* need to add some error checking to this function */
    void transfers(void)
    {
    	int type;
    	float amount;
    
    		printf("Source account:");
    		type = checking_or_savings();
    /* Line 120 */
    		printf("Enter amount of transfer:");
    		scanf("%f", &amount);
    
    		if(type == 1)
    
    		{
    			checking_balance = checking_balance - amount;
    			savings_balance = savings_balance + amount;
    		}
    
    		else
    /* Line 130 */
    		{
    			savings_balance = savings_balance - amount;
    			checking_balance = checking_balance + amount;
    		}
    }
    
    
    
    
    void main(void)
    /* Line 140 */
    {
    	do
    	{
    		print_menu();
    		get_menu_choice();
    
    		switch(menu_choice);
    		{
    		case '1': withdrawls();
    				break;
    		case 2: deposits();
    				break;
    		case 3: transfers();
    				break;
    		case 4: balances();
    				break;
    		case 5: printf("Thank You for your business");
    				break;
    		default : printf("Invalid selection...  Please try again \n");
    				break;
    		}
    
    	}
    		while(menu_choice != 5);
    }
    I get the following errors from Microsoft Visual C++ 6.0 when I compile

    Compiling...
    Week 2 Problem ATM.c
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(21) : warning C4305: 'initializing' : truncation from 'const double ' to 'float '
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(22) : warning C4305: 'initializing' : truncation from 'const double ' to 'float '
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(152) : error C2046: illegal case
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(154) : error C2046: illegal case
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(156) : error C2046: illegal case
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(158) : error C2046: illegal case
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(160) : error C2046: illegal case
    C:\Program Files\Microsoft Visual Studio\MyProjects\Week 2 Problem ATM\Week 2 Problem ATM.c(162) : error C2047: illegal default
    Error executing cl.exe.

    Week 2 Problem ATM.exe - 6 error(s), 2 warning(s)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    switch(menu_choice);
    Get rid of the semicolon.

    Also, main() should be declared as returning int, not void.

    One of your case statements is '1' where the others are integers 2,3,4,5. Is this intentional?
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Talking Thanks

    Thanks, removing the semi colon after switch(menu_choice) fixed the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM

Tags for this Thread