-
while loop question
hello there all, i am just beginning to learn the C++ language and i am having trouble with this excercise from the book that i am doing:
Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
int totalMile, totalGallon, gallonCounter, aveGallon, aveMile;
double milePerGallon, average, mile, gallon, mileGal;
totalMile = 0;
totalGallon = 0;
gallonCounter = 0;
cout << "Enter the gallons used, (-1 to end) : " << endl;
cin >> gallon;
cout << "Enter the miles driven: " << endl;
cin >> mile;
mileGal = mile / gallon;
cout << "The miles / gallon for this tank was: " << setprecision( 2 )
<< setiosflags( ios::fixed | ios::showpoint )
<< mileGal << endl;
while ( gallon != -1)
{
totalGallon = totalGallon + gallon;
totalMile = totalMile + mile;
gallonCounter = gallonCounter + 1;
cout << "Enter the gallons used, (-1 to end) : " << endl;
cin >> gallon;
cout << "Enter the miles driven: " << endl;
cin >> mile;
mileGal = mile / gallon;
cout << "The miles / gallon for this tank was: " << setprecision( 2 )
<< setiosflags( ios::fixed | ios::showpoint )
<< mileGal << endl;
}
if ( gallonCounter != 0 )
{
aveMile = totalMile/gallonCounter;
aveGallon = totalGallon/gallonCounter;
average = static_cast< double >( aveMile )/static_cast< double >( aveGallon );
cout << "The overall average miles/gallon was: " << setprecision( 2 )
<< setiosflags( ios::fixed | ios::showpoint )
<< average << endl;
}
else
{
cout << "No gallons entered." << endl;
}
return 0;
}
the code is executing but once i want to end the entering of data through -1 it adds the -1 to the computation.
i just want to know what is wrong with my code.
thank you very much.
-
The basic principle is...store the input to "gallon"; check that "gallon" not equal -1
Code:
cout << "Enter the gallons used, (-1 to end) : " << endl;
cin >> gallon;
while ( gallon != -1) {
// do processing
// ...
cout << "Enter the gallons used, (-1 to end) : " << endl;
cin >> gallon;
}
-
pardon me sir but could you elaborate it coz i am just a newbee in C++, will i reorder my code? or coz i also have that mile input, the excercise in the book wants an output like this:
Code:
Enter the gallon used ( -1 to end ): 12.8
Enter the mile driven: 287
The miles/gallon for this tank was 22.42
Enter the gallon used ( -1 to end ): 10.3
Enter the mile driven: 200
The miles/gallon for this tank was 19.41
Enter the gallon used ( -1 to end ): 5
Enter the mile driven: 120
The miles/gallon for this tank was 24.00
Enter the gallon used ( -1 to end ): -1
The overall average miles/gallon was 21.60
thanks a lot for the reply.
-
When you read a value, test it straight away. For example;
Code:
cin >> gallon;
if (gallon != -1)
{
// do whatever you want with gallon
}
In addition, I also suggest checking that gallon is non-zero; dividing by zero is rarely a good idea ....
-
You also need to get into the habit of checking your streams for errors. For instance, if someone fails to enter a value that is convertable to a float, the stream goes into an error state and any subsequent reads will fail. I find it best to read in all user data (and this includes data from file, socket, etc.) into char arrays (I prefer strings when in C++), then do the processing on the string. The input can still fail (the stream could be broken, for instance), but you have much better control over what is going on and I feel it results in better to maintain code.
-
thank you very much all.
it helps me alot.