Leap Year Program C++

Hey,

I need to write a program that tells whether it's a leap year or not. But I'm not quite sure how. If someone could point me in the write direction that'd be great thanks.

I've gotten this far but I'm a little stuck, any suggestions?
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
#include <iostream>

using namespace std;

int main()
{
    int year, not_leap_year, is_leap_year;

    cout << "Please enter a year:";
    cin >> year;

    if (year (4 >= 0))
    {
        cout << "Do something";
        then;
        {
            cout << "Do something";
        }
       if (year (100 >= 0))
       {
           cout << "Do something";
           then;
           {
                cout << "Do something";
           }
               if (year (400 >= 0))
               {
                   cout << "Do something";
                   then;
                   {
                       is_leap_year;
                       cout << year << " is a leap year.";
                   }
                    else;
                    {
                        not_leap_year;
                        cout << year << " is not a leap year.";
                    }
                }
            else;
            {
                is_leap_year;
                cout << year << " is a leap year.";
            }
        }
        else;
        {
            not_leap_year;
            cout << year << " is not a leap year.";
        }
     }

     cout << "The program will now close";

    return 0;
}
Last edited on
Start your C++ IDE or a text editor and start writing.
Use the provided pseudo-code and turn it into C++ code.
I know how to start and put it in a C++ editor, I use CodeBlocks, but I'm not sure how to get the program to tell a leap year using the below.

I've refined it, but I get some errors, what did I do wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main()
{
    int year, modulo, not_leap_year, is_leap_year;

    if (year modulo 4 is 0)
        then;
       if (year modulo 100 is 0)
           then;
               if (year modulo 400 is 0)
                   then;
                       is_leap_year;
               else;
                   not_leap_year;
        else;
            is_leap_year;
    else;
        not_leap_year;
    return 0;
}
Last edited on
I suggest reading the chapters introducing variables and operators in your textbook.
Or this:
http://www.cplusplus.com/doc/tutorial/operators/
Ok, thanks I will do that and try to figure it out.
Ok well read through everything, and fixed some stuff. I think I forgot my {} on my statements, which I just missed for some reason. Should I use a string to fix my problem?

Sorry I'm trying my professor has a LOT of homework this week and I'm really trying hard to figure everything out.

But I'm still getting some errors, which are;

I know the statements have no effect because I haven't told them to do anything yet, but I'm trying to get the syntax errors out of way so I can start making it work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp||In function `int main()':|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|12|error: `year' cannot be used as a function|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|16|error: `then' undeclared (first use this function)|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|16|error: (Each undeclared identifier is reported only once for each function it appears in.)|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|20|error: `year' cannot be used as a function|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|28|error: `year' cannot be used as a function|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|34|warning: statement has no effect|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|36|error: expected primary-expression before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|36|error: expected `;' before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|38|warning: statement has no effect|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|40|error: expected primary-expression before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|40|error: expected `;' before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|42|warning: statement has no effect|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|44|error: expected primary-expression before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|44|error: expected `;' before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|46|warning: statement has no effect|
||=== Build finished: 11 errors, 4 warnings ===|


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
#include <iostream>

using namespace std;

int main()
{
    int year, not_leap_year, is_leap_year;

    cout << "Please enter a year:";
    cin >> year;

    if (year (4 == 0))
    {
        cout << "Do something";
    }
        then;
        {
            cout << "Do something";
        }
       if (year (100 == 0))
       {
            cout << "Do something";
       }
           then;
           {
                cout << "Do something";
           }
               if (year (400 == 0))
               {
                    cout << "Do something";
               }
                   then;
                   {
                       is_leap_year;
                   }
               else;
               {
                   not_leap_year;
               }
        else;
        {
            is_leap_year;
        }
    else;
    {
        not_leap_year;
    }
    return 0;
}
Last edited on
Cleaned up the code more and got less errors. My if statements weren't properly enclosed.

Here's what I have so far anyone?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp||In function `int main()':|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|12|error: `year' cannot be used as a function|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|15|error: `then' undeclared (first use this function)|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|15|error: (Each undeclared identifier is reported only once for each function it appears in.)|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|19|error: `year' cannot be used as a function|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|26|error: `year' cannot be used as a function|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|32|warning: statement has no effect|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|34|error: expected primary-expression before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|34|error: expected `;' before "else"|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|36|warning: statement has no effect|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|42|warning: statement has no effect|
C:\Users\Will\Documents\C++ Projects\LeapYear\main.cpp|48|warning: statement has no effect|
||=== Build finished: 7 errors, 4 warnings ===|


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
#include <iostream>

using namespace std;

int main()
{
    int year, not_leap_year, is_leap_year;

    cout << "Please enter a year:";
    cin >> year;

    if (year (4 >= 0))
    {
        cout << "Do something";
        then;
        {
            cout << "Do something";
        }
       if (year (100 >= 0))
       {
           cout << "Do something";
           then;
           {
                cout << "Do something";
           }
               if (year (400 >= 0))
               {
                   cout << "Do something";
                   then;
                   {          
                       is_leap_year;
                       cout << year << " is a leap year.";
                   }
                    else;
                    {
                        not_leap_year;
                        cout << year << " is not a leap year.";
                    }
                }
            else;
            {
                is_leap_year;
                cout << year << " is a leap year.";
            }
        }
        else;
        {
            not_leap_year;
            cout << year << " is not a leap year.";
        }
     }

     cout << "The program will now close";

    return 0;
}
Last edited on
The modulo operator isn't (.
There's no "then" keyword.
The semicolons after else don't belong there.
Just writing a variable name on its own like "is_leap_year" doesn't actually do anything.
closed account (10oTURfi)
This is why I refused to learn pseudo programing language in primary school at computer science classes.
I knew shit is gonna happen if I do.

Never. Ever. Learn pseudo language.
its EVIL!

edit: it gets you kicked out from classes, but it was sure worth it ;)
Last edited on
@Athar alright well I'm not quite sure how to implement that into my code can you help me out?

@Krofna Yeah I understand the normal C++ stuff, but this pseudo stuff I don't get my teacher has taught us nothing about it and he's like here read this wiki page and make a leap year program on top of 3 other programs due tomorrow. It's bs. That's why I'm asking for help I don't really get it. I'm not asking for a hand out, I've shown progress throughout this post. I'm just a little stuck and it's frustrating as hell.
Last edited on
but this pseudo stuff I don't get my teacher has taught us nothing about it

Because there's nothing to teach. There are no rules. It isn't a well-defined language, it's just some mix between English and something that looks like a programming language and is sometimes used to describe algorithms.
Last edited on
@Krofna: If you can't use your head from pseudo code to any programming language, you shouldn't be programming. Direct code for simple problems on this web site might be a class given project and most of us will not help but in pseudo code. This process is to learn how program the logic yourself. This is how I learned programming back when I was 12 and I didn't start in C++, just now have quite a bit in C++ now.

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
// pseudo code translation.
int main()
{
    int year; 
    bool leap_year = false 

    //if (year modulo 4 is 0)
      if( !(year % 4) )
     {
        // then;
        //  if (year modulo 100 is 0)
          if( !(year %100) )
          {
              // then;
              // if (year modulo 400 is 0)
                  if( !(year % 400) )
                  {
                       leap_year = true;
                  }
           }
    }

    if(leap_year)
    {
          std::cout << year << " is a leap year!" << std::endl;
    }
    else
    {
         std::cout << year << " is not a leap year!" << std::endl;
    }

    return 0;
}
Last edited on
Thanks Azagaros,

It makes much more sense now. Sometimes all it takes is a good example or some help and people can totally learn from it. I got it working and it was much easier than I thought. I over looked stuff, thanks for showing me.
A more direct translation is this:
if (year%4==0)

And here's the collapsed version:
bool leap_year=year%4==0 && (year%100!=0 || year%400==0);
closed account (10oTURfi)
@azagaros
Pseudo code is not pure english (read: croatian). Here in croatia it has some srs syntax and rules. Some is generaly correct, but most of it is wrong for any computer language.

I can translate pseudo code to c++ only beacuse I know c++. I was talking about other way around.

When I think about program I will write. It sounds nothing like the pseudo language from books. It pretty much sounds like mix of croatian, english with pieces of C++ syntax , NOT pseudo syntax

Pseudo code does 1 thing correctly: the obvious stuff such as if else and do while, and teaches you bad syntax. If you need pseudo code to teach you that....
Last edited on
Topic archived. No new replies allowed.