Reading temperatures from file (Solved)

Hi guys. I am making a program that reads in data from a file that contains temperatures for City 1 and City 2. I need to use a prototype function to calculate the average, highest, and lowest temperatures for both cities. Here is the file I need to read from. (temperature1.txt)
------------------------------
City 1
-20
City 2
-20
City 1
100
City 2
100
City 1
50
City 2
50
--------------------------------

Here is how the output is supposed to look.
Enter the input filename: temperature1.txt
**************************************************
City 1:
3 temperature1 are recorded.
The average temperature is 43.33
The highest temperature is 100.00
The lowest temperature is -20.00

City 2:
3 temperature1 are recorded.
The average temperature is 43.33
The highest temperature is 100.00
The lowest temperature is -20.00

Do you want to process another file?(y/n)


Here is my code.

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void process(ifstream & ifile); // Prototype
int main()
{
    string fname;
    ifstream ifile;
    char again;
    do
    {
        cout << "Enter the input filename: ";
        getline(cin, fname);
        ifile.open(fname.data());
        if (ifile)
        {
           cout << "**************************************************\n";
           process(ifile);
        }
        else
            cout << "Error opening " << fname << endl;
        ifile.close();
        ifile.clear();
        cout << "\nDo you want to process another file?(y/n) ";
        cin >> again;
        cin.ignore(80,'\n');
    } while (again == 'y' || again == 'Y');
    return 0;
}
void process(ifstream & ifile)
{
    string city;    // for reading in the city name
    int count1 = 0, // counts the number of temperature recorded for City 1.
        count2 = 0; // counts the number of temperature recorded for City 2/  
    float temp1,    // for reading in the temperature of City 1.
       highest1,    // for storing the highest temperature of City 1.
       lowest1,     // for storing the lowest temperature of City 1.
       sum1 = 0,    // for keeping the running total of the temperatures of City 1
       average1,    // for storing the average temperature of City 1.
       // Below variables are for City 2
       temp2,      
       highest2,
       lowest2,
       sum2 = 0,
       average2;

//    Pseudocode    
      getline(cin, city); 
      while (ifile >> temp1 || ifile >> temp2)
      {
            if (city == "City 1")
            {
                cin >> temp1; 
                if (temp1 > highest1) highest1 = temp1;
                if (temp1 < lowest1) lowest1 = temp1;
                sum1 += temp1; 
                count1++;
            }
            else if (city == "City 2")
            {
                cin >> temp2; 
                if (temp2 > highest2) highest2 = temp2;
                if (temp2 < lowest2) lowest2 = temp2;
                sum2 += temp2; 
                count2++; 
            }
      getline(cin, city); 
      }
      average1 = sum1 / count1; 
      average2 = sum2 / count2; 
      
      cout << "City 1:" << endl; 
   //   cout << count1 << " " << fname << " are recorded." << endl; 
      cout << "The average temperature is " << average1 << endl; 
      cout << "The highest temperature is " << highest1 << endl; 
      cout << "The lowest temperature is " << lowest1 << endl; 
      
      cout << "City 2:" << endl; 
    //  cout << count2 << " " << fname << " are recorded." << endl; 
      cout << "The average temperature is " << average2 << endl; 
      cout << "The highest temperature is " << highest2 << endl;
      cout << "The lowest temperature is " << lowest2 << endl; 
      
}


When I run the program, it compiles but the output does not appear.
Also I can't get the name of the file to show up (code that is commented out) in the output since it is out of scope. How would I fix these issues?
Last edited on
A loop for reading a file generally looks like, this is based on some of your variables and not..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int inTemp;
string city;

while(!infile.eof())  // test against the end of file
{
        getline(infile, city);  // read the first line.
        infile >> inTemp;     // read the temp.

        // now we process what we read in.
        if(city == "City 1")
        {
               //.....
        }
        else if(city == "City 2")
        {
             //.....
        }
}  // end of file loop 


I hope that points you in a direction.
Hold on I am going to post new code soon.
Last edited on
Here is my code. It almost works the way I want it to.
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void process(ifstream & ifile); // Prototype
int main()
{
    string fname;
    ifstream ifile;
    char again;
    do
    {
        cout << "Enter the input filename: ";
        getline(cin, fname);
        ifile.open(fname.data());
        if (ifile)
        {
           cout << "**************************************************\n";
           process(ifile);
        }
        else
            cout << "Error opening " << fname << endl;
        ifile.close();
        ifile.clear();
        cout << "\nDo you want to process another file?(y/n) ";
        cin >> again;
        cin.ignore(80,'\n');
    } while (again == 'y' || again == 'Y');
    return 0;
}
void process(ifstream & ifile)
{
    string city;    // for reading in the city name
    int count1 = 0, // counts the number of temperature recorded for City 1.
        count2 = 0,
        intemp;  
        // counts the number of temperature recorded for City 2/  
    float temp1,    // for reading in the temperature of City 1.
       highest1,    // for storing the highest temperature of City 1.
       lowest1,     // for storing the lowest temperature of City 1.
       sum1 = 0,    // for keeping the running total of the temperatures of City 1
       average1,    // for storing the average temperature of City 1.
       // Below variables are for City 2
       temp2,      
       highest2,
       lowest2,
       sum2 = 0,
       average2;

//    Pseudocode    
      
      while (!ifile.eof())
      {
            getline(ifile, city); 
             
            if (city == "City 1")
            {
                ifile >> temp1; 
                if (temp1 > highest1) 
                highest1 = temp1;
                if (temp1 < lowest1) 
                lowest1 = temp1;
                sum1 += temp1; 
                count1++;
            }
            else if (city == "City 2")
            {
                ifile >> temp2; 
                if (temp2 > highest2) 
                highest2 = temp2;
                if (temp2 < lowest2) 
                lowest2 = temp2;
                sum2 += temp2; 
                count2++; 
            }
      getline(ifile, city); 
      }
      average1 = sum1 / count1; 
      average2 = sum2 / count2; 
      
      cout << "City 1:" << endl; 
      cout << count1 << " " << " are recorded." << endl; 
      cout << fixed << setprecision(2) << "The average temperature is " << average1 << endl; 
      cout << fixed << setprecision(2) << "The highest temperature is " << highest1 << endl; 
      cout << fixed << setprecision(2) << "The lowest temperature is " << lowest1 << endl; 
      
      cout << "City 2:" << endl; 
      cout << count2 << " " << " are recorded." << endl; 
      cout << fixed << setprecision(2) << "The average temperature is " << average2 << endl; 
      cout << fixed << setprecision(2) << "The highest temperature is " << highest2 << endl;
      cout << fixed << setprecision(2) << "The lowest temperature is " << lowest2 << endl; 
      
}

It works for temperature2.txt (below)
----------------------------------------------------
City 2
-25
City 1
-24
City 2
-23
City 1
-22
City 2
-21
City 1
-20
City 2
-19
City 1
-18
City 2
-17
City 1
-16
City 2
-15
City 1
-14
City 2
-13
City 1
-12
City 2
-11
City 1
-10
City 2
-9
City 1
-8
City 2
-7
City 1
-6
City 2
-5
City 1
-4
City 2
-3
City 1
-2
City 2
-1
City 1
0
City 2
1
City 1
2
City 2
3
City 1
4
City 2
5
City 1
6
City 2
7
City 1
8
City 2
9
----------------------------------------------------------
But the lowest value for City 2 in temperature1.txt (below) is incorrect for some reason, I get a gigantic number. (everything else is correct, including City 2 lowest in temperature2.txt)
------------------------------------------------------------
City 1
-20
City 2
-20
City 1
100
City 2
100
City 1
50
City 2
50
------------------------------------------------------------
Also how do I state the name of the file in the output? (Since it is out of scope).
Last edited on

How do I state the name of the file in the output?
cout << fname;
or
cout << (fname.c_str());

Initialize your variables like this, so they will be handled properly:
1
2
3
4
5
6
7
8
9
10
float temp1,
       highest1 = -65000.f,
       lowest1 = 65000.f,
       sum1 = 0.f,
       average1,
       temp2,
       highest2 = -65000.f,
       lowest2 = 65000.f,
       sum2 = 0.f,
       average2;
Instead of -6500.f I just initialized it to 0 and it worked. Your method seems a bit more complicated.

The only problem left: I cannot change the main function to display the name of the file so how would I display it in the 'process' function? (I am not allowed to change main function.)

These two lines work.
1
2
cout << count1 << " " << " are recorded." << endl; 
cout << count2 << " " << " are recorded." << endl;


These don't.
1
2
cout << count1 << " " << fname << " are recorded." << endl;
cout << count2 << " " << fname << " are recorded." << endl;
Last edited on
Instead of -6500.f I just initialized it to 0 and it worked. Your method seems a bit more complicated.

If in your file you will never find values over 0, you will never record a max/min value.
Probably You Cannot on the fly.
Last edited on
So is what does -65000.f and 65000.f mean? Does it mean the number of digits (65000 digits) of any number positive or negative?
Last edited on
It means the lowest/highest float number that can be reached (It's not the lowest/highest float number, it's just approximative, but the lower/higher, the better, but be careful not to exceed)
If I changed my variables to doubles I would not need this right? (Just insert 6500. without f after .?) Since the default for floating point literals is double, not float.
Last edited on
Yes. Since you are using integers in your code, you can check out for the lowest/highest value for an integer.
Lowest value, according to MSDN: –2147483647
Highest // // // // : 2147483647
Alright now this is my new code.
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void process(ifstream & ifile); // Prototype
int main()
{
    string fname;
    ifstream ifile;
    char again;
    do
    {
        cout << "Enter the input filename: ";
        getline(cin, fname);
        ifile.open(fname.data());
        if (ifile)
        {
           cout << "**************************************************\n";
           process(ifile);
        }
        else
            cout << "Error opening " << fname << endl;
        ifile.close();
        ifile.clear();
        cout << "\nDo you want to process another file?(y/n) ";
        cin >> again;
        cin.ignore(80,'\n');
    } while (again == 'y' || again == 'Y');
    return 0;
}
void process(ifstream & ifile)
{
    string city;    // for reading in the city name
    int count1 = 0, // counts the number of temperature recorded for City 1.
        count2 = 0;
          
        // counts the number of temperature recorded for City 2/  
    double temp1,
       highest1 = -3.4E-38,
       lowest1 = 3.4E-38,
       sum1 = 0,
       average1,
       temp2,
       highest2 = -3.4E-38,
       lowest2 = 3.4E-38,
       sum2 = 0,
       average2;
   
      
      while (!ifile.eof())
      {
            getline(ifile, city); 
             
            if (city == "City 1")
            {
                ifile >> temp1; 
                if (temp1 > highest1) 
                highest1 = temp1;
                if (temp1 < lowest1) 
                lowest1 = temp1;
                sum1 += temp1; 
                count1++;
            }
            else if (city == "City 2")
            {
                ifile >> temp2; 
                if (temp2 > highest2) 
                highest2 = temp2;
                if (temp2 < lowest2) 
                lowest2 = temp2;
                sum2 += temp2; 
                count2++; 
            }
      getline(ifile, city); 
      }
      average1 = sum1 / count1; 
      average2 = sum2 / count2; 
      
      cout << "City 1:" << endl; 
      cout << count1 << " " << " are recorded." << endl; 
      cout << fixed << setprecision(2) << "The average temperature is " << average1 << endl; 
      cout << fixed << setprecision(2) << "The highest temperature is " << highest1 << endl; 
      cout << fixed << setprecision(2) << "The lowest temperature is " << lowest1 << endl; 
      
      cout << "City 2:" << endl; 
      cout << count2 << " " << " are recorded." << endl; 
      cout << fixed << setprecision(2) << "The average temperature is " << average2 << endl; 
      cout << fixed << setprecision(2) << "The highest temperature is " << highest2 << endl;
      cout << fixed << setprecision(2) << "The lowest temperature is " << lowest2 << endl; 
     
      
}

I decided to stick the highest possible values for a double into the program (according to my textbook). These temperatures are not even physically feasible so I feel safe now.

I just have one more issue. I need to display the name of the file like this.
Enter the input filename: temperature1.txt
**************************************************
City 1:
3 temperature1 are recorded.
The average temperature is 43.33
The highest temperature is 100.00
The lowest temperature is -20.00

City 2:
3 temperature1 are recorded.
The average temperature is 43.33
The highest temperature is 100.00
The lowest temperature is -20.00

Do you want to process another file?(y/n) y

I cannot change the main function in the process.
Like I said before.
These two lines work. (lines 81 and 87)

1
2
cout << count1 << " " << " are recorded." << endl; 
cout << count2 << " " << " are recorded." << endl;


These don't. (replacements for lines 81 and 87)

1
2
cout << count1 << " " << fname << " are recorded." << endl;
cout << count2 << " " << fname << " are recorded." << endl;


EDIT: Apparently I don't need to write the name of the file since it is pretty obvious what it is. Thanks for all the help guys.
Last edited on
Topic archived. No new replies allowed.