Thread: miles per gallon program

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    2

    miles per gallon program

    This program is just supposed to ask the user 3 questions and get a calculation of the miles per gallon over and over until the user types a negative number. I have 2 problems currently, first the miles per gallon comes out to be some odd number (ex. 15000 initial miles, 15250 final miles, 10 gallons, should be 25 miles per gallon but it comes out to be 1254792).

    Second its supposed to ask all 3 questions again after it calculates the miles per gallon...but instead it just start off asking the 2nd question after it gives the weird calculation. Any idea what I'm doing wrong?


    Code:
    #include <stdio.h>
    
    int main()
    {
      int   input_value, final, gallons, milespergallon;
    
      printf("What is your initial odometer reading:\n");
      scanf("%d", &input_value);
      
      while ( input_value > 0 )
      {
    printf("What is your final odometer reading:\n");
        scanf("%d", &final);
    
        printf("What is the number of gallons of gas:\n");
        scanf("%d", &gallons);
    milespergallon = ( final - input_value ) / gallons;
    
        printf("Miles per gallon: %d, &milespergallon");
      }
      return 0;
    }



    -James

    [edit]Code tags added by Hammer.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The fault was in this line:
    printf("Miles per gallon: %d, &milespergallon");
    Also your quit logic was a little off.

    This should do the job.
    Code:
    #include <stdio.h>
    
    int main()
    {
       int input_value, final, gallons, milespergallon;
    
       printf("What is your initial odometer reading:\n");
       scanf("%d", &input_value);
    
       while ( input_value > 0 )
       {
          printf("What is your final odometer reading:\n");
          scanf("%d", &final);
    
          printf("What is the number of gallons of gas:\n");
          scanf("%d", &gallons);
          milespergallon = ( final - input_value ) / gallons;
    
          printf("Miles per gallon: %d", milespergallon);
    
          printf("\n\nWhat is your initial odometer reading:\n");
          scanf("%d", &input_value);
       }
       return 0;
    }
    EDIT -
    [edit]Beat by Lightening boy
    That's cuz you're too busy editting code tags. You and your superpowers
    Last edited by Cshot; 09-27-2002 at 01:18 PM.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>printf("Miles per gallon: %d, &milespergallon");
    This is wrong. Try this instead:

    >>printf("Miles per gallon: %d\n", milespergallon);

    [edit]Beat by Lightening boy
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    2
    thanks, it worked perfectly

  5. #5
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167

    I probably would have done it like this!

    I personally would have done it like this.
    Code:
    #include <stdio.h>
    
    int main()
    {
       int input_value, final, gallons, milespergallon;
    
       do{
                printf("What is your initial odometer reading:\n");
                scanf("%d", &input_value);
                printf("What is your final odometer reading:\n");
                scanf("%d", &final);
    
                printf("What is the number of gallons of gas:\n");
                scanf("%d", &gallons);
                milespergallon = ( final - input_value ) / gallons;
    
                printf("Miles per gallon: %d", milespergallon);
          }while ( input_value > 0 );
          return 0;
    }
    [colour=blue]You probably forgot about this a long time ago, but I just found this thread!![/colour]:-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Can't figure out for the life of me what I am doing wrong.
    By paulntysmom in forum C Programming
    Replies: 7
    Last Post: 03-14-2006, 08:26 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Need help please :) Newbie trying to learn
    By paulntysmom in forum C Programming
    Replies: 11
    Last Post: 03-07-2006, 01:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM