/*
Matthew Giampiccolo
&
Jason M. Snouffer
Computer Science & Programming
Wednesday, October 23, 2002
Lab 8 Problem 1
This program recieves liters of gas consumed by the user's car and the number of
miles traveled by the car. It then converts the inputted liters to gallons and
calculates the miles per gallon (MPG) used by the car.
Test Suite:
Input (liters used): 1
Input (miles travelled): 10
Expected Output: Your car's gas mileage was 37.8531 miles per gallon.
Input (liters used): 25
Input (miles travelled): 300
Expected Output: Your car's gas mileage was 45.4237 miles per gallon.
Input (liters used): 300
Input (miles travelled): 25
Expected Output: Your car's gas mileage was 0.315443 miles per gallon.
Input (liters used): 13
Input (miles travelled): 13
Expected Output: Your car's gas mileage was 37.8531 miles per gallon.
Input (liters used): 1
Input (miles travelled): 10
Expected Output: Your car's gas mileage was 3.78531 miles per gallon.
Input (liters used): 25
Input (miles travelled): 67
Expected Output: Your car's gas mileage was 10.1446 miles per gallon.
All of the sample inputs within this test suite were tested and performed as expected.
*/
#include
#include
using namespace std;
const double LITERS_PER_GALLON(0.264179);
/*
---------------------------------------get positive integer--------------------------------------------
this function gets a value from the user and repeatedly asks for a positive number if one is not given
pre: none
post: values read from the user whenever the user enters a non-positive value, they will recive a message
to enter a positive value.
*/
int get_pos_int ();
/*--------------------------liters_to_gallons----------------------------------
this function converts from liters to gallons
pre: number of liters used
post: the number outputted will be the conversion of liters used to gallons used
*/
double liters_to_gallons (int liters_used);
/*--------------------------------Miles per gallon-------------------------------------
this function computes the miles per gallon used by the car on the trip
pre: conversion from litres to gallons
post: the output will be the miles the car went per one gallon of gas
*/
double miles_per_gallon (double gallons_used, int miles_travelled);
/* this program computes the miles per gallon of the trip*/
int main ()
{
int litres_used;
cout<<"Please enter the liters used on the trip: ";
litres_used = get_pos_int();
int miles_traveled;
cout<<"Please enter the number of miles traveled: ";
miles_traveled= get_pos_int();
double conversion;
conversion = liters_to_gallons(litres_used);
double mpg;
mpg= miles_per_gallon (conversion, miles_traveled);
cout<<"Your car's gas mileage was "
<> num;
while (num<=0)
{
cout<<"Please enter a positive number: ";
cin>> num;
}
return num;
}
/*---------------------------litres to gallon conversion------------------------------------
this function converts from liters to gallons
pre: number of liters used
post the number outputted will be the converson of liters used to gallons used
*/
double liters_to_gallons (int liters_used)
{
double gallons_used;
gallons_used = liters_used * LITERS_PER_GALLON;
return gallons_used;
}
/*
-----------------------------miles per gallon-------------------------------------------------
this function computes the miles per gallon used by the car on the trip
pre: conversin from litres to gallons
post: the output will be the miles the car went per one gallon of gas
*/
double miles_per_gallon (double gallons_used, int miles_traveled)
{
double mpg;
mpg= miles_traveled / gallons_used;
return mpg;
}