![]() |
| | #1 |
| Registered User Join Date: Aug 2003
Posts: 71
| Small program that has to calculate miles per gallon I am developing a program that should calculate the miles per gallon obtained from tankful. After processing all the input data, the program should calculate the combined miles per gallon obtained for all tankfuls. So, this is what the output should look like - Enter the gallons user( -1 to end): 12.8 Enter the miles driven: 287 The miles / gallon for this tank was 22.421875 Enter the gallons user( -1 to end): 10.3 Enter the miles driven: 200 The miles / gallon for this tank was 19.417475 Enter the gallons user( -1 to end): -1 The overall average miles/gallon was 21.601423 This is the code Iv'e done so far. It doesn't give the desired result, as it, does like an endless loop after Iv'e entered the gallons and miles. Can anyone give me some advice and/or tips please? Code: #pragma argsused
#pragma hdrstop
#include<iomanip>
#include<iostream>
#include<conio>
using namespace std;
int main(int argc, char* argv[])
{
// declare the variables
double gallons; // read in the gallons
double miles; // read in the miles
double totalGallons; // calculate the total gallon input
double totalMiles; // calculate the total mile input
double total; // calculate the gallons/miles
double average; // calculate the average overall
double counter;
// initialization phase
total = 0; // initialise total
counter = 1; // initialise loop counter
cout << "Enter the gallons used( -1 to end): " << endl;
cin >> gallons;
cout << "Enter the miles driven: " << endl;
cin >> miles;
average = miles / gallons; // calculate the the current input
cout << average; // display average
// loop until sentinal value read from user
while ( gallons != -1 ) {
total = totalMiles + totalGallons;//add total of gallons to total of miles
counter = counter + 1; // increment the counter
// prompt for input and read the next gallons
cout << "Enter the gallons user( -1 to end): " << endl;
cout << "Enter the miles driven: " << endl;
}
// termination phase
// if user entered at least one gallon...
if ( counter != 0 ) {
// calculate average of all gallons over miles entered
average = static_cast< double >( total ) / counter;
// display overall average with six digits of precision
cout << "Overall average is " << setprecision( 6 )
<< fixed << average << endl;
}
else
cout << "No gallons or miles were entered" << endl;
getch();
return 0;
}
|
| Guti14 is offline | |
| | #2 |
| The Defective GRAPE Join Date: Feb 2003
Posts: 949
| Code: while ( gallons != -1 ) {
total = totalMiles + totalGallons;//add total of gallons to total of miles
counter = counter + 1; // increment the counter
// prompt for input and read the next gallons
cout << "Enter the gallons user( -1 to end): " << endl;
cout << "Enter the miles driven: " << endl;
}
__________________ Do not make direct eye contact with me. |
| Lurker is offline | |
| | #3 |
| Been here, done that. Join Date: May 2003
Posts: 1,036
| Code: total = totalMiles + totalGallons;//add total of gallons to total of miles
__________________ There are only 10 types of people in the world -- those that use binary, and those that don't |
| WaltP is offline | |
| | #4 |
| Registered User Join Date: Jan 2004 Location: In front of a monitor
Posts: 48
| i looked through it, and changed it a little, and this is what i got. Code: #pragma argsused
#pragma hdrstop
#include<iomanip>
#include<iostream>
#include<conio.h>
using namespace std;
int main(int argc, char* argv[])
{
// declare the variables
double gallons; // read in the gallons
double miles; // read in the miles
double gallons2;
double miles2;
double average3;
double average4;
double average; // calculate the average overall
double average2;
cout << "Enter the gallons used( 0 to end): " << endl;
cin >> gallons;
cout << "Enter the miles driven: " << endl;
cin >> miles;
average = miles / gallons; // calculate the the current input
cout << "your average for the trip was " << average << endl; // display average
// loop until sentinal value read from user
if ( gallons != 0 )
{
// prompt for input and read the next gallons
cout << "Enter the gallons used( 0 to end): " << endl;
cin >> gallons2;
cout << "Enter the miles driven: " << endl;
cin >> miles2;
average2 = miles2 / gallons2;
cout << "your average for trip 2 was " << average2 << endl;
}
else
{
cout << "No gallons or miles were entered" << endl;
return 0;
}
// termination phase
// if user entered at least one gallon...
if ( gallons2 != 0 )
{
average3 = average + average2; // to get the average of the 2
average4 = average3 / 2; // to get the average of the 2
// display overall average with six digits of precision
cout << "Overall average is " << average4;
cin.get();
}
else
{
cout << "No gallons or miles were entered" << endl;
return 0;
}
}
it worked for me, but it didnt stay up to give me the over all average. maybe one of you can see what was wrong. (note: sorry if its redundant/ not clean, still a noob) Last edited by Aalmaron; 01-06-2004 at 12:36 AM. |
| Aalmaron is offline | |
| | #5 |
| Been here, done that. Join Date: May 2003
Posts: 1,036
| Where's your loop? It would help if you fix your format so the indentation is consistant. And if you're going to apologize for not commenting, comment and save the apology
__________________ There are only 10 types of people in the world -- those that use binary, and those that don't |
| WaltP is offline | |
| | #6 |
| Tha 1 Sick RAT Join Date: Dec 2003
Posts: 267
| Plain and simply- as pointed out by Lurk & Aalmaron- you forgot to read in the values for the variables in your loop i.e. cin>>gallons; and the other. Always make sure you can reach the exit conditon you set for a loop.
__________________ A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside |
| WDT is offline | |
| | #7 |
| Registered User Join Date: Jan 2004 Location: In front of a monitor
Posts: 48
| oh hey, if you wanted to loop it, among other things that you'd need to change, you shouldnt have the paramiters be the gas. Code: int counter = 1
while ( counter = 1)
{
yadda yadda yadda
counter = counter + 1;
}
while ( counter = 2)
{
yadda yadda yadda
counter = counter + 1;
}
Code: counter = 1 Last edited by Aalmaron; 01-06-2004 at 02:50 PM. |
| Aalmaron is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Program to Calculate Molar Mass | ebrayer | C Programming | 2 | 08-02-2008 03:20 PM |
| guidence to a program C | nurofen | C Programming | 17 | 04-14-2008 11:50 PM |
| Right Triangle Program | BSmith4740 | C# Programming | 9 | 02-27-2008 12:24 AM |
| fopen(); | GanglyLamb | C Programming | 8 | 11-03-2002 12:39 PM |
| A small problem with a small program | Wetling | C Programming | 7 | 03-25-2002 09:45 PM |