Thread: Something wrong with my for statement

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Something wrong with my for statement

    I'm getting three or four errors in thi sone statement...?? I can't for the life of me see what they could be.

    Code:
    	int 	stay;
    	int 	car = 0;
        float 	charge;
    
    
    
        printf("How long did the vehicle stay in the garage?");
        scanf("%d" , &stay);
    
        for (stay <= 24; car++) {

    Any help would be appreciated.
    Regards,
    extro

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    for ( /* initialization? */; stay <= 24; car++ ) {
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    68
    I wanted it to be car <=3 but it says operator has no effect(on the stay argument too):

    Code:
    int 	stay;
    	int 	car = 0;
        float 	charge;
    
    
    
        printf("How long did the vehicle stay in the garage?");
        scanf("%d" , &stay);
    
        for(car <= 3; stay <= 24; car++) {

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Extropian
    I wanted it to be car <=3 but it says operator has no effect(on the stay argument too)
    Could you describe in your own words what you think an initialization of car <=3 should do?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    207
    When you say 'car <= 3', you're actually making a comparison. However, the computer is expecting you to assign a value (ie: 'car = 3').

    Also, when you say 'stay <= 24' you're using a variable that is input from the user:

    1) If the user enters anything over 24, then the for loop will not execute
    2) If the 'stay' variable is not changed inside of the for loop, it will execute infinitely

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    68
    I got some working code but I'm getting a syntax error right at the end, any thoughts onw aht could be wrong would be a big help
    Thanks,
    Extro

    Code:
    include<stdio.h>
     float    calculateCharges (float charge);
     int main()
    {
       
    	int 	stay;
    	int 	car = 0;
        float 	charge;
    
    
    
        printf("How long did the vehicle stay in the garage?");
        scanf("%d" , &stay);
    
        for(car <= 3; stay <= 24; car++) {
    
    	    if(stay <= 3){
    	       charge = 2.00;
               printf("%s%13s%13s\n", "Car", "Hours", "Charge");
               printf("%13d%13d%13f\n" ,car, stay, charge);
    }
            if(stay > 3){
               printf("%d%13%13\n," , car, stay , calculateCharges(charge) );
    }
            if(stay == 24){
    	       charge = 10;
               printf("%13d%13d%13f\n" , car, stay, calculateCharges(charge));
    }
    }
    return 0;
    }
    
    float calculateCharges (float charge);
    {
    return stay * 1 / 2;
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Errant semicolon.
    Code:
    float calculateCharges (float charge);
    {
    return stay * 1 / 2;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    207
    Your 'for' loop still seems to be incorrect.

    Also, when you pass 'charge' to your function 'calculateCharges' you don't use it for anything.

    You don't need the semicolon after 'float calculateCharges (float charge);' near the bottom.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    68
    Ok I foxed that but now 'stay' is undeclared. Can I carry it over into the function somehow?

  10. #10
    Registered User
    Join Date
    May 2005
    Posts
    207
    Sure. Just declare it as an argument in your prototype (at the top of your program):

    float calculateCharges (float charge, int stay);

    Once you do that, you can pass 'stay' to the function.

    mw

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    68
    Thanks, that ssemed to work but now I have the error:
    too few arguments to function 'calculateCharges'

    Code:
     printf("%13d%13%13f\n," , car, stay , calculateCharges(charge) );
    Thank for all the help.
    extro

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Extropian
    Thanks, that ssemed to work but now I have the error:
    too few arguments to function 'calculateCharges'
    Put a little more effort into reading the diagnostic messages. If you change your function from receiving one parameter to receiving two parameters, do you think you should call it with one argument or two?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM