I need some help inputting from text files

I need to set up a loop that will pull a set of data from a text file for example, wet.txt In the text file will contain high and low temperature data, as well as rain accumulations for an unknown amount of days, For example:

low high rain
30 60 0.3
25 55 0
30 65 0

The output will calculate all of the summed totals and give an average of all of the data from the text file. Right now when I run my program it freezes so I'm not sure where I am going wrong, here is what I have so far:

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

using namespace std;


int main(int argc, char *argv[])
{
    
    ifstream fin("wet.txt", ios::in);

    int numDays = 0;
    int sumLow   = 0;
    int sumHigh = 0;
    double sumRain = 0;
    int low, high, rain;
    
    
    
    while( fin.good() )
    {
        fin >> low >> high >> rain;
       
        sumLow += low;
        sumHigh += high;
        sumRain += rain;
        ++numDays;
    }

    int avgLow = sumLow / numDays;
    int avgHigh = sumHigh / numDays;
    double avgRain = sumRain / numDays;
    
    cout << avgLow << 
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
There are two or maybe three issues with the way you read the file.

If this is the actual contents of the file,
low high rain
30 60 0.3
25 55 0
30 65 0

then the first issue is the header text, "low high rain". If that is really part of the file, then you need to read past that before commencing to read the numeric data. Either use getline() to read it into a string or use ignore() to read and ignore up to the first newline character.

After that, variable rain is of type int but the data value on the first row is 0.3. What happens here? Well, rain will contain the value 0, and the rest of the value, ".3" will remain in the buffer. On the next iteration, "." is not a valid character for int low so the read fails, and the fail() flag is set, no more input after that. So change the type of variable rain to double.

Finally, what happens when the last line of the file is reached? Well, if there is a trailing newline or any whitespace after the last value, the read will succeed, and the fin.good() condition will remain true, so one more iteration of the loop takes place.
In order to fix that, put the fin >> operation inside the while condition.

Something like this:
1
2
3
4
5
6
7
8
9
    fin.ignore(1000, '\n');     // ignore header line

    while( fin >> low >> high >> rain)
    {
        sumLow += low;
        sumHigh += high;
        sumRain += rain;
        ++numDays;
    }

Last edited on
I'd start by adding in a check that the file exists before opening it:
fstream dataFile;
dataFile.open("wet.txt". ios::in)
if (dataFile.fail())
{
// the file does not exist, you should create it
dataFile.open("wet.txt". ios::out);
//continue to process the file....
}
else // the file already exists
{
dataFile.close();
cout>>"the file wet.txt exists, lets proceed">> \n;

i'd also make sure the information in "wet.txt" is formatted correctly:




Topic archived. No new replies allowed.