I'm stuck and I don't know what to do I need help. I need help on how to do the nested for loops and how to get to data from the input file to display corresponding values. Can somebody help me please. Read the attached documents and see if you can please. Input file is also provided.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{

//Declares variables to hold value entered by user
	int number;
	int value;

//Loop declaration
	int year;
	int month;
	
//Open File
	ifstream inFile;
	inFile.open("input 3.txt");

	if (!inFile)						// if file could not be opened
	{
		cout << "Input file was not found!" << endl;
		return 1;
	}
	
	

//Displays title

	cout<<"This Program prints monthly and average rainfalls of the last 10 years."<<endl<<endl;

//Prompts the user to enter the input data, reads value from keyboard and stores it in corresponding value
	
	cout<<"Please enter the number of months per year to be averaged: ";
			cin>>number;
			cout<<endl;

//Validates entry of months, if not valid waits for valid entry

	while (number >= 1 && number <=12)

		inFile >> value;
		
		for (year = 0; year < 10; year++)						//outer loop for rows/lines
		{
												
		
		for (number = month; month < 12; month++)					//inner loop for columns/number per row
		{
			
			cout<<setw(10)<<right<<value;
		
		}
		
		}
		}if (!(number >= 1 && number <=12))
		{
			system ("cls");
		cout<<"This Program prints monthly and average rainfalls of the last 10 years."<<endl<<endl;
		cout<<"Please enter the number of months per year to be averaged: ";
			cin>>number;
			cout<<endl;
	}
	
//Close file
	inFile.close();
return 0;
}
Attachments
8 36 14 11 31 17 22 23 17 8 9 33 

23 32 18 39 23 25 9 38 14 38 4 22

18 11 31 19 16 17 9 32 25 8 1 23

19 13 33 28 5 28 34 19 34 32 0 7 

25 0 16 5 12 4 15 39 33 33 29 21

6 0 34 1 16 14 15 2 29 0 16 20

16 29 24 6 37 24 38 4 21 6 7 13

20 29 11 32 18 6 4 3 31 20 21 4

19 5 8 3 20 17 35 15 25 24 11 8

7 9 1 22 26 33 12 0 30 0 32 31

Interesting assignment, except for the requirement to use system("cls") to clear the screen. But that's your teacher's problem, not yours.

1) Your validation for months should test if the number entered is less than 1 or greater than 12. If it is, then print an error message and make the user re-enter.

2) You need to use { and } around the while statement beginning on line 43. Without them only line 45 will get executed within that loop.

This article has been dead for over six months. Start a new discussion instead.