Thread: Homework Help Please

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    Homework Help Please

    Question:

    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.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const double LIT_GAL = .264179;
    
    	double miles;
    	double gallons;
    	double liters;
    
    	char again;
    	
    	do
    	{
    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)
    {
    	return(miles/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
    Location
    The edge of the known universe
    Posts
    39,659
    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
    Location
    Earth
    Posts
    282
    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
    Location
    Canada
    Posts
    1,034
    Looks good.

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

    Code:
    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 Cprogramming.com
    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