confusion leap year problem

This is what my problem is asking "Write a program that takes in a year as input (as a command line argument) and returns the string "{year} was a leap year'' if true and "{year} was not a leap year'' if false."

I feel I need to make leapYear = to something belonging to argv[1] but I'm a bit confused on what.

I did "leapYear = atoi(argv[1]);" at first but i'm now thinking its wrong

WHAT AM I DOING RIGHT AND WHAT AM I DOING WRONG?? Please help!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
using namespace std;

int main(int argc, char*argv[]) {
   
    int leapYear;
    
           
        if (leapYear % 400 == 0 || (leapYear % 4 == 0 && leapYear % 100 != 0 ))
         
            cout << argv[1] << "was a leap year" <<endl ;
     
        else cout << argv[1] << "was not a leap" <<endl;
    return 0;                                          
}
Last edited on
> I keep getting an error code with the "leapYear = atoi(argv[1]);" and I'm confused on why
You need to tell us the actual error message.

https://www.cplusplus.com/reference/cstdlib/atoi/
For one thing, you don't include the right header file.
I reworded the question lol I did "leapYear = atoi(argv[1]);" based on help from a classmate when I asked a question previously but i'm thinking it was wrong in a way or I'm missing something along with it. I got the error code Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

**also I changed the header and still received the same error message
Last edited on
Well you also need to supply the year as an argument to the function.

> "Write a program that takes in a year as input (as a command line argument)
This much I thought you knew.

As in
myprog.exe 1984

As opposed to just typing
myprog.exe


Normally, you check argc has a suitable value (say > 1) before trying to access argv[1]
thank you for your help, I realized I did the problem correct at the beginning after I resubmitted it to my instructor. Seems like I understand it overall I was just reallllly overthinking it and freaking myself out.
Topic archived. No new replies allowed.