Thread: Trouble with case structure

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    Trouble with case structure

    I am getting an error,

    Parse Error, expecting `CASE' or `DEFAULT' or `'}''
    'switch(pay) { Default 1: set pay = 10'
    in miracle (yes I am installing a dif compiler) I am still new at this and will take all information as constructive. Thanks



    Code:
    #include <stdio.h> 
    maian () 
    { 
        char name [30];
        int pay = 0;       /*selected from case group by alph corispondant # */ 
        int hours = 0;      /*user input with if elas statments */ 
        int hlhrs = 0;      /*user input in conjunction with the if eals statments */ 
        int ot = 0;         /*mathmatical calculation with in the if eals than statments*/ 
        int othol = 0; 	
        int check = 0;      /*total amount of pay for the week*/       
            printf ("What is your Name?"); 
            scanf ("%s", name); 
            printf ("Good Day to you %s."); 
            printf ("%s" "please chose one of the following choses so we know your hourly pay.");
            printf ("1 : $10.00 an hour.");
            printf ("2 : $20.00 an hour.");
            printf ("3 : $30.00 an hour.");
            printf ("4 : $50.00 an hour."); 
              scanf ("%d", pay);      
    switch(pay)
    	{ 
                    Default 1: 
                            set pay = 10; 
                            Break; 
                    case 2: 
                            pay = 20; 
                            break; 
                    case 3:                 
                            pay = 30; 
                            break; 
                    case 4: 
                            pay = 50; 
                            break;                         
    	} //end switch 
    
            printf ("So How many Hours did you work?");
                    scanf ("%d", hours); 
            
    if (hours < = 40 ) 
    { 
            pay * hours = check 
                    printf ("Your pay this week will be $,%check"); 
    } 
    	 else if (hours > 40) 
    { 
    	        printf ("How many Holiday and Sunday Hours did you have? IF any!"); 
                    scanf ("%d", hlhrs); 
                    
            	Printf ("How many Overtime hours did you work? If any!");
                    scanf ("%d", ot); 
                            (hlhrs * 2) + (ot * 1.5) + hours - (hlhrs + ot) = 
    check         
     }// end if eals 
    
          		printf ("Your pay for the week will be $,&check"); 
            
         	        printf ("Do you want to know your yearly income with no Overtime, Sunday or Holiday pay?");
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534


    Code:
    kermit@minty ~/cprogs/board $ gcc -Wall -o caser caser.c
    caser.c:3: warning: return type defaults to `int'
    caser.c: In function `maian':
    caser.c:13: warning: too few arguments for format
    caser.c:14: warning: too few arguments for format
    caser.c:19: warning: format argument is not a pointer (arg 2)
    caser.c:22: error: `Default' undeclared (first use in this function)
    caser.c:22: error: (Each undeclared identifier is reported only once
    caser.c:22: error: for each function it appears in.)
    caser.c:22: error: parse error before numeric constant
    caser.c:24: error: `Break' undeclared (first use in this function)
    caser.c:37: warning: format argument is not a pointer (arg 2)
    caser.c:39: error: parse error before '=' token
    caser.c:7: warning: unused variable `hlhrs'
    caser.c:8: warning: unused variable `ot'
    caser.c:9: warning: unused variable `othol'
    caser.c:10: warning: unused variable `check'
    caser.c: At top level:
    caser.c:47: error: parse error before string constant
    caser.c:47: warning: type defaults to `int' in declaration of `scanf'
    caser.c:47: error: conflicting types for 'scanf'
    caser.c:47: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    caser.c:47: error: conflicting types for 'scanf'
    caser.c:47: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    caser.c:47: warning: data definition has no type or storage class
    caser.c:49: error: parse error before string constant
    caser.c:49: warning: type defaults to `int' in declaration of `Printf'
    caser.c:49: warning: data definition has no type or storage class
    caser.c:50: error: parse error before string constant
    caser.c:50: warning: type defaults to `int' in declaration of `scanf'
    caser.c:50: warning: data definition has no type or storage class
    caser.c:51: error: parse error before '*' token
    caser.c:55: error: parse error before string constant
    caser.c:55: warning: type defaults to `int' in declaration of `printf'
    caser.c:55: error: conflicting types for 'printf'
    caser.c:55: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    caser.c:55: error: conflicting types for 'printf'
    caser.c:55: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
    caser.c:55: warning: data definition has no type or storage class
    caser.c:57: error: parse error before string constant
    caser.c:57: warning: type defaults to `int' in declaration of `printf'
    caser.c:57: warning: data definition has no type or storage class
    wow man - you have got yourself some problems there. First of all, you need to learn to type better.

    Now the one function necessary in every C program (except maybe some embedded programs??) is the main function - not 'maian ', and it returns an int - you can declare it like this:

    Code:
    int main(void)
    {
    
    /* other code */
    
    return 0;
    }
    Also, you should #include <stdio.h> when you are using functions from that library, such as printf and scanf.

    This:
    Code:
     scanf("%s", name);
    is as bad as using the gets function, in that you can overrun a buffer, as there is no way to control user input. Have a look at the FAQ on how to Get a line of text from the user/keyboard.

    Code:
     printf("Your pay this week will be $,%check");
    You really ought to read some documentation on the proper usage of printf. Generally speaking, when you want to display an int (as in the variable check) you would do something like this:
    Code:
     printf("Your pay this week will be $%d\n", check);
    Code:
    scanf("%d", ot);
    When you are grabbing a scalar value with scanf, you need to use the 'address of' operator like so:

    Code:
    scanf("%d", &ot);
    And what did you mean by this?

    Code:
     (hlhrs * 2) + (ot * 1.5) + hours - (hlhrs + ot) = check}
    Had you wanted to assign the result of that computation to some variable? Maybe you meant to do this?

    Code:
    check = (hlhrs * 2) + (ot * 1.5) + hours - (hlhrs + ot)}
    Code:
     set pay = 10;
    Nope - that won't work either. 'set' is not a valid C keyword, and you cannot use spaces in variable names, so what you get is this:

    Code:
    caser.c:26: error: `set' undeclared (first use in this function)
    caser.c:26: error: (Each undeclared identifier is reported only once
    caser.c:26: error: for each function it appears in.)
    caser.c:26: error: parse error before "pay"
    Get rid of 'set'

    Code:
    Break;
    
    linker output:
    
    caser.c:27: error: `Break' undeclared (first use in this function)
    caser.c:27: error: (Each undeclared identifier is reported only once
    caser.c:27: error: for each function it appears in.)
    Careful with your typing - 'Break' is not a C keyword, but 'break' is.

    Next....

    Code:
     printf("Good Day to you %s.");
    You never passed 'name' to printf as an argument to correspond with %s. Try:

    Code:
     printf("Good Day to you %s.\n", name);
    also for,

    Code:
     printf("%s"
                   "please chose one of the following choses so we know your hourly pay.");
    ..you may as well do:

    Code:
     printf("Please chose one of the following choses so we know your hourly pay.\n");
    Code:
     pay *hours = check
    Well, you missed a semi-colon here, but that is the least of your problems. You cannot do things like what you have - did you mean

    Code:
    check = pay * hours;
    ??

    Code:
     Printf("How many O
    Again, watch those capitals! its printf, not Printf


    Code:
    Default 1: 
                            set pay = 10; 
                            Break;
    You can have a default case, but it looks like this:

    Code:
     switch (pay) {
            case 1:
                    pay = 10;
                    break;
            case 2:
                    pay = 20;
                    break;
            case 3:
                    pay = 30;
                    break;
            case 4:
                    pay = 50;
                    break;
    	default:
    	  /* some stuff  to catch the case that 'can't happen */
    	  break;
            }
    Last edited by kermit; 12-31-2005 at 04:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  4. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 10:03 AM
  5. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM