I'm so not good at this and need help

This is a discussion on I'm so not good at this and need help within the C Programming forums, part of the General Programming Boards category; Yes, sorry this is homework i've been working on this for over a week and am still having major problems. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    12

    Unhappy I'm so not good at this and need help

    Yes, sorry this is homework i've been working on this for over a week and am still having major problems. I'm not asking for someone to write this, I already have and will post it below. I need to know what the heck i'm doing wrong. This thing is due tonight and i'm frustrated to the point of tears. I will admit i'm very grateful this is the only programming class I have to take.

    Here is the code (hope I don't mess this up too)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void main(void);
    
    {
    	wages ();
    	gasmileage();
    	decibel();
    
    }
    	void wages(void)
    {
         int z;    /*  initialize variable for program selection  */
    
             printf("Enter the number of the program you would like to run\n"); /*determine which program to run*/
             printf("1.  Calculate Wages \n");
       	 printf("2.  Calculate Gas Mileage \n");
     	 printf("3.  Evaluate Decibel  level \n");
    	 scanf("%d", &z);       /*read program selection*/
    			
    	 {
    		if (z=1)
    		    int a,b,c,d,e,f;     /*initialize variables for wage calculation*/
    		    printf("Please enter employee number\n, a");     /*prompt for wage info*/
    		    printf("Please enter hourly wage\n, b");
    		    printf("Please enter number of hours worked\n, c");
    		    scanf("%lf %lf %lf", &a,&b,&c);     /*read wage info*/
    		
    			if (c>40)              /*calculate pay with overtime*/
    			    d=c-40;            /*determine amount of overtime*/ 
    			    e=(c*b)+(d*(b+(b/2)));    /*calulate earnings*/
    			    printf("Employee %a whose standard hourly rate of pay is $%b worked %c hours and will be paid $%e\n %lf a, %lf b, %lf c, %lf d");
        			
    			else         /*calculate pay when no overtime*/
    			    f=c*b;
    			    printf("Employee %a worked %c hours at a rate of $%b and will be paid %f.\n, &a, &b, &c, &f");
    
    		return;      /*end of wage*/
    
    	 }
    
    	 void gasmileage(void)
    
    	 {	
    		else if (z=2)
    		    int h,i,j,k,l,m,n,p,q;          /*initialize variable for gas mileage*/
    		    printf("What is the price of 1 gallon of gas?\n");        /*prompt for gas price*/
    		    scanf("%lf", &k);                                         /*read gas price*/
    		    printf("Do you drive a Mini Cooper?  enter 1 for yes 2 for no.\n");   
    		    scanf("%d", &l);
    		    	
    			if (l=1)          /*calculate if mini*/
    			    n=k*10;
    			    printf("A Mini Cooper will go 510 miles on a tank of gas and each tank will cost $%n when gas is $&k \n, n,k");
    			    
    
    			else if 
    			printf("Do you drive a Neon? enter 1 for yes 2 for no.\n");
    			scanf("%d", &m);
    				if (m=1)          /*calculate if Neon*/
    				     p=k*14;
    				     printf("A Neon will go 350 miles on a tank of gas and each tank will cost $%p when gas is $&k \n, p,k"); 
    
    			else if
    			printf("Do you drive an Explorer? enter 1 for yes 2 for no.\n");
    			scanf("%d", &h);
    				if (h=1)          /*calculate if Explorer*/
    				     i=k*30;
    				     printf("An Explorer will go 600 miles on a tank of gas and each tank will cost &%i when gas is $&k \n, i,k");
    
    		return;     /*end of gas mileage*/
    	 }
    
    	void decibel(void)
    
    	 {
    		else if (z=3)
    		    int x;          /*initialize variable for decibel level*/
    		    printf("Enter the decibel level at which you listen to your favorite song\n");
    		    scanf("%d", &x);
    				if (x>0 && x<51)
    				   printf("&x db is quiet.\n");
    				     else if (x>50 && x<71)
    					printf("&x db is intrusive.\n");
    					    else if (x>70 && x<91)
    						printf("&x db is annoying.\n");
    						    else if (x>90 && x<112)
    							printf("&x db is very annoying.\n");
    							     else if (x>111 && x<141)
    								printf("&x db is uncomfortable.\n");
    								    else (x>140)
    									printf("&x db is damaging.\n");
    
    		return;        /*end of decibel*/
    
    	 }
    		else
    		    printf("Shame on you, you didn't enter 1, 2 or 3, please try again!\n");
    		    scanf("%d", &z);
    
    		return;        /*end of program*/
    
    }

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    ok, I see the code, but where's the question about the code?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    When I compile (microsoft visual c++)

    Code:
    I get "error C2449: found '{' at file scope (missing function header?)"  at the very first {
    
    and also "error C2059: syntax error : '}' " at the very last }
    sadly i'm sure there are other problems but first I need to figure out how to fix this one.

    thanks!

  4. #4
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    You have put a semicolon here: void main(); This has caused a parsing error, which means your opening brace isn't interpreted as you would expect by the compiler.

    Also, main should return int, not void. Just change it to int and put 'return 0;' at the end of main.

    edit: You have some other issues with where your braces are as well. It looks like you're using a text editor that indents them for you, as the indentation appears to be fairly correct. Have a look at the pairs of braces which line up vertically - they match each other. Your functions are not ending where you think they are (as indicated by your comments). You have opening braces in the middle of function where there's no need for them. Fix those up and a lot of errors should go away.

    edit 2: Specifically, you need to remove the opening brace just before if (z=1), and you need to remove the closing brace at the end.

    edit 3: (last one I hope!) Also this:
    Code:
    else
    		    printf("Shame on you, you didn't enter 1, 2 or 3, please try again!\n");
    		    scanf("%d", &z);
    should be inside the decibel function (at the end).

    Similarly, you need to change the else if's at the start of your functions to just if's - they do not know about the if's in the previous functions, so else doesn't make sense here.

    This:
    Code:
    return;        /*end of program*/
    should not be there at all - instead you should have a return 0; in main, like I said above.

    One last comment: Don't get too worked up about this. Your mistakes were mostly just syntax errors, which can be easy to do, but hard to find in order to fix them. You need to understand functions a bit better I think (they are entirely modular and do not know about previous functions) but other than that your code is not bad.
    Last edited by lemnisca; 10-27-2005 at 09:01 AM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    ok I got rid of the ;

    and deleted all of the extra braces leaving just the first and last.

    now i'm at 22 errors, I think I can figure some of those out. I don't understand about the return thing though...
    Last edited by notacgirl; 10-27-2005 at 09:03 AM.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Quote Originally Posted by notacgirl
    I don't understand the second part, what do I need to change to int?
    Where you have:
    Code:
    void main()
    you need to have:
    Code:
    int main()
    and then at the end of your main function a line that says:
    Code:
    return 0;
    This is just part of what main is - it is defined to be int main(), so that's what you should use.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Don't delete all of the extra braces! Just the ones I said in my post (see my edits). Braces define sections of your code, in this case functions. Your code should basically look like this:
    Code:
    int main()
    {
       /* do some stuff, call other functions */
       return 0;
    }
    
    void my_function()
    {
        /* do the stuff in my_function */
    }
    
    char a_char_function(char a, char b)
    {
        char a_char;
        /* do some stuff with characters */
        return a_char;
    }
    The types of your functions can vary, but they must all look like:
    Code:
    type function_name(<parameters>)
    {
        /* variable declarations */
        /* processing code */
        /* return statement (not requried if the return type is void */
    }
    You cannot start functions inside of other functions, it is not allowed in C. They must all be separate.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    thank you!!!!!

    I'm going to work on this and see what I can do, I'll post back after my lit class.

    You have no idea how much I appreciate the help!

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    aughhhhh, still having issues.... here's where i'm at right now. I have various sytax errors and it keeps locking up the compiler so I have to shut it down and re-open it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    
    {
    	wages ();
    	gasmileage();
    	decibel();
    
    }
    	void wages(void);
    {
         int z;    /*  initialize variable for program selection  */
    
             printf("Enter the number of the program you would like to run\n"); /*determine which program to run*/
             printf("1.  Calculate Wages \n");
       	 printf("2.  Calculate Gas Mileage \n");
     	 printf("3.  Evaluate Decibel  level \n");
    	 scanf("%d", &z);       /*read program selection*/
    			
    	 {
    		if (z=1)
    		    int a,b,c,d,e,f;     /*initialize variables for wage calculation*/
    		    printf("Please enter employee number\n, a");     /*prompt for wage info*/
    		    printf("Please enter hourly wage\n, b");
    		    printf("Please enter number of hours worked\n, c");
    		    scanf("%lf %lf %lf", &a,&b,&c);     /*read wage info*/
    		
    			if (c>40)              /*calculate pay with overtime*/
    			    d=c-40;            /*determine amount of overtime*/ 
    			    e=(c*b)+(d*(b+(b/2)));    /*calulate earnings*/
    			    printf("Employee %a whose standard hourly rate of pay is $%b worked %c hours and will be paid $%e\n %lf a, %lf b, %lf c, %lf d");
        			
    			else         /*calculate pay when no overtime*/
    			    f=c*b;
    			    printf("Employee %a worked %c hours at a rate of $%b and will be paid %f.\n, &a, &b, &c, &f");
    
    		return;      /*end of wage*/
    
    	 }
    
    	 void gasmileage(void);
    
    	 {	
    	 if (z=2)
    		    int h,i,j,k,l,m,n,p,q;          /*initialize variable for gas mileage*/
    		    printf("What is the price of 1 gallon of gas?\n");        /*prompt for gas price*/
    		    scanf("%lf", &k);                                         /*read gas price*/
    		    printf("Do you drive a Mini Cooper?  enter 1 for yes 2 for no.\n");   
    		    scanf("%d", &l);
    		    	
    			if (l=1)          /*calculate if mini*/
    			    n=k*10;
    			    printf("A Mini Cooper will go 510 miles on a tank of gas and each tank will cost $%n when gas is $&k \n, n,k");
    			    
    
    			else if 
    			printf("Do you drive a Neon? enter 1 for yes 2 for no.\n");
    			scanf("%d", &m);
    				if (m=1)          /*calculate if Neon*/
    				     p=k*14;
    				     printf("A Neon will go 350 miles on a tank of gas and each tank will cost $%p when gas is $&k \n, p,k"); 
    
    			else if
    			printf("Do you drive an Explorer? enter 1 for yes 2 for no.\n");
    			scanf("%d", &h);
    				if (h=1)          /*calculate if Explorer*/
    				     i=k*30;
    				     printf("An Explorer will go 600 miles on a tank of gas and each tank will cost &%i when gas is $&k \n, i,k");
    
    		return;     /*end of gas mileage*/
    	 }
    
    	void decibel(void);
    
    	{
    	 if (z=3)
    		    int x;          /*initialize variable for decibel level*/
    		    printf("Enter the decibel level at which you listen to your favorite song\n");
    		    scanf("%d", &x);
    				if (x>0 && x<51)
    				   printf("&x db is quiet.\n");
    				     else if (x>50 && x<71)
    					printf("&x db is intrusive.\n");
    					    else if (x>70 && x<91)
    						printf("&x db is annoying.\n");
    						    else if (x>90 && x<112)
    							printf("&x db is very annoying.\n");
    							     else if (x>111 && x<141)
    								printf("&x db is uncomfortable.\n");
    								    else (x>140)
    									printf("&x db is damaging.\n");
    
    		else
    		    printf("Shame on you, you didn't enter 1, 2 or 3, please try again!\n");
    		    scanf("%d", &z);
    
    		return;        /*end of decibel*/
    	
    	 
    		else
    		    printf("Shame on you, you didn't enter 1, 2 or 3, please try again!\n");
    		    scanf("%d", &z);
    
    		return;        /*end of program*/
    	}
    	return 0;
    
    }

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Start by properly indenting your code. It makes the multitude of errors easier to see. Oh, and a book on C would help immensely.
    My best code is written with the delete key.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    				     else if (x>50 && x<71)
    					printf("&x db is intrusive.\n");
    					    else if (x>70 && x<91)
    						printf("&x db is annoying.\n");
    						    else if (x>90 && x<112)
    							printf("&x db is very annoying.\n");
    							     else if (x>111 && x<141)
    								printf("&x db is uncomfortable.\n");
    ->
    Code:
    				     else if (x>50 && x<71)
    					printf("&x db is intrusive.\n");
    			             else if (x>70 && x<91)
            				printf("&x db is annoying.\n");
    				     else if (x>90 && x<112)
    					printf("&x db is very annoying.\n");
    				     else if (x>111 && x<141)
    					printf("&x db is uncomfortable.\n");
    And did you mean
    Code:
    printf("%i db is uncomfortable.\n", x);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    if (z=3)
    ->
    Code:
    if(z==3)
    Code:
    void wages(void);
    {
    ->
    Code:
    void wages(void)
    {
    etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    Yes, that's what it was supposed to have been the printf thing, I fixed it thank you. Also fixed the indenting...

    Any idea why it's locking the compiler up?

  14. #14
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by notacgirl
    aughhhhh, still having issues.... here's where i'm at right now. I have various sytax errors and it keeps locking up the compiler so I have to shut it down and re-open it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      wages ();
      gasmileage();
      decibel();
    
      return 0;
    }
    
    void wages(void) /* removed another ; */
    {
      int z;    /*  initialize variable for program selection  */
    
      printf("Enter the number of the program you would like to run\n"); /*determine which program to run*/
      printf("1.  Calculate Wages \n");
      printf("2.  Calculate Gas Mileage \n");
      printf("3.  Evaluate Decibel  level \n");
      scanf("%d", &z);       /*read program selection*/
    
      if (z=1)
    /*  { missing? */
        int a,b,c,d,e,f;     /*initialize variables for wage calculation 'c' initalized here */
        printf("Please enter employee number\n, a");     /*prompt for wage info*/
        printf("Please enter hourly wage\n, b");
        printf("Please enter number of hours worked\n, c");
        scanf("%lf %lf %lf", &a,&b,&c);     /*read wage info*/
    /*  } <-- missing? */		
      if (c>40)
      /* { missing? */              /*calculate pay with overtime*/
        d=c-40;            /*determine amount of overtime*/ 
      e=(c*b)+(d*(b+(b/2)));    /*calulate earnings*/
      printf("Employee %a whose standard hourly rate of pay is $%b worked %c hours and will be paid $%e\n %lf a, %lf b, %lf c, %lf d");
        			
      else /* { missing? */         /*calculate pay when no overtime*/
        f=c*b;
      printf("Employee %a worked %c hours at a rate of $%b and will be paid %f.\n, &a, &b, &c, &f");
    
      return;      /*end of wage*/ /* return WHAT exactly? */
    }
    
    void gasmileage(void) /* removed ANOTHER semicolon */
    {	
      if (z=2)
        int h,i,j,k,l,m,n,p,q;          /*initialize variable for gas mileage*/
      printf("What is the price of 1 gallon of gas?\n");        /*prompt for gas price*/
      scanf("%lf", &k);                                         /*read gas price*/
      printf("Do you drive a Mini Cooper?  enter 1 for yes 2 for no.\n");   
      scanf("%d", &l);
    		    	
      if (l=1)          /*calculate if mini*/
        n=k*10;
      printf("A Mini Cooper will go 510 miles on a tank of gas and each tank will cost $%n when gas is $&k \n, n,k");
    
      else if 
        printf("Do you drive a Neon? enter 1 for yes 2 for no.\n");
      scanf("%d", &m);
      if (m=1)          /*calculate if Neon*/
        p=k*14;
      printf("A Neon will go 350 miles on a tank of gas and each tank will cost $%p when gas is $&k \n, p,k"); 
    
      else if
        printf("Do you drive an Explorer? enter 1 for yes 2 for no.\n");
      scanf("%d", &h);
      if (h=1)          /*calculate if Explorer*/
        i=k*30;
      printf("An Explorer will go 600 miles on a tank of gas and each tank will cost &%i when gas is $&k \n, i,k");
    		return;     /*end of gas mileage*/
    }
    
    void decibel(void)
    {
      if (z=3)
        int x;          /*initialize variable for decibel level*/
      printf("Enter the decibel level at which you listen to your favorite song\n");
      scanf("%d", &x);
      if (x>0 && x<51)
        printf("&x db is quiet.\n");
      else if (x>50 && x<71)
        printf("&x db is intrusive.\n");
      else if (x>70 && x<91)
        printf("&x db is annoying.\n");
      else if (x>90 && x<112)
        printf("&x db is very annoying.\n");
      else if (x>111 && x<141)
        printf("&x db is uncomfortable.\n");
      else (x>140)
        printf("&x db is damaging.\n");
      else /* two else? else what? */
        printf("Shame on you, you didn't enter 1, 2 or 3, please try again!\n");
      scanf("%d", &z);
      return;        /*end of decibel*/
    } /* inserted */	
    /*
    
      All of this I dont know where you wanted it to be?	 
      else
        printf("Shame on you, you didn't enter 1, 2 or 3, please try again!\n");
      scanf("%d", &z);
    
      return;        /*end of program*/
      }
      return 0;
    }
    */
    You have the same repeated errors throughout your program, too many to point out individually, your return statements need to actually return something, you need to look into "scope", also you keep having the same missing brackets/too many brackets, wrong brackets, semicolons out of place, etc...

    Its, slightly better indented now, you may be able to read itm ore clearly and see your mistakes? dunno...its obvious that you dont have the fundamentals down, so as suggested you should get a book on C.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    12
    Sad thing is I have a book on C and it isn't making much sense to me either. I am the first to admit that I'm having trouble grasping the fundamentals

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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