c program help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sleepwalker817
    New Member
    • Mar 2008
    • 6

    c program help

    Hello,
    I need some help with a c program that is supposed to determine the kilometers per liter for 4 tanks of gasoline that a user fills in his/her car.

    Here is what I have so far:
    [code=c]
    for(i=1;i<=4;i+ +)
    {
    printf("/n/nEnter the number of kilometer for tank %d#: ",i);
    scanf("%d", &km);
    printf("Enter the number of liters used by tank %d#: ",i);
    scanf("%f", <);
    res = (float)km / lt;
    printf("*** The kilometers per liter for tank %d# is %.1f", i, res);
    }
    getchar();
    printf("Your overall average kilometers per liter for 4 tanks is /n/n");
    printf("Thanks for using the Sears KPL calculator program.");
    }
    [/code]
    As it is now I get the error message: parse error after '<'.

    Thanks in advance.
    Last edited by sicarie; Mar 14 '08, 01:05 AM. Reason: Code tags
  • manjuks
    New Member
    • Dec 2007
    • 72

    #2
    Hi,

    scanf("%f", <); what is '>' . You have to give lt right?
    remove < and give lt. it will work.

    Thanks,
    Manju

    Comment

    • Sleepwalker817
      New Member
      • Mar 2008
      • 6

      #3
      Thank you for your help

      Code:
      Full code removed
      Can you help me with the average?
      Last edited by sicarie; Mar 14 '08, 01:05 AM. Reason: Posting Guidelines

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        What's the problem with calculating the average?

        Comment

        • Sleepwalker817
          New Member
          • Mar 2008
          • 6

          #5
          Originally posted by oler1s
          What's the problem with calculating the average?
          For some reason Dev-C++ is displaying the average part of the program, so I can't tell if it is working properly. Can you tell me if what I have is correct or what I have to fix? Thanks.

          Code:
          #include<stdio.h>
          #include<conio.h>
          int main()
          {
           int km, i;
           float res, lt, sum, avg;
          
           printf("Welcome to the Sears kilometers per liter calculator.\n\n");
          
           for(i=1;i<=4;i++)
           {
            printf("\n\nEnter the number of kilometer for tank %d#: ",i);
            scanf("%d", &km);
            printf("Enter the number of liters used by tank %d#: ",i);
            scanf("%f", &lt);
            res = (float)km / lt;
            printf("*** The kilometers per liter for tank %d# is %.1f", i, res);
           }
           sum = 0;
           sum = sum + res;
           avg = sum/4;
           printf("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
           printf("\n\nThanks for using the Sears KPL calculator program.");
           getchar();
          }

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            The closing for loop brace seems to need to be relocated 2 lines down....i think?

            Comment

            • Nanite
              New Member
              • Mar 2008
              • 1

              #7
              All your are doing is is dividing result of the last tank by 4.


              To get the average:
              Initialize sum to zero before the for loop.
              You want to add res to sum each time res is calculated. So you what to do that inside the for loop after the calculation of res.
              After the loop divide by the total number of tanks gets you the average.

              Like this.

              Code:
              Edit: removed code since I realized it is a against posting guidelines. I'll send you the code in a PM sleep.
              Note: "sum += res;" does the same thing as "sum = sum + res;" it's just preference. Also I added NUM_TANKS because it's good practice to create a #define or variable for any common value appearing multiple times in you code such as the number of tanks, so if you ever need to change it you only need to go to one spot. No big deal since it's only used twice, just for good practice.

              Comment

              • Sleepwalker817
                New Member
                • Mar 2008
                • 6

                #8
                Many thanks for all the help you guys have given me.

                Comment

                Working...