Comparing Temperatures

From a file of a temperatures I trying to compare different temperatures. Unfortunately I'm getting multiple outputs when I only want one. I just need either higher, lower, or same to output. Code is outputting samehigher or lowerhigher.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  #include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
istream& readTemperatures(istream&, int&, int&, int&, int&, int&);
ostream& printHeadings(ostream&);
ostream& printTemperatures(ostream&, int, int, int, int, int);
string lowSameHigh(int, int);
//*********************************************************************
int main()
{
	string fileName;
	int month,
		day,
		year,
		high,
		low;
	cout << "Name of input file? ";
	cin >> fileName;
	ifstream fin(fileName);
	if (!fin)
	{
		perror(fileName.data());
		exit(1);
	}
	system("cls");
	printHeadings(cout);
	while (readTemperatures(fin, month, day, year, high, low))
	{
		printTemperatures(cout, month, day, year, high, low);
	}
	return 0;
}
//********************************************************************
// return mm, dd, yy, high, and low as reference parameters
// input stream as return value 
istream& readTemperatures(istream& inFile, int& mm, int& dd, int& yy, int& high,
	int& low)
{
	inFile >> mm >> dd >> yy >> high >> low;
	return inFile;
}
//********************************************************************
// print headings to outFile, after clearing the screen
ostream& printHeadings(ostream& outFile)
{
	outFile << fixed << left << "Date" << right 
			<< setw(20) << "Daily High" << right
			<< setw(20) << "Compared to Prior" << right << setw(20)
			<< "Daily Low" << right << setw(20) << "Compared to Prior" << endl;
	return outFile;
}
//********************************************************************
// print output line and keeps track of priorHigh, priorLow, and lineCount
// when lineCount hits 21 resets lineCount to zero.
ostream& printTemperatures(ostream& outFile, int mm, int dd, int yy,
	int high, int low)
{
	static int	priorHigh = 0,
		priorLow = 0,
		lineCount = 1;

	outFile << fixed << setprecision(0) << left << mm << "/";

	if (dd < 10)
	{
		cout << "0";
	} 

	outFile << dd << "/" << yy << right << setw(15) << high << setw(20); 
	
		lowSameHigh(high, priorHigh);
		priorHigh = high;

	outFile << setw(20) << low << setw(20);
	
		lowSameHigh(low, priorLow);
		priorLow = low;
	
	outFile << endl;

	if (lineCount < 21)
	{
		++lineCount;
	}
	else
	{
		system("pause");
		system("cls");
		lineCount = 0;
		printHeadings(cout);
	}
	return outFile;
}
//********************************************************************
// returns lower, same, or higher depending on wether value is less,
// equal to, or greater than previous value
string lowSameHigh(int a, int b)
{
	string status;
	if (a < b)
	{
		cout << "lower";
	}
	else if (a = b)
	{
		cout << "same";
	}
	else (a > b);
	{
		cout << "higher";
	}
	return status;
}
else if (a = b) = is not the same as ==

else (a > b);
This is just wrong. There's no if here, so what's that (a > b) condition for? Also, see that semi-colon? That marks the END of the else block. So line 111 onwards isn't part of the else block, so line 111 onwards is just normal code that always gets run.

Line 110 should be this:
else
Yeah that is what I thought but when I was coding in Visual Studio
it kept giving an error saying that it needed a semicolon.
Thanks Moschops,

was missing the other = sign.

For some reason Visual Studio did not like the else statement. Changed it to an else if statement and it worked the way I wanted it to.
The last condition doesn't need an else if because if neither of the first two conditions are satisfied, the third one must be true.
1
2
3
4
5
6
7
8
9
10
11
12
    if (a < b)
    {
        cout << "lower";
    }
    else if (a == b)
    {
        cout << "same";
    }
    else
    {
        cout << "higher";
    }


Conversely, if you really do need an else if (in this case you don't), good practice would indicate that there should be another final else to pick up whatever is left.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    if (a < b)
    {
        cout << "lower";
    }
    else if (a == b)
    {
        cout << "same";
    }
    else if (a > b)
    {
        cout << "higher";
    }
    else
    {
        // what goes here?
    }


Last edited on
Topic archived. No new replies allowed.