Gas prices, stumped!

This is a discussion on Gas prices, stumped! within the C Programming forums, part of the General Programming Boards category; Cant figure this one out. Ive attached it...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    Gas prices, stumped!

    Cant figure this one out. Ive attached it
    Attached Files Attached Files
    • File Type: c gas.c (905 Bytes, 76 views)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Read about the homework policy.
    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
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ahealy88 View Post
    Cant figure this one out. Ive attached it
    Can't figure this post out. I've quoted it.

    My mind-reading crystal ball is in the shop for a little polishing, so please post your detailed question(s), about the program.

    (I would add a getchar() after every scanf() to pull the newline char off the keyboard buffer.)

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Consider posting the code on the board with code tags as well. People who don't use Windows will have less trouble with newlines / carriage returns and so on. I will post it here, but there are no changes to it.

    Code:
    /* Alison Healy
       COP 3223
       Section 2
       Assignment 1 - Problem A: Gas Expenses
       1/26/2009 */
    
    #include <stdio.h>
    int main(void)
    {
        //Variables
        int mi_per_gal, size_tank_gal, mi_per_month;
        float price_per_gal, cost_per_month;
        printf("How many miles per gallon does your vehicle get?\n");
        scanf("%d", &mi_per_gal);
        printf("What is the size of your gastank in gallons?\n");
        scanf("%d", &size_tank_gal);
        printf("What is the price of gas per gallon?\n");
        scanf("%1.2f", &price_per_gal);
        printf("How many miles do you drive in a month?\n");
        scanf("%d", &mi_per_month);
        printf("\n");
        cost_per_month = (mi_per_month / mi_per_gal) * price_per_gal;
    
        //Output
        printf("The cost of gas for the month is $%.2f.\n", cost_per_month);
        system("pause");
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    I can enter input for the first 3 questions, but the last question doesnt allow me to enter anything, I think it goes wrong right around here:

    printf("How many miles do you drive in a month?\n");
    scanf("%d", &mi_per_month);
    printf("\n");
    cost_per_month = (mi_per_month / mi_per_gal) * price_per_gal;

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So %1.2f means "invalid input" in a scanf string. Presumably you mean %4f, which means "take at most four characters of input".

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You might want to consider using floats or doubles in place of int in your calculations. Results from integer division are truncated.
    Last edited by kermit; 01-25-2009 at 12:15 PM.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    It works now, but I when I run a test with:
    24 mpg
    12 gallons in a tank
    3.00 per gallon
    1000 miles a month
    I should get $125, but I get $123
    I've been working on this for days
    Code:
    #include <stdio.h>
    
    int main(void)        {
        //Variables
        int mi_per_gal,size_tank_gal,mi_per_month;
        float price_per_gal,cost_per_month;
        
        printf("How many miles per gallon does your vehicle get?\n");
        scanf("%d", &mi_per_gal);
        
        printf("What is the size of your gastank in gallons?\n");
        scanf("%d", &size_tank_gal);
        
        printf("What is the price of gas per gallon?\n");
        scanf("%4f", &price_per_gal);
        
        printf("How many miles do you drive in a month?\n");
        scanf("%d", &mi_per_month);
        
        //Calculate
        cost_per_month=(mi_per_month/mi_per_gal)*price_per_gal;
        
        //Output
        printf("The cost of gas for the month is $%.2f.\n",cost_per_month);
        
        system("pause");
             
        return 0;       }

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    when i replaced my floats with doubles (//Declare and //Output) it doesn't calculate and out puts $0.00

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1000/24 = 41, not 41.6666667. If you intend to divide integers and get a decimal answer, well, you won't. You should probably rearrange the calculation to multiply by the float first, then divide.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ahealy88 View Post
    when i replaced my floats with doubles (//Declare and //Output) it doesn't calculate and out puts $0.00
    You need to change other things to be consistent -- for instance scanf("%lf") for doubles -- scanf("%f") won't work.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    I changed everything to doubles and now it works. Thank you everyone so very much.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    You might also consider reading Scanf woes and Pause console.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  14. #14
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Similar

    Since you have solved it, can I just say it looks very similar to this, which is what I did back in 2001:

    Code:
    #include <stdio.h>
    
    /*main function - driver
    **/
    int main ( int argc, char *argv[] ) {
        int milesDriven = 0;
        float gallonsUsed = 0,
              totalMPG = 0;
        
        puts("Enter the gallons used ( enter -1 to quit):" );
        scanf("%f", &gallonsUsed);
        
        while ( gallonsUsed != -1 ) {
              puts("Enter the miles driven:");
              scanf("%d", &milesDriven);
              
              /*calculate total milage*/
              totalMPG = milesDriven / gallonsUsed;
              
              printf("Total MPG for this tankful: %.6f\n", totalMPG);
              
              puts("\nEnter the gallons used ( enter -1 to quit):");
              scanf("%f", &gallonsUsed);
        }
        
        getchar(); /*freeze console output*/
        
        return 0; /*return value from int main()*/
    }
    I'm just trying to be a better person - My Name Is Earl

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by swgh View Post
    Since you have solved it, can I just say it looks very similar to this, which is what I did back in 2001:
    which contains several minor problems like using != for float var
    not checking return value of scanf
    not validating user input
    not clearing input buffer from garbage if scanf failed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't Pump Gas on May 15th
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 05-04-2007, 04:28 PM
  2. Gas dropping?
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 06-10-2004, 06:25 AM
  3. I have a function and I need multiple averages returned
    By tommy69 in forum C++ Programming
    Replies: 20
    Last Post: 04-13-2004, 11:45 AM
  4. Gas prices
    By Maverik in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 04-24-2003, 02:56 PM
  5. Hardware prices
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-12-2001, 08:54 PM

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