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

  1. #1
    Registered User
    Join Date
    May 2020

    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:

    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){
                            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';
                    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    windlog is ???

  4. #4
    Registered User
    Join Date
    May 2020
    Quote Originally Posted by maemec View Post
    windlog is ???
    Windlog is a vector, that stores Date, Time, Temperature and Solar Radiation from the input file.

  5. #5
    Registered User
    Join Date
    May 2020
    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 subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-07-2019, 09:32 AM
  2. Replies: 1
    Last Post: 02-23-2012, 05:26 PM
  3. checking a leap year
    By elton_fan in forum C Programming
    Replies: 7
    Last Post: 04-10-2007, 05:59 AM
  4. a program to create months in a year newbie question
    By robasc in forum C Programming
    Replies: 2
    Last Post: 03-05-2005, 05:41 PM
  5. Months of the year in c
    By kingrizzla18 in forum C Programming
    Replies: 5
    Last Post: 11-18-2003, 09:31 PM

Tags for this Thread