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;
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.