How to make it continue looping

Code is completely finished, but I forgot one part. The program will read in a year, the number of times it needs to run (runs), and then several sets of inches. It works perfectly for one set of data, I'm not sure how to make it continue going. It needs to work with up to 3 sets. Included are two given sets of data.

Edit: I'm now realizing how gross this code actually is. Trying to fix it, but I feel like I just took 10 steps backwards.
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
  #include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	int year;
	int runs;
	int inches = 0;
	double avg;
	int total = 0;
	int month = 1;
	int count = 01;

	ifstream infile;
	ofstream outfile;

	outfile.open("out.txt");
	infile.open("text.txt");

	infile >> year >> runs;
	
	
	while (count <= runs)
	{
		
		while (month <= 12)
		{
			outfile << "** " << year << " **";
			infile >> inches;
			total = total + inches;
			outfile << "\nJanuary " << setw(6) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nFebruary " << setw(5) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nMarch " << setw(8) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nApril " << setw(8) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nMay " << setw(10) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nJune " << setw(9) << inches << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nJuly " << setw(9) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nAugust " << setw(7) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nSeptember " << setw(4) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nOctober " << setw(6) << inches  << " inches";
			infile >> inches;
			total = total + inches;
			outfile << "\nNovember " << setw(5) << inches  << " inches";
			infile >> inches;
			outfile << "\nDecember " << setw(5) << inches << " inches";
			
			avg = total / 12.00;
			outfile << "\nTotal " << setw(11) << fixed << setprecision(2) << avg
				<< " inches";
			outfile << "\n\n\n";
			
			
			year++;
			total = 0;
			break;
		}

		
		count++;
		
	}

	
	outfile.close();
	infile.close();
	return 0;
}


1990 5
10 12 14 15 12 9 4 4 8 12 9 10
12 10 5 10 12 5 14 4 6 11 6 11
13 5 2 13 19 11 11 3 9 12 1 12
20 6 12 9 8 8 8 10 9 13 9 4
11 3 9 12 1 12 20 6 12 9 8 8

1910 11
10 8 3 10 12 14 15 12 9 4 4 8
12 9 10 12 10 5 10 12 5 14 4 6
11 6 11 4 2 17 9 9 9 0 0 8
13 5 2 13 19 11 11 3 9 12 1 7
12 20 6 12 9 8 8 8 9 13 9 4
11 3 9 12 1 12 20 6 12 9 8 8
3 5 2 13 19 11 11 3 9 12 1 12
6 2 3 8 9 10 11 0 9 2 5 11
2 6 12 9 8 8 8 1 9 13 9 4
11 3 9 12 1 12 20 10 12 4 5 2
19 0 2 18 3 7 9 2 0 6 5 12
Last edited on
You keep missing key steps out of your while loops.

FWIW, I think you'd be better off prototyping the code using for loops to make the thing work, THEN do the deliberate transformation to while loops with code that works.

1
2
3
4
5
6
for ( count = 0 ; count < runs ; count++ ) {
    for ( months = 0 ; months < 12 ; months++ ) {
        infile >> inches;
        total = total + inches;
    }
}


A for loop of the form
1
2
3
for ( a ; b ; c ) {
   d;
}


Can be rewritten as
1
2
3
4
5
a;
while ( b ) {
   d;
   c;
}


So
1
2
3
4
5
6
7
8
9
10
count = 0
while ( count < runs ) {
    months = 0;
    for ( months < 12 ) {
        infile >> inches;
        total = total + inches;
        months++;
    }
    count++;
}

I have an awful professor and I can't even touch for loops right now. Unless it's a way to cut down on the repetition, I really can't edit this code anymore. I need to get this final loop done, the restrictions I've had to get around have been insane.
I'm not suggesting you HAND IN for loops.

I'm suggesting you USE for loops to get your program correct first, then change them to while loops and hand that in.

> outfile << "\nJanuary " << setw(6) << inches << " inches";
Do you know about arrays?

 
string monthnames[] = { "January", ... };


Which then enables you to write
outfile << monthnames[month] << setw(6) << inches << " inches";

Instead of copy/pasting the same code 12 times.
Line 28: Don't forget that each time though the outer loop you have to set months back to 1 before entering the inner loop.

Line 31: I think this line is out of place. It should be at line 28.

Lines 68-71: These should be in the outer loop, not the inner loop.

Line 75: You should reset total at the top of the loop, not the bottom.

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

using namespace std;

const char * monthnames[13] =
{   "",     //  Not used
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December" };
    
int main()
{
    int year;
    int runs;
    int inches = 0;
    double avg;
    int total = 0;
    int month = 1;
    int count = 01;

    ifstream infile;
    ofstream outfile;

    outfile.open("out.txt");
    infile.open("text.txt");

    infile >> year >> runs;

      while (count <= runs)
    {
        outfile << "** " << year << " **";
        total = 0;
        for (month = 1; month <= 12; month++)
        {
            infile >> inches;
            total = total + inches;
            outfile << "\n" << monthnames[month] << setw(6) << inches << " inches";
        }

        avg = total / 12.00;
        outfile << "\nTotal " << setw(11) << fixed << setprecision(2) << avg
                << " inches";
        outfile << "\n\n\n";
        year++;
        count++;
    }

    outfile.close();
    infile.close();
    return 0;
}

Last edited on
Hello ZestyCthulhu,

I am a bit unclear on the instructions. Are you only allowed two while loops?

I used three while loops with the first being: while (inFile >> year >> numYears). Sorry I ended up changing "infile" to "inFile" that I usemost often. This way when you try to read past end of file the stream faile with the "eof" bit set making the while condition false, so the loop fails.

You have opened a file for input, but never check to make sure it opened. I use this when opening an input file:
1
2
3
4
5
6
7
8
9
10
const std::string inFileName{ "info1.txt" };

std::ifstream inFile(inFileName);

if (!inFile)
{
	std::cout << "\n File \"" << inFileName << "\" did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread". Optional as is the header files.
	return 1;  //exit(1);  // If not in "main".
}

The if state is most important.

The same concept can be used for the output file, but it is not as important because it is harder for an output file stream to fail, but it can happen.

The inner while loop to print all the inched looks like it will print a nice format, but you could shorten all that to:
1
2
3
4
5
6
7
8
9
while (month < NUMMONTHS)
{
	inFile >> inches;

	outFile << setw(pos[month]) << " " << setw(2) << inches;

	total += inches;
	month++;
}

The total += inches; does the same as what you are doing. Using the array in the first "setw" allowed me to position the numbers under the headings the way I wanted.

When you marked the last thread as finished I just played around with the code for fun. To that end I changes some of the variable names and did things differently.

Both salem c and AbstractionAnon make very good points.

Just as an idea this is the output I get:

January  February March April  May June July August September October November December  Avg 
                                             ** 1990 **
  10       12      14    15     12   9    4     4       8       12        9       10     9.92

                                             ** 1991 **
  12       10       5    10     12   5   14     4       6       11        6       11     8.83

                                             ** 1992 **
  13        5       2    13     19  11   11     3       9       12        1       12     9.25

                                             ** 1993 **
  20        6      12     9      8   8    8    10       9       13        9        4     9.67

                                             ** 1994 **
  11        3       9    12      1  12   20     6      12        9        8        8     9.25


I do not know if it matches what you need because you did not post an example

Hope that helps,

Andy
Topic archived. No new replies allowed.