Not Declared In Scope

Can anyone please help? I really don't understand what's wrong with my program. Whenever I try to compile it, I end up with the phrase:
prog.cpp: In function ‘int main()’:
prog.cpp:30:18: error: ‘year’ was not declared in this scope
isfourdigit(year);

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cstdlib>
using namespace std;

bool isfourdigit(short int year);
bool isleapyear (short int year);
char userInput;
short int yearNumber = 0;

int main()
{
    do
    {
    	system("cls");

		cout << "Test if a Year is a Leap Year" << endl;
		cout << "-----------------------------" << endl << endl;
		cout << "Enter a 4-digit year: ";
		cin >> yearNumber;
    	cout << endl << endl;
    	
    	isfourdigit(year);
    	isleapyear(year);
    	
    	if (isfourdigit(year) == 1)
    	{
    		cout << "Error: Enter a 4-digit year!";
		}
    	else
    	{
    		if (isleapyear(year) == 1)
    		{
    			cout << "Year " << yearNumber << " is an ordinary year.";
    		}
    		else
    		{
    			cout << "Year " << yearNumber << " is a leap year.";
    		}
		}
    	
    	cout << endl << endl;
    	cout << "Run the program again?[y/n]: ";
    	cin >> userInput;

    	return 0;	
    } while (userInput == 'y' || userInput == 'Y');
}

bool isfourdigit(short int year)
{
	if (yearNumber < 1000 || yearNumber > 9999)
	{
		year = 1;
	}
	else 
	{
		year = 0;
	}
	
	return year;
}

bool isleapyear(short int year)
{
	if (yearNumber % 4 == 0)
	{
		if (yearNumber % 100 == 0)
		{
			if (yearNumber % 400 == 0)
			{
				year = 1;
			}
			else
			{
				year = 0;
			}
		}
		else
		{
			year = 1;
		}
	}
	else
	{
		year = 0;
	}
	
	return year;
}
Last edited on
Please use code tags.
http://www.cplusplus.com/articles/z13hAqkS/

Where is the variable 'year' declared?
Whoops, I'll fix that. Also, doesn't it count that I declare the "year" variable in the bool functions? Sorry, I'm still a beginner and don't understand much. XD
On line 23, for example, you try to pass the variable 'year' as a parameter to your function, but there isn't any such variable defined in main(). What value/variable where you trying to pass to it?
Basically, I wanted int main() to perform the bool functions so that "year" can be assigned the value of 1 or 0.

For example, in line 22, I declared the function so that it would have to skip to line 49 and calculate whether "year" is 1 or 0. After that, the bool isfourdigit function will return the data to int main so that it can do the next procedures.

I'm just not sure if I declared the functions in int main correctly, if the data was really returned, or if my logic is correct.

Did that help?
The year in your isfourdigit and isleapyear functions have nothing to do with each other, or with the (non-existent) year in main.

Perhaps a quick recap on functions is in order:
http://www.cplusplus.com/doc/tutorial/functions/

What I would do is move
7
8
char userInput;
short int yearNumber = 0;
into main() (global variables are typically frowned upon), and change your functions to
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
bool isfourdigit(short int yearNumber)
{
    if (yearNumber < 1000 || yearNumber > 9999)
    {
        return false; // You originally had 'year = 1;' here (presumably corresponding to 'true'),
                      // but I felt that returning false was more appropriate since the function
                      // is called 'isfourdigit' and if we're in this branch of the if statement,
                      // then the number is NOT four digits.
    }
    else 
    {
        return true;
    }
}

bool isleapyear(short int yearNumber)
{
    // Similar to above 

(Basically, make yearNumber your parameter and return true or false instead of setting a year variable).

Your function calls in main would then simply be
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
if (!isfourdigit(yearNumber)) // if NOT four digits -- 
{                             // makes more sense than "if 'is four digits' is 1 ( == true?)"
    cout << "Error: Enter a 4-digit year!";
}
else
{
    if (!isleapyear(yearNumber))
    {
        cout << "Year " << yearNumber << " is an ordinary year.";
    }
    else
    {
        cout << "Year " << yearNumber << " is a leap year.";
    }
}
Oh wow, it actually works. Thanks, you guys are awesome. :)
Topic archived. No new replies allowed.