How do I associate variables with eachother?

I'm working on a small program for class. It is a simple program that presents temperatures from different cities located in a txt file. Once it does that it calculates the highest temp, lowest temp, high average and low average. I've done all this however the high temp and low temp should output their respective cities. How do I display the city that produced the high and low temp at the same time as the high and low time.

i.e.
The lowest low temperature: 35 ( Gettysburg )

Below are both my text file and source code. Any help would be appreciated since it's due tomorrow morning. Thanks in advance.

1
2
3
4
5
6
7
8
Allentown 39 62
Erie 44 52
Gettysburg 35 60
Harrisburg 41 60
Pittsburg 38 56
Philadelphia 50 66
Scranton 40 60
Tannsersville 40 60


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
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>

using namespace std;


int main()
{

	ifstream Infofile;
		char City[20];
		
		int Low;
		int Lowest = 150;
		int High;
		int Highest = 0;
		int Max = 0;
		int Min = 150;		
		
		double Cities = 0;
		double Lowsum = 0;
		double Highsum = 0; 
		
	
	Infofile.open("temps.txt");

	if (!Infofile)
		{
			cout << "Could not open file temps.txt" << endl;
			system("pause");
			return 0;
		}

	
	cout << endl;
	cout << setw(14) << "City" << "       Low Temp" << "    High Temp" << endl;
	cout << setw(14) << "----" << "       --------" << "    ---------" << endl;
	cout << endl;
	
	while (Infofile >> City >> Low >> High)
	
		{
			
			cout << setw(14) << City << "  " << setw(10) << Low << "  " << setw(10) << High << endl;
			Lowsum += Low;
			Lowest = (Low < Lowest) ?Low:Lowest;
			Highsum += High;
			Highest = (High > Highest) ?High:Highest;
			Cities++;

		
		}


	double Lowavg = (Lowsum / Cities);
	double Highavg = (Highsum / Cities);
	
	cout << endl;
	
	
	cout << setw (33) << "The average low temperature : " << fixed << setprecision(1) << Lowavg << endl;
	cout << setw (33) << "The average high temperature : " <<  fixed << setprecision(1) << Highavg << endl;
	cout << setw (33) << "The lowest low temperature : " << Lowest << endl;
	cout << setw (33) << "The highest high temperature : " << Highest << endl;
	cout << endl;
	cout << endl;



system ("pause");
return 0;
}
Last edited on
You could always use an array of strings, then use stoi to convert the numerical values in the second two strings to numbers.
To be honest we have not yet learned arrays and I am not sure what you mean.
If I were to in the middle of the loop use and if statement

cout << setw(14) << City << " " << setw(10) << Low << " " << setw(10) << High << endl;
Lowsum += Low;
Lowest = (Low < Lowest) ?Low:Lowest;
if (Low == Lowest)
cout << City << endl;

My cout would be my lowest temperature's city. Is there some way I can hold that until the end of the program? I tried to

Lowcity = City, but it gives me a compiler error, apparently it's impossible to modify Lowcity because it's "char"?
Last edited on
Add new variables:
1
2
    char LowCity[20]  = "";
    char HighCity[20] = "";


Change this line:
 
    Lowest = (Low < Lowest) ?Low:Lowest;

to:
1
2
3
4
5
    if (Low < Lowest)
    {
        Lowest = Low;
        strcpy(LowCity, City);
    }


The strcpy() function is just one of many functions used to manipulate c-strings.

However, if you use C++ std::string, things are much easier (and safer) as you can just do:
1
2
string LowCity;
LowCity = City;

Topic archived. No new replies allowed.