#whats wrong with my code

1 messages · Page 1 of 1 (latest)

vocal python
#

day is getting saved to the value of 19 for some reason // lab 7 oct 2023
// exercise 21
//
//
//***********************
#include <iostream>
using namespace std;
int day;
int x;
int daynumber, month, year, j;
bool leapyear;

void getinput();
void leapyearfunc();
void dayofyear();

int main() {
day = 1;
j = 12;
getinput();
dayofyear();
leapyearfunc();

return 0;

}
void getinput() {
cout << " enter the month day year in this format (month day year) " << endl;

cin >> month >> daynumber >> year;
return;
        }

void dayofyear() {

j = 12;
if (month == j)
    day = 365 - daynumber;

else
    j--;
if (month == j)
    day = 334 - daynumber;
else
    j--;
if (month == j)
    day = 304 - daynumber;
else
    j--;
if (month == j)
    day = 273 - daynumber;
else j--;
if (month == j)
    day = 243 - daynumber;
else
    j--;
if (month == j)
    day = 212 - daynumber;
hearty sigilBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

vocal python
#

continuation

hearty sigilBOT
#

day is getting saved to the value of 19 for some reason ```cpp
// lab 7 oct 2023
// exercise 21
//
//
//***********************
#include <iostream>
using namespace std;
int day;
int x;
int daynumber, month, year, j;
bool leapyear;

void getinput();
void leapyearfunc();
void dayofyear();

int main() {
day = 1;
j = 12;
getinput();
dayofyear();
leapyearfunc();

return 0;
}
void getinput() {
cout << " enter the month day year in this format (month day year) " << endl;

cin >> month >> daynumber >> year;
return;
}

void dayofyear() {
j = 12;
if (month == j)
day = 365 - daynumber;

else
j--;
if (month == j)
day = 334 - daynumber;
else
j--;
if (month == j)
day = 304 - daynumber;
else
j--;
if (month == j)
day = 273 - daynumber;
else
j--;
if (month == j)
day = 243 - daynumber;
else
j--;
if (month == j)
day = 212 - daynumber;

jacob
gaunt finch
#

!f

hearty sigilBOT
#

else
j--;
```cpp
if (month == j)
day = 181 - daynumber;
else
j--;
if (month == j)
day = 151 - daynumber;
else
j--;
if (month == j)
day = 120 - daynumber;
else
j--;
if (month == j)
day = 90 - daynumber;
else
j--;
if (month == j)
day = 59 - daynumber;
else
j--;
// if its not a leap year if it is +1
if (month == j)
day = 31 - daynumber;

else
j--;

return;
}
void leapyearfunc() {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
leapyear = true;
else
leapyear = false;

if (leapyear == true) {
cout << "Its a leap year, woo! " << endl;
x = day + 1;
cout << "The day number of your inputted date is: " << x << endl;
} else
cout << "its not a leap year, aww " << endl;
x = day;
cout << "The day number of your inputted date is: " << x << endl;

return;
}

jacob
gaunt finch
# vocal python day is getting saved to the value of 19 for some reason // lab 7 oct 2023 // ex...

Ohh, okay, I thought one thing, but it's actually another. First, learn to debug your program, always use the debugger, it's faster to spot where your code went wrong than to ask here. I was going to complain about your void functions, but it isn't an issue because all your variables are global (well, in fact, that's a separate issue that I won't address here). If I'm assuming correctly that dayofyear() is supposed to calculate the total amount of days that passed, and thus what you wanted for the date 12/12/2002 is 353, your problem has to do with how you made the if-else chain...

 j = 12;
  if (month == j)
    day = 365 - daynumber;

In date 12/12/2002, this is regarded as true, passing on to the next statement:

else
    j--;

This won't be executed because month == j and the if was executed, hence j will not decrease at all. Next statement:

  if (month == j)
  ...

The same condition, and remember, month is 12 and j remains 12, so this is true.

Conclusion: the else conditions are never true (soj never changes), and the if conditions are always true (j == month), so day will be decreased until it reaches day = 31 - daynumber which will equal 19 for your given date.

gaunt finch
#

And just to be clear that this isn't a problem with the month being 12, all other months except January won't work. If you have month 6, for example, the code will work as intended until the 6th if condition, afterwards, the same bug happens: for every if condition afterwards month will be equal j and day will be decreased more than you want it to.