Uninitialized Local Variable

Feb 2, 2017 at 1:16am
So apparently this function receives errors after the first "if" statement saying "uninitialized local variable 'yearEntered' used" although I thought it was initialized when I declared int yearEntered = 0. Any help? Thanks

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
void print10LeapYears() {
    
    int yearEntered = 0;

    cout << "Enter year -->";
    cin >> yearEntered;
    cout << endl << "Leap year is" << endl;
    isLeapYear(yearEntered);

    if (isLeapYear == false) {
        for (int yearEntered; isLeapYear(yearEntered) == true; yearEntered = yearEntered + 1) {
        }
        for (int yearEntered; yearEntered < (yearEntered + 36); yearEntered = yearEntered + 4) {
            cout << yearEntered << endl;
        }
    }
    else {
        for (int yearEntered; yearEntered < (yearEntered + 36); yearEntered = yearEntered + 4) {
            cout << yearEntered << endl;
        }
    }
    
    
    return;
}
Last edited on Feb 2, 2017 at 1:18am
Feb 2, 2017 at 2:09am
Line 11, line 12 and 18, why are you redeclaring yearEntered ?
Also, instead of yearEntered = yearEntered + 1, you could have used ++yearEntered or yearEntered++.

As for yearEntered = yearEntered + 4 I think it is better to use
yearEntered += 4
Last edited on Feb 2, 2017 at 2:14am
Feb 2, 2017 at 3:39am
Thanks, I appreciate that catch. I'll see if it works with my program now!
Feb 2, 2017 at 3:48am
Okay, so it definitely compiles now, which is great, but apparently I set up the loop in a way that I didn't intend. The point of the function is to input a given year and then it returns the next 10 leap years. My isLeapYear function is definitely correct since I tested it, but I suppose something isn't working right with it. I also need it to obviously stop once it returns the next 10 leap years and right now it's going indefinitely. Sorry if this requires another topic post.

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
void print10LeapYears() {
    
    int yearEntered = 0;

    cout << "Enter year -->";
    cin >> yearEntered;
    cout << endl << "Leap year is" << endl;
    isLeapYear(yearEntered);

    if (isLeapYear == false) {
        for (yearEntered; isLeapYear(yearEntered) == true; yearEntered = yearEntered++) {
        }
        for (yearEntered; yearEntered < (yearEntered + 36); yearEntered = yearEntered += 4) {
            cout << yearEntered << endl;
        }
    }
    else {
        for (yearEntered; yearEntered < (yearEntered + 36); yearEntered = yearEntered += 4) {
            cout << yearEntered << endl;
        }
    }
    
    
    return;
}
Feb 2, 2017 at 3:57am
Line 10 doesn't do what you think it does. The condition is always false.

I suspect you want to do something like this:
1
2
3
4
5
6
7
int years_so_far = 0;
while (years_so_far < 10) { 
  if (isLeapYear(++year) { 
    std::cout << year << " is a leap year\n";
    years_so_far++;  
  }
}


PS.:
Next 10 leap years
Following your directions, if the user enters a leap year, it will not be counted.

PPS.:
It will help you to get in the habit of compiling with all warnings on.
Last edited on Feb 2, 2017 at 3:58am
Feb 2, 2017 at 5:13am
Last edited on Feb 2, 2017 at 5:13am
Feb 2, 2017 at 5:46am
And this is without mentioning the genuine performance problem all the extra flushing can cause.
http://chris-sharpe.blogspot.in/2016/02/why-you-shouldnt-use-stdendl.html


CoreGuidelines:
Apart from the (occasionally important) issue of performance, the choice between '\n' and endl is almost completely aesthetic. https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rio-endl
Feb 2, 2017 at 12:37pm
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    int year = 0;
    
    std::cout << "Enter a year: ";
    std::cin >> year;
    year += ( 4 - year % 4 );
    
    for(int i = 0; i < 10; i++)
    {
        std::cout << i + 1 << ' ' << year << '\n';
        year += 4;
    }
    
    return 0;
}
Feb 2, 2017 at 11:23pm
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/207530/
Topic archived. No new replies allowed.