Need help on this while statement, thanks!

This is a discussion on Need help on this while statement, thanks! within the C Programming forums, part of the General Programming Boards category; I'm wondering if something is wrong with this code. the supposed output should be: Code: Enter gallons consumed(-1 to end): ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22

    Wink Need help on this while statement, thanks!

    I'm wondering if something is wrong with this code.

    the supposed output should be:

    Code:
    Enter gallons consumed(-1 to end): -1
    //skips the miles prompt and ends the loop
    ...some code after loop...
    but it still asks for the miles value to be entered and then ends the loop. what's wrong with this? please help.

    here's the code:

    Code:
    while (gallon != -1)
        {
              printf("Enter gallons consumed(-1 to end): ");
              scanf("%f", &gallon);
              
              printf("Enter miles: ");
              scanf("%f", &miles);
              
              tmil += miles;
              tgal += gallon;
              
              mpg = miles/gallon;
              
              printf("Your fuel efficiency is %.2f\n\n", mpg);
              }
    Thanks,

    Jann

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Notice that after you read -1 to gallon, you don't check for it. Basically, you need to fix the logic.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    awts! that's it! thanks a lot laserlight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 02:18 PM
  2. what does this statement do?
    By timhxf in forum C Programming
    Replies: 8
    Last Post: 04-29-2007, 02:51 AM
  3. help with an if statement
    By jjj93421 in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2004, 09:28 AM
  4. if statement
    By Joe100 in forum Game Programming
    Replies: 1
    Last Post: 12-07-2003, 04:57 AM
  5. Help with if statement
    By viryonia in forum C Programming
    Replies: 1
    Last Post: 02-11-2003, 08:52 PM

Tags for this Thread


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