Nested for loop troubles

I've been given an assignment where I am to pull data from a .txt file. I was told to use a nested for loop within a for loop. I have it set up to the point where it prints the name and elevation perfectly, but precipitation sum from each row is used at the beginning of the second for loop and it creates one large total.

How do I tell the loop to start over when it gets to a new row?

Thanks for any help!




Here is the text file data:
AGUA CANYON 8900 2.4 3.1 2.7 1.9 1.5 0.9 1.9 2.7 2.3 2.1 2.2 2.4
BEAVER DAMS 8000 2.9 2.8 3.4 3.4 2.2 0.9 1.1 1.7 1.9 2.4 2.7 2.7
BEAVER DIVIDE 8280 3.3 3.1 3.3 3.3 2.6 1.5 1.4 1.8 1.8 2.2 2.9 3.3
BEN LOMOND PEAK 8000 8.9 8.2 6.7 4.8 5 3 1.5 2.3 4 4.7 6.8 8.2
BEN LOMOND TRAIL 6000 5.7 5.3 4.5 3.5 3.7 2 1.1 1.7 2.7 3.8 5 5.5
BIG FLAT 10290 3.8 3.6 4 3.6 2.9 1.7 1.8 2.4 2.1 2.2 2.7 3.1
BLACK FLAT-U.M. CK 9400 1.9 2.1 2.3 2.1 1.8 1.1 1.8 2.1 1.8 1.8 1.8 1.8
BOX CREEK 9800 2.6 2.8 3.2 2.8 2.3 1.2 1.3 2 1.9 2 2.2 2.2
BRIGHTON 8750 4.9 5.1 5.3 4.3 3.5 1.6 1.6 1.9 2.4 3.6 4.8 5
BROWN DUCK 10600 3.4 3.9 3.6 3.3 2.9 2.1 2.1 2.3 2.5 2.9 3 3
BUCK FLAT 9800 3.4 3.6 3.6 2.9 2.5 1.2 1.7 2 2.2 2.5 3.3 3.3
BUG LAKE 7950 3.7 3.3 3.3 2.9 2.7 1.5 0.8 1.3 1.7 2.4 3.3 3.5
CAMP JACKSON 8600 3.6 3.3 3.1 2.1 1.6 0.9 2.3 2.6 2.2 3 2.8 3.2
CASTLE VALLEY 9580 2.6 2.9 3.1 2.4 1.7 1 1.9 2.3 1.9 2 2.2 2.2
CHALK CREEK #1 9100 4.3 4.2 4.6 4.7 3.4 1.9 1.6 1.9 2.4 3.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3.2 5.1 6.4 6.9
STEEL CREEK PARK 10100 2.5 2.8 3.4 3.9 3 1.7 2 2 2 2.4 2.8 2.4
STRAWBERRY DIVIDE 8400 4 3.9 3.8 3.2 2.3 1.3 1 1.5 2.1 2.8 3.4 4.1
THAYNES CANYON 9200 4.2 4.6 4.2 3.6 2.9 1.2 1.3 1.8 2 3.1 3.8 4.3
TIMPANOGOS DIVIDE 8140 4.7 4.6 4.3 3.1 3.1 1.7 1.5 1.9 2.6 3.3 3.9 4.4
TONY GROVE LAKE 8400 7 5.8 5.6 4.7 3.6 1.8 1.2 1.6 2.3 3.8 6.2 6.7
TRIAL LAKE 9960 4.6 4.7 4.7 4.2 3.9 2.1 1.7 2.3 2.4 2.9 3.8 4.5
TROUT CREEK 9400 1.7 2.1 3 2.9 2.7 2.1 2.1 2.1 2.1 2.2 2.3 2
VERNON CREEK 7500 2.9 3 3.1 2.3 2.3 1.1 0.8 1.2 1.5 2.1 2.7 2.5
WEBSTER FLAT 9200 2.9 3.6 4 3.1 2.2 1.1 1.9 2.8 2.1 2.3 2.7 2.6
WHITE RIVER #1 8550 2.6 2.3 2.2 1.9 1.8 1 1.2 1.4 1.5 1.9 2.5 2.6
WIDTSOE #3 9500 2.2 2.8 3 2.3 1.8 1 1.5 2 1.8 1.9 2.1 1.9

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
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;

int main(){
    
    int count, count2, elevation;
    float precip, total, sum;
    const int NUM_RESORTS = 78, NUM_MONTHS = 12;
    string resort, nameTitle="Weather Station", elevTitle="Elevation", precipTitle="Precipitaiton";
    
    ifstream inFile;
    ofstream outFile;
    inFile.open("monthlyPrecipitation.txt");
    outFile.open("annualPrecipitation.txt");
    
    cout << "\nThis is the Annual Precipitation Report!\n" << endl;
    cout << left << setw(23) << nameTitle << setw(18) << elevTitle << right << setw(10) << precipTitle << endl << endl;
    
    if (!inFile)
    {
        cout << "Could not open the file. Please check the file and try again!" << endl;
    }
    
    else{
        cout << setprecision(1) << fixed;
        
        
        for (count = 0; count < NUM_RESORTS; count++)
        {

            getline(inFile, resort, '\t');
            cout << left << setw(25) << resort;
            inFile >> elevation;
            cout << right << setw(5) << elevation;
            
            
            
           for (count2 = 0; count2 < NUM_MONTHS; count2++)
            {
                inFile >> precip;
                total += precip;
            }
            
            
            
            cout << right << setw(20) << total;
            cout << endl;
            inFile.ignore();
            
        }
        
    }
    
    cout << endl;
    return 0;
    
}
You get the nested loop wrong. The for loops need to be one after another.

Create an encompassing loop like so:
1
2
3
4
while(inFile.good())
{
  ... // the two loops
}


Note that the above loop will read one more line then existing. To avoid that you need to check the error state of the stream after you read something (i.e. line 34, 36, 43) and break; the loops accordingly.
Topic archived. No new replies allowed.