I need help on how to write a while loop for this program (the assignment is posted below). I have no idea how to set it up other than what I have in the code area of this. Thanks in advance for any tips or help!
Programming Assignment:
Car MileageYou are to write a program to calculate the average miles per gallon driven over a period of time. The input file contains of an unknown number of lines of the form odometer-reading gallons-pumped.
Note that the 12.3, the gallons pumped on the very first line, is not used for anything. (You can imagine that this is the gallons pumped to fill the car tank to start.)
First, thank you so much for your response and help! Here is the upgraded code... When I compile it, it sends back this error message: mileage.cpp:29: error: invalid operands of types ‘bool’ and ‘float’ to binary ‘operator>>’
I am not really sure how to solve this issue. I am extremely new to programming, so I am not familiar with most things.
cout << endl;
cout << "Please enter a file name: ";
cin >> inFileName;
inFile.open(inFileName.c_str());
if (! inFile >> mile >> pumped);
{
cout << "Could not read first file" << endl;
return 1;
}
1) The ; terminates the if statement. The subsequent lines are executed unconditionally.
2) () are needed around the input operation:
if (! (inFile >> mile >> pumped))
Note that in the example I gave you, the first read was to the odometer variable (i.e. previous mileage reading). If you don't do that, you need to move miles to odometer before you enter the loop.