Thread: c program help

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    c program help

    Hi, I need some help with a program that determines 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:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
     int km, i;
     float res, lt;
    
     clrscr();
    
     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", <);
      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.");
    }
    As it is now, I get the error message: parse error before '<'.

    Thanks in advance.

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Code:
      scanf("&#37;f", <);
    < is not a valid identifier name. Maybe try &lt.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by xuftugulus View Post
    Code:
      scanf("%f", <);
    < is not a valid identifier name. Maybe try &lt.
    That's probably a HTML translation problem, I would think. (&lt in HTML becomes '<')

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Research on the pitfalls of using scanf for user input.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Firstly to make your new lines display correctly use \n instead of /n.

    To get your final calculation add a variable to hold sum of your results. When you exit the loop then divide the sum by then number of tanks of fuel. That will give you your average.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Thanks to all of you for your help so far. This is my revised version of the program. I'm still not too sure about how to get the loop average so continued input is still appreciated.

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
     int km, i;
     float res, lt, sum, avg;
     sum = 0;
     avg = sum/4;
    
     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 &#37;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 = sum + res;
     }
     printf("Your overall average kilometers per liter for 4 tanks is %.1f", avg);
     printf("\n\nThanks for using the Sears KPL calculator program.");
     getchar();
    }

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Just calculate average after you finish you loop; not before it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM