Need help with homework almost done

I am very new to c++ and would like some help writing this out. I am not asking for you to write it out for me. I'm just asking for someone to help me start it.

So the program I have to write is if someone picks the years from 400 to 2400 is it going to be a leap year using the if else statement and here is what i have so far.


My teacher said it needs to be nested and also tested for each one of the equations
Also I think im missing this but not sure where to put it (year <400 || year > 2400) return -1;

here is what I have

using namespace std;



//main body


int main ()
{
//local identifiers
int Year,LeapYear;
//input module

cout<<"please input year that you would like to check:\t";
cin>>Year;
if
(Year % 4!=0)
cout<< "year is not leap year\t";
else
cout <<"year is Not a leap year\t";
if
(Year % 100!=0)
cout<< "Leap year";
else if (Year % 400!=0)
cout<< "is a century year";
else
cout<<" is a century leap year";
//output

cout<<"Year"<<endl;

system ("PAUSE");
return 0;

}
1
2
3
4
5
if
(Year % 4!=0)
cout<< "year is not leap year\t";
else
cout <<"year is Not a leap year\t";


Should that second cout statement be there??
haha thanks I didn't even notice my mistake
closed account (G854izwU)
Hello! Just for your information it would be helpful if you put your code in code format in future posts. Also not to be mean but it might help you out in the future if you spaced your code a little better. But now to the problem at hand.

I don't understand how this would work. These two lines.

1
2
3
4
5
if
(Year % 4!=0)
cout<< "year is not leap year\t";
else
cout <<"year is Not a leap year\t";


You know that the else means ANY other condition so your code is always going to say year is not a leap year. Another thing it is not good practice to use system("pause"); Because in the future when you start making bigger programs that people could actually start using it creates a large security hole.

Now if you could clarify what you want from me more I would appreciate it! I will try to help you as much as I can!
Last edited on
closed account (G854izwU)
Haha! the guy above already beat me to that.
Last edited on
Thanks im just so new to this
closed account (G854izwU)
Also I like to remind others along with myself very often that programming is not about being able to write the code it is about thinking in a logical and sequential way to get you to an answer. The code and the computer is just a tool to help you find the answer.

If I were you what I would do is I would get a big sheet of paper and just write down all of your thoughts and ideas about the program that you want to create. You should write down all the different things that your program needs to do and all the steps it needs to take to get the answer. In your case you would need to figure out how to tell if a year is a leap year, or if the year is a century year. You would also need to think about taking the user input and what you want to do with it along with what variables you need. Then once you have all of that just sort of write out what you want your program to do don't worry about writing real code just use plain English. Once you are done with that convert it to code. If it all works then the next step would to try to make the code as simple as possible think to yourself, do I really need that line? or how can I get rid of this part! I hope this helps.

-TheNewKid-
closed account (G854izwU)
Have you revised your code any because if you have I would like to see it!
closed account (G854izwU)
Don't copy just learn.

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
//This code was made completely by -TheNewKid-  Please do not take credit for this as your own.

#include <iostream>

using namespace std;

int main ()
{
    int year;

    cout << "Please input the year that you would like to check: ";
    cin >> year;

    if (year >= 400 && year <= 2400) {
        if (year % 4 == 0 && year % 100 == 0) {
            cout << "The year you entered is a century leap year!" << endl;
        }
        else if (year % 4 == 0 && year % 100 != 0) {
            cout << "The year you entered is a leap year!" << endl;
        }
        else if (year % 4 != 0 && year % 100 != 0) {
            cout << "The year you entered is not a leap year or a century leap year!" << endl;
        }


    }

    else {
        cout << "The year you entered is not valid!  Please enter a year between 400 and 2400." << endl << endl;
    }


    return 0;
}


Hope this helps. (I have been there before)

-TheNewKid-
Topic archived. No new replies allowed.