Thread: Homework Help Please

  1. #1
    Registered User
    Join Date
    Sep 2005

    Homework Help Please


    A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline comsumed by the user's car and the number of miles traveled by the car, and will then output the number of miles per gallons the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the numbers of liters per gallon.

    #include <iostream>
    using namespace std;
    int main()
    	const double LIT_GAL = .264179;
    	double miles;
    	double gallons;
    	double liters;
    	char again;
    cout << "How many liters can your car hold?";
    cin >> liters;
    cout << "How many miles can your car drive without refilling?";
    cin >> miles;
    		gallons = liters * LIT_GAL;
    	cout << "Numbers of liters your car holds is " << liters << " liters.\n";
        cout << "Number of miles your car drives without refilling tank " << miles << " miles.\n";
    	cout << "Press Y for yes or any key for no,\n";		
    	cout << "then hit return.\n";						
    	cin >> again;
    	}while(again == 'y' || again == 'Y');
    	return 0;
    double mpg (double miles, double gallons)
    I know it's not done but I want to know - Do I have the right idea or doing this all wrong?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Looking good so far.
    You just need to call your function and print the answer it returns.

    And keep an eye on the formatting of the code
    No one likes to see random indentation of code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Yeah, other than neater code, and calling your function you are pretty much done.

    However, quick note to save you some points on the assignment.
    You said it says Your program should use a globally defined constant for the numbers of liters per gallon..

    How you currently have it, it is not globally defined.

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Looks good.

    You'll want a prototype directive of your function right overtop of main():

    double mpg (double miles, double gallons);
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM