0

I'm just get started learning C++.

And I'm trying to write code that prints out the last date of the month from given the year and month. Leap years have to be considered.

I think I do something wrong.

If I put 2020 then 2 output should be 29.

Or I put 2018 then 4 output should be 30.

Please give me any kind of hint.

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int main()
{
    int loopNum;

    ifstreeam infile("input2.txt");

    infile >> loopNum;
    for (int i = 0; i < loopNum; i++) {
        int year;
        infile >> year;
        
        int month;
        infile >> month;
        

        if (year) = year % 4 == 0 or year % 400 == 0;
            if month == 2
                cout << "29" << endl;

        else if (month) = 2
            cout << "28" << endl;
            else if (month) = 1, 3, 5, 7, 8, 10, 12
                cout << "31" << endl;
            else 
                cout << "30" << endl;
    }

    infile.close();

    return 0;

}

Tuvshin
  • 21
  • 3
  • 2
    *Please give me any kind of hint.* - [Use a debugger](https://stackoverflow.com/a/68159325). – Evg Sep 14 '21 at 07:08
  • 2
    Why do you think you did something wrong? What input do you have, output you get, and output you expected? – ShadowMitia Sep 14 '21 at 07:09
  • @Evg is my way of thinking correct? – Tuvshin Sep 14 '21 at 07:09
  • 2
    I don't know, you wrote nothing about your way of thinking. The code in your question doesn't even compile. – Evg Sep 14 '21 at 07:10
  • Welcome! Yup, ad @Evg says, debugger, debugger, debugger (after it compiles & links, of course). Knowing how to use the debugger cold prevent a large portion of the questions on this site. Also, there's [more to leap years](https://www.wikihow.com/Calculate-Leap-Years) than your code handles. E.g "If a year is divisible by 100, but not 400, then it is not a leap year. If a year is divisible by both 100 and 400, then it is a leap year" ---> – Mawg says reinstate Monica Sep 14 '21 at 07:24
  • --- > When your code works, post it to our [code review](https://codereview.stackexchange.com/) site for tips from more experienced programmers on how to improve it. Good luck. Also, please read [ask] – Mawg says reinstate Monica Sep 14 '21 at 07:25

1 Answers1

2

There are so many syntax errors in this code, that's why it might not even compile. I'm assuming you're coming from Python background, because a lot is different in C++.

Such as:

  1. Your whole if statement needs to be enclosed in brackets.
  2. When comparing use "==", instead of "=".
  3. Your if and else statements don't have "{" and "}" enclosing the code that is to be executed.

Other than that, the logic applied seems to be good. Just fix the syntax, and hopefully it will start working like a charm.

Brookie_C
  • 427
  • 2
  • 10