Help with File Read In and Output to File

Hello! Been working on this assignment for about a week now and haven't been able to find what I'm doing wrong. Must have searched every C++ help tutorial as well. Current project has me read in data from file and output to screen. That works fine. The data is planet Number (integer), planet name (string), diameter (int), temp in Celsius(double). The output should calculate temp in Fahrenheit. The program works fine up to this point, however I'm also supposed to output the coldest planet and give temp in celsius. It should look something like this.

The coldest planet is:.....The temp is:......

Same thing for hottest planet. both lines appear but the output for hottest planet is incorrect. lastly, I'm supposed to output the original planet number, name,diameter, celsius and fahrenheit to a different file name. My file doesn't have any data in it. Any help is tremendously appreciated. I do my best at researching and reading to solve this but am stuck now.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;
int main() { 

// Variables to hold values 
int planNbr;
string planetName;
int diameter = 0;
double celsius = 0;
double fahrenheit = 0; 

double coldTemp = -100;
double hotTemp = 400;

// Open .dat file that contains data for future display and calculations
ifstream inputFile;
inputFile.open("PlanetCelsius.dat");
ofstream outputFile;
outputFile.open("PlanetFahrenheit.dat");

// Output information to display. The rest of the read in file will align underneath
cout<<"Number"<<setw(20)<<"Planet Name"<<setw(15)<<"Diameter"<<setw(15)<<"Celsius"<<setw(15)<<"Fahrenheit"<<endl; 	

// Test for file open error
if(inputFile.fail()) {
	cout<<"File did not open."<<endl; }

// Use while statement to ouput data from file if the file opened correctly
while(inputFile>>planNbr>>planetName>>diameter>>celsius) {

	fahrenheit = (9*celsius)/5 + 32;
	cout<<planNbr<<setw(20)<<planetName<<setw(20)<<diameter<<setw(15)<<fixed<<setprecision(2)<<celsius
		<<setw(15)<<fahrenheit<<endl; }

	if(celsius < coldTemp) {
		coldTemp = celsius;
		cout<<"The Coldest Planet Is: "<<planetName<<"\t\t"<<"The Temperature is: "<<celsius<<endl; }
	else(celsius > hotTemp);
		hotTemp = celsius; 
		cout<<"The Hottest Planet Is: "<<planetName<<"\t\t"<<"The Temperature is: "<<celsius<<endl; 

inputFile.close();
outputFile.close();


system("Pause");
return 0;
}
Hi there,

Your main issue is with curly braces:

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Use while statement to ouput data from file if the file opened correctly
while(inputFile>>planNbr>>planetName>>diameter>>celsius) 
{

	fahrenheit = (9*celsius)/5 + 32;
	cout<<planNbr<<setw(20)<<planetName<<setw(20)<<diameter<<setw(15)<<fixed<<setprecision(2)<<celsius
		<<setw(15)<<fahrenheit<<endl; }

	if(celsius < coldTemp) 
       {
		coldTemp = celsius;
		cout<<"The Coldest Planet Is: "<<planetName<<"\t\t"<<"The Temperature is: "<<celsius<<endl; 
        }
	else(celsius > hotTemp) //you had an extra semicolon here, which causes trouble
        {
		hotTemp = celsius; 
		cout<<"The Hottest Planet Is: "<<planetName<<"\t\t"<<"The Temperature is: "<<celsius<<endl;
         }
}


I don't see any statements writing to outputFile. It works pretty much the same as getting input from a file, only you use the insertion operator (<<) in stead of the extraction operator (>>):

outputFile << "My data: " << some_variable;

One more little style-note:

1
2
double coldTemp = -100;
double hotTemp = 400;


You should consider making these const. Constants can be hardcoded by the compiler which means they don't need to be fetched from memory every time:

1
2
const double coldTemp = -100;
const double hotTemp = 400;


Hope that gets you on your way. Please do let us know if you require any further help.

All the best,
NwN
Thanks for the help. The program now writes data to the new file but my calculations are wrong. The same planet and temp are given for cold and hottest planets. Using the const didn't work.

After I've read in all the data from the inputFile how do I write the if...else statement to compare all the temps then output to display the coldest planet name and it's temperature and the hottest temp? Each line of data is on a separate line in the .dat file. Here is my latest code. Thanks for the guidance and help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;
int main() { 

// Variables to hold values 
int planNbr;
string planetName;
int diameter = 0;
double celsius = 0;
double fahrenheit = 0; 

double coldTemp = -100;
double hotTemp = 400;
// Open .dat file that contains data for future display and calculations
ifstream inputFile;
inputFile.open("PlanetCelsius.dat");
ofstream outputFile;
outputFile.open("Planet Fahrenheit.dat");

// Output information to display. The rest of the read in file will align underneath
cout<<"Number"<<setw(20)<<"Planet Name"<<setw(15)<<"Diameter"<<setw(15)<<"Celsius"<<setw(15)<<"Fahrenheit"<<endl; 	

// Test for file open error
if(inputFile.fail()) {
	cout<<"File did not open."<<endl; }

// Use while statement to ouput data from file if the file opened correctly
while(inputFile>>planNbr>>planetName>>diameter>>celsius) {

	fahrenheit = (9*celsius)/5 + 32;
	cout<<planNbr<<setw(20)<<planetName<<setw(20)<<diameter<<setw(15)<<fixed<<setprecision(2)<<celsius
		<<setw(15)<<fahrenheit<<endl; 
//Output to new file "Fahrenheit.dat"
	outputFile<<planNbr<<endl;
	outputFile<<planetName<<endl;
	outputFile<<diameter<<endl;
	outputFile<<celsius<<endl;
	outputFile<<fahrenheit<<endl; }

	if(celsius < coldTemp) {
		coldTemp = celsius;
		cout<<"The Coldest Planet Is: "<<planetName<<"\t\t"<<"The Temperature is: "<<celsius<<endl; }

	else(celsius > hotTemp); 
		hotTemp = celsius; 
		cout<<"The Hottest Planet Is: "<<planetName<<"\t\t"<<"The Temperature is: "<<celsius<<endl; 
		

inputFile.close(); 
outputFile.close();

system("Pause");
return 0;
}
The reason they are both the same is the ; at the end of this line
else(celsius > hotTemp); // <----- ; should be removed!
As for how to get the correct answer, you need to check the hottest and coldest inside the while loop. The quick and dirty way would be to add two new strings string hotName,coldName; to hold the names as you identify them. Then when you exit the while loop you can print out the answer with the correct informantion.
1
2
cout<<"The Coldest Planet Is: "<<coldName<<"\t\t"<<"The Temperature is: "<<coldTemp<<endl;
cout<<"The Hottest Planet Is: "<<hotName<<"\t\t"<<"The Temperature is: "<<hotTemp<<endl;
Last edited on
Topic archived. No new replies allowed.