Reading from file one int a time..

I want to read this file to read each temperature and have it tell me if one of the substances listed will freeze or boil. My code is only reading the last temperature. And I am also having trouble understanding what the question wants me program exactly. I have the question to this program commented out within the code and not sure if I'm doing mrs than asked. Im self-learning and aging trouble. If some one can help explain and solve my issue, it'll be very much appreciated.

-------------------------------------------------------------------------------
/*
Modify the freezing and boiling points program described in Programming Challenge 20 so it reads its input from a file instead of from the keyboard. Perform the necessary test to deter- mine if an error occurs when the file is opened. If an error occurs, display a message informing the user. The following data to test your program can be found in the FrzBoil.dat file.
-173 -38 -362 32
172 676 -306 212
*/

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ofstream outputFile;
ifstream inFile;

double temperature, a, b, c, d;

outputFile.open("FrzBoil.txt");

outputFile << "-173 " << "-38 " << "-362 " << "32" << endl;
outputFile << "172 " << "676 " << "-306 " << "212" << endl;

outputFile.close();

inFile.open("FrzBoil.txt");

if (inFile.fail())
{
cout << "Error Opening file." << endl;
}

while (!inFile.eof())
{
inFile >> temperature;

if (temperature <= -362)
{
cout << "Ethyl Alcohol, Mercury, Oxygen and Water all freeze at " << temperature << " degrees." << endl;
exit(0);
}
else if (temperature <= -173 && temperature > -362)
{
cout << "Ethyl Alcohol, Mercury and Water all freeze at " << temperature << " degrees." << endl;
cout << "Oxygen will boil at " << temperature << " degrees." << endl;
exit(0);
}
else if (temperature <= -38 && temperature > -173)
{
cout << "Mercury and Water will freeze at " << temperature << " degrees." << endl;
cout << "Oxygen will boil at " << temperature << " degrees." << endl;
exit(0);
}
else if (temperature <=32 && temperature > -38)
{
cout << "Water will freeze at " << temperature << " degrees." << endl;
exit(0);
}

if (temperature >= 172 && temperature < 212)
{
cout << "Ethyl Alcohol and Oxygen boil at " << temperature << " degrees." << endl;
exit(0);
}
else if (temperature >= 212 && temperature < 676)
{
cout << "Ethyl Alcohol, Oxygen and Water boil at " << temperature << " degrees." << endl;
exit(0);
}
else if (temperature >= 676)
{
cout << "Ethyl Alcool, Oxygen, Water, and Mercury boil at " << temperature << " degrees." << endl;
exit(0);
}
}



return 0;
}
Last edited on
Due to the exit(0); you read the first value only. So remove the exit(0);

By the way: change
1
2
3
4
while (!inFile.eof())
{
inFile >> temperature; // This way you read eof
// Here you don't check whether you read eof -> you show the last value twice 
to
1
2
while (inFile >> temperature)
{

Makes plenty of sense. Thank You!
Topic archived. No new replies allowed.