Thread: whileloop/running total problem

  1. #1
    Registered User
    Join Date
    Jan 2012

    whileloop/running total problem

    hi i have been asked to input 2 integers, a drivers MILES and LITRES of fuel they have used.. i am then meant to printf the miles per litre for that specific entry AND a running total of the combined MPL's added so far...

    #include <stdio.h>
    int main( )
    int miles;
    int litres;
    int tanks;
    int mpl;
    int totalmpl;
    int counter;
    while ( miles > 0, counter++ ) {
            printf("please enter the amount of miles you have driven, alternatavely, press 0 to finish the script.\n");
            scanf("%d", &miles);
            printf("please enter the amount of litres you used\n");
            scanf("%d", &litres);
            printf("the number of miles per litre you have gained is:%d\n", mpl);
            while (counter > 1)
                    printf("the total running miles per litre is:%d\n", totalmpl);
       return 0;
    this outputs nothing, if anyone can help i would be delighted. THANKS

  2. #2
    Registered User
    Join Date
    May 2009
    FYI: You code has no running total in it.

    With the idea that you want a running total.

    Try this instead

    Edit: Your code makes little sense to me.

    while (counter > 1)
    Is there any reason to believe the value of counter is changing inside the loop?
    I say no, therefor the loop will never run or be endless!

    Tim S.
    Last edited by stahta01; 01-22-2012 at 09:08 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    while ( miles > 0, counter++ )
    Since miles is uninitialized at this point, it is just as likely to be negative as positive. You need to initialize it to 1 or something first. The same is even more true for counter, altho I actually don't see counter as serving any useful purpose here. Just add mpl to total (total should be initialized to 0 -- that's three uninitialized variables that need to be, tch) and get rid of counter.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-12-2011, 10:17 AM
  2. Total noob problem. QUICK!
    By DCN in forum C Programming
    Replies: 3
    Last Post: 09-21-2010, 10:31 AM
  3. Replies: 11
    Last Post: 12-31-2007, 02:32 PM
  4. total noob with a problem
    By dwainpipe in forum C++ Programming
    Replies: 5
    Last Post: 03-07-2006, 07:48 AM
  5. ff7 running problem...
    By doubleanti in forum A Brief History of
    Replies: 24
    Last Post: 11-22-2001, 08:02 PM