Thread: help with float

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    10

    help with float

    Howdy. I need a little help with float.

    Code:
    /*******************************************************************************
    * Name: Trip.c
    * Assignment: LAB 2
    * Date Written: 10/11/2007
    * Course: CS133
    * Purpose: Calculate usefull info for a car trip
    * Sources:
    *******************************************************************************/
    #include <stdio.h>
    
    main()
    {
          printf("Welcome to the Trip Planner! So you are ready to take a trip?"
          "Let me help you plan for your fuels costs."
    "==============================================================================="
    "Please provide answers to the prompts below and I will"
    "display a summary for you when I have computed the results."
    "===============================================================================\n")
    
    float MPG, cost, miles, gallons, Tcost; 
    
    printf("Please input your car's average miles per gallon >> ");
    scanf("&#37;f", &MPG);
    printf("Please tell me the going rate for fuel (per gallon) >> $ ");
    scanf("%f", &cost);
    printf("Please tell me how many miles you plan to travel >> ");
    scanf("%f", &miles);
    
    gallons= miles / MPG;
    Tcost= gallons * cost;
    
    printf("You will need to purchase" %f\n, gallons "gallons of fuel");
    printf("The approximate cost of fuel for your trip is: %f\n", Tcost);
    
    printf("Thank you, please drive safely and have a nice trip!");
    
    return 0;
    }
    Thats the main chuck of the source code. According to the compiler, none of my objects listed under fload are declared. Just not really sure what to do to fix all this. I'm very new at all of this!

    Thanks in advanced,
    Kevin
    Last edited by thor4life; 10-11-2007 at 09:25 PM. Reason: added whole code

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    gallons= miles / MPG;
    Tcost= gallons * cost;
    what is the whole code?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Hai friend you had given MPH in the place of MPG in the scanf so it will give the compile error...

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Quote Originally Posted by robwhit View Post
    Code:
    gallons= miles / MPG;
    Tcost= gallons * cost;
    what is the whole code?
    That was the only chuck yelling at me. But I just put in the whole code.

    Lol, good catch sreeramu. Thanks. But with those 2; and that correction, I still get the exact same errors. If you want, I can post the errors that it's giving me.

    And FYI, I know that the line: printf("You will need to purchase" &#37;f\n, gallons "gallons of fuel");
    is way wrong. I'm trying to have the program input the amout of gallons you need to buy in the middle of the printf, and I have no idea. But thats a (somewhat) different question.
    Last edited by thor4life; 10-11-2007 at 09:36 PM.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by thor4life View Post

    Code:
    /
    main()
    {
          printf("........")   /* <---missing semicolon */
    
    float MPG, cost, miles, gallons, Tcost;
    T..what to do...
    Some compilers require that all data declarations in a block must appear before executable statements.


    All compilers require semicolons at the end of statements.

    (Why not keep the printf stuff short and simple until you verify the functionality? Dress it up later. Just a thought.)


    D.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    10
    Oh, trust me, I'd like to, but this is for an online class, and the teacher is out for a couple days.

    The missing ; after that 1st long printf solved the float issue. Now I have to figure out how to make:
    Code:
     printf("You will need to purchase" &#37;f\n, gallons "gallons of fuel");
    work. Any ideas?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
       printf("You will need to purchase %f gallons of fuel\n" , gallons);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("Welcome to the Trip Planner! So you are ready to take a trip?"
        "Let me help you plan for your fuels costs."
        "==============================================================================="
        "Please provide answers to the prompts below and I will"
        "display a summary for you when I have computed the results."
        "===============================================================================\n")
        
        {
            float MPG, cost, miles, gallons, Tcost; 
            
            printf("Please input your car's average miles per gallon >> ");
            scanf("&#37;f", &MPG);
            printf("Please tell me the going rate for fuel (per gallon) >> $ ");
            scanf("%f", &cost);
            printf("Please tell me how many miles you plan to travel >> ");
            scanf("%f", &miles);
            
            gallons= miles / MPG;
            Tcost= gallons * cost;
            
            printf("You will need to purchase %f gallons of fuel\n", gallons);
            printf("The approximate cost of fuel for your trip is: %f\n", Tcost);
            
            printf("Thank you, please drive safely and have a nice trip!");
        }
    
        
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM