# Thread: Nested for loop logic, checking for year then values for the months - C++

1. ## Nested for loop logic, checking for year then values for the months - C++

So I have a method, that takes in my vector and does a for loop where it checks if the year and month entered in by the user exists in the file, the data from those matches is taken in and some calculations performed. Now I have to do another condition, where the user only enters in the year and I need to get all records for all the months that have information in that year.

What I have so far:

Code:
```int month;
int year;
int SIZE = windlog.size();
float averagetemp[SIZE];
float sd[SIZE];
float sum = 0;
float sigma = 0;
float avgtemp1;
float sd1;
int nrofel = 0;
int check[SIZE];

cout << "Please enter a year: " << endl;
cin >> year;

for(int i = 0; i < windlog.size(); i++){

if(windlog[i].d.GetYear() == year){

for(int x = 1; x < 13; x++){

if(windlog[i].d.GetMonth() == x){

nrofel++;
sum += windlog[i].temp.GetTemperature();
averagetemp[i] = (sum/nrofel);
sigma += (windlog[i].temp.GetTemperature() - averagetemp[i])*(windlog[i].temp.GetTemperature() - averagetemp[i]);
sd[i] = (sigma)/(nrofel - 1);
check[i] = x;

}

}

}

}

int z = 0;
for(int y = 1; y < 13; y++){

if(check[z] != y){

cout << checkMonth(y) << ": No Data" << '\n';
z++;

}
else{

cout << checkMonth(y) << ": " << averagetemp[z] << " degrees C, stdev: " << sd[z] << '\n';

}

}

}```
So far my output shows 'No Data' for every month except March in the year 2014, however there is data from March till December. Pretty sure my logic is incorrect, can anyone point me in the right direction?

If the input file is needed let me know. Just to clarify, the first column consists of both the date and time, and I split the values in the program. 2. Well your check[i] = x; is full of garbage for all the years and months that don't meet your tests.

x,y,z are not good variable names. 3. windlog[i].d.GetYear()
windlog is ??? 4. Originally Posted by maemec windlog is ???
Windlog is a vector, that stores Date, Time, Temperature and Solar Radiation from the input file. 5. Just to clarify a little more, all I have to do is find out if the Year at position[i] of windlog is equal to the year input by the user, for all the records of each month in that year I need to make calculations. If there is no existing month for that year, say there are no records for March, I just have to output 'No Data' for that month. If there are say 60 records for April, I need to calculate average temperature and standard deviation for all 60 records in the month of April and so on. Popular pages Recent additions #### Tags for this Thread

arrays, c++, float, logic, loops 