Stuck Finding Hottest & Coldest Temp

Hoping someone can help me find the solution to an assignment. Done pretty well up to this point. Code compiles but the output data is incorrect and there is nothing in the output file. The assignment reads in the data from a .dat file. The data is a number, planet name, diameter, temp(in Celsius). Instructions are to read in the data, convert Celsius to Fahrenheit and output to display all information formatted and output to different file. Think I have most of it, I'm a little stuck on finding the hottest/coldest planet temps by comparing to all the temps. Here is what I have thus far. Thanks!

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; 

// Open .dat file that contains data for future display and calculations
ifstream inputFile;
inputFile.open("PlanetCelsius.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) {

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

// Not sure if whether to use if statements or a For Loop to find the coldest and warmest planets????
if(fahrenheit== 480) {
	cout<<"The Hottest Planet Is: "<<planetName<<" "<<"The Temperature is: "<<fahrenheit<<endl;
	cout<<endl;}

else if(celsius <= -200) {
	cout<<"The Coldest Planet Is: "<<planetName<<setw(25)<<"The Temperature Is: "<<celsius<<endl; }
	
	inputFile.close(); 

/* Next I will have to create the output file PlanetFahrenheit.dat and write the output from above to it. 
Will most likely have to use another WHILE LOOP to continue to write data to the file as long as it is
open.*/

ofstream outputFile;  
outputFile.open("PlanetFahrenheit.dat");

cout<<planNbr<<planetName<<diameter<<celsius<<fahrenheit;
outputFile.close();

system("Pause");
return 0;
}

You could define some extra variables, such as double minTemp and double maxTemp.
At the start, before reading any data from the file, give these suitable initial values which you know will later be replaced.
 
    double minTemp = +1000000; // give the minimum a very big value 

Then inside the loop where you have read the data for each planet, put something like this:
1
2
3
4
    if (fahrenheit < minTemp)
    {
        minTemp  = fahrenheit;
    }


Since you will later want to print the corresponding planet name, you could also define a new variable string coldName; then each time the value of minTemp is updated, store the name of the planet too.

Then later, you simply print these results, no if conditions are needed, no for loop, just print them out.

Topic archived. No new replies allowed.