This program is a headache, and every time I reread the directions I realize I'm doing something wrong. Since I doubt I could summarize it here are the directions:
Write a program that uses nested while loops to collect data and calculate the total rain fall for each year and over the average rainfall per year for a period of years. Read data from a file assuming the first value is the actual beginning year(e.g. 1990)and the second is the number of complete years to be processed. This is followed by the amount of rainfall in inches per month (January –December) for each of the years(groups of 12). Assume monthly totals are integer. Print the average for the years in 2 decimal places.
The outer while loop will iterate once for each of the years, to the end of the file. The inner while loop will iterate 12 times, one for each month of the year. Produce a report similar to the form shown below.(Note that the numbers are right aligned
The outfile or infile will not cooperate at all, and 100% of the time give a nonsense answer. You can see the small chunk where I read in and immediately outfile the year, which is always nonsense. If there are any other pieces of advice for this program I'll gladly take them, since I'm a little out of my depth here.
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
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int year;
int runs = 0;
int inches = 0;
//int avg;
int month = 1;
int i = 1;
ifstream infile;
ofstream outfile;
outfile.open("out.txt");
infile.open("info1.txt");
infile >> year;
outfile << year;
while (i <= runs)
{
outfile << "test";
outfile << "** " << year << " **";
while (month <= 12)
{
outfile << "\nJanuary" << inches << "\nFebruary" << inches
<< "\nMarch" << inches << "\nApril" << inches << "\nMay"
<< inches << "\nJune" << inches << "\nJuly" << inches
<< "\nAugust" << inches << "\nSeptember" << inches
<< "\nOctober" << inches << "\nNovember" << inches
<< "\nDecember" << inches;
month++;
}
year++;
i++;
}
outfile.close();
infile.close();
system("pause");
return 0;
}
|
The infile is:
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