writing code to check if year is a leap year

I need help with fixing the error I get with my code.
The error is "expression must have point to object type."


[code]
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>

using namespace std;


int main()
{
int i = 0,
days,
choice = 0,
year,
n;

const int months = 12;

string month[] = { "January", "February", "March", "April", "May", "June", "july", "August", "September", "October", "November", "December" };

for (i = 0; i <= 6; i++)
{
if (i % 2 == 0)
{
days[i] = 31;
}
else
{
days[i] = 30;
}
}
for (i = 7; i < 12; i++)
{
if (i % 2 == 0)
{
days[i] = 30;
}
else
{
days[i] = 31;
}
}

do
{

cout << "Enter the month number from option below: - " << endl;
for (i = 0; i < 12; i++)
{
cout << (i << 1) << " " << month[i] << endl;
}
cin >> n;

if (n == 2)
{
cout << "Enter the year " << endl;
cin >> year;

bool leap = false;

if (year % 4 == 0)
{
if (year % 100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if (year % 400 == 0)
{
leap = true;
}
else
{
leap = false;
}
}
else
{
leap = true;
}
}
else
{
leap = false;
}

if (leap)
{
cout << year << L" is a leap year." << endl;
days[1] = 29;
}
else
{
cout << year << L" is not a leap year." << endl;
days[1] = 28;
}
}
cout << month[n - 1] << " " << days[n - 1] << std::endl;

cout << "Enter 1 to continue or repeat and any other input to exit" << endl;
cin >> choice;
} while (choice == 1);

system("Pause");
return 0;
}


Last edited on
closed account (yARMjE8b)
Your error points to the very first line:

#include "stdafx.h"


I looked it up and this thread explains why that line generated an error:

http://www.cplusplus.com/forum/beginner/62451/

In addition, here's an article explaining that line of code:

http://www.cplusplus.com/articles/2z86b7Xj/

Hope this helps
 
    days[i] = 31;

days should be an array, but it is a plain integer.

Also, not an error, but the code should make use of the constant which was declared but not used:
 
const int months = 12;
- though actually I often use a value of 13 when dealing with months, that means the first entry in the array is unused, but elements 1 to 12 correspond with our usual human usage of those numbers to represent months.
Hello ricardo l17

Welcome to the forum.

It looks like you missed the closing [/code] tag. I offer this if you need some help:
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

All of your big if statement can be reduced to if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)) then it is a leap year.

When you created your project or whatever you did, did you use the wizard to create everything or create an empty project from the wizard?

An empty project does not need the "stdafx.h" file.

Hope that helps,

Andy
I used the wizard to create the project
Hello ricardo l17,

Using the wizard is fine. It sets you up with the code for main to get you started. I even found the file that the wizard used to create the file that contains main and made so changes to it for my own use.

The problem that I found is not in the use of including "stdafx", but as Chervil said in your code you are using "days[i]" as an array, but you defined "days" as a single variable not an array. Changing the definition of "days" to an array solved the problem.

Now if you would like to discus how to use VS we can use the private message system here so as not to tie up the public forum.

I still need to test your code to see if there are any other problems other than the first two for loops.They work, but there is/are better ways to deal with the days of the months that are much easier.

Hope that helps,

Andy
Last edited on
Hello ricardo l17,

Just to give you an idea of what you can do. Read the comments in the program:

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
//#include "stdafx.h"  // <--- Not needed outside of your VS usage.
#include <iomanip>
#include <iostream>
#include <cmath>
#include <string>

using namespace std;


int main()
{
	const int MAXSIZE = 12;  // Changed name. Not necessary though. Should be the first line of main or above main.
	int i = 0,
		days[MAXSIZE]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},  // <--- Changed to an array.
		choice = 0,
		year{},  // <---Needs to be initialized.
		n{};  // <---Needs to be initialized.


	//  Used "months" in the array. Better to show what you mean.
	string month[MAXSIZE] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

	//  These for loops not needed if you initialize the array as I did.
	//for (i = 0; i <= 6; i++)
	//{
	//	if (i % 2 == 0)
	//	{
	//		days[i] = 31;
	//	}
	//	else
	//	{
	//		days[i] = 30;
	//	}
	//}
	//for (i = 7; i < 12; i++)
	//{
	//	if (i % 2 == 0)
	//	{
	//		days[i] = 30;
	//	}
	//	else
	//	{
	//		days[i] = 31;
	//	}
	//}

	do
	{

		cout << "Enter the month number from option below: - " << endl;
		for (i = 0; i < 12; i++)
		{
			cout << (i + 1) << " - " << month[i] << endl;
		}
		std::cout << "\n Enter month: ";
		cin >> n;

		if (n == 2)
		{
			cout << "Enter the year: ";
			cin >> year;

			bool leap = false;

			if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0))
			{
				cout << '\n' << year << " is a leap year." << endl;
				days[1] = 29;
			}
			else
			{
				cout << '\n' << year << " is not a leap year." << endl;
				days[1] = 28;
			}
		}

		cout << '\n' << month[n - 1] << " has " << days[n - 1] << " days" << std::endl;

		cout << "\n Enter 1 to continue or repeat and any other input to exit: ";
		cin >> choice;
	} while (choice == 1);

	std::cout << "\n\n";

	//system("Pause");  // <--- Not the best idea to use "system"  and not really needed here with the do/while loop.
	return 0;
}


Hope that helps,

Andy
Helped a lot thanks you.
Topic archived. No new replies allowed.