Alan G
Alan G

Reputation: 1

What is wrong with my loop? I've tried everything

I'm doing a final project right now and I have the following code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

int main(void){
int day;
int year;
int month;
int daynum=0;
int days=365;
printf("\nPlease input what year you would like to meet\n");
scanf("%d",&year);
printf("\nPlease input what month you would like to meet\n");
scanf("%d",&month);
printf("\nPlease input what day you would like to meet\n");
scanf("%d",&day);

//Later in the code i have the following loop

int k=month;

do {
    k--;
    switch (k) {
        case 0:
        case 2:
        case 4:
        case 6:
        case 7:
        case 9:
        case 11:
            daynum=daynum+31;
            printf("\nadded 31");
            break;
        case 1:
            if (days==365) {
                daynum=daynum+28;
            }
            else if (days==366) {
                daynum=daynum+29;
            }
            printf("\nadded 28/29");
            break;
        case 3:
        case 5:
        case 8:
        case 10:
            daynum=daynum+30;
            printf("\nadded 30");
            break;
        default: 
            printf("\nsomething went wrong");
    }
}while(k>=0);
return 0;
}

I've tried essentially the same thing with a while and for loop but every time the only thing that shows up in the console is something went wrong. I even tried checking the value of k before the loop and it says it is equal to month. I've also tried setting k equal to a number and it works just fine with that, but i need it to equal month. I really have no idea whats wrong and any help would be appreciated.

Upvotes: 0

Views: 81

Answers (3)

arfneto
arfneto

Reputation: 1765

Well, I believe you now know the error as @Barmar wrote.

Anyway I add:

  • always, I mean always, check for the return of scanf(): there is no point in letting the program go if some of the values are not read. scanf() always returns the number of specifiers read, that things that starts with a single %. In your case it will be 1, 0 or -1.
  • about this
do {
    k--;
    switch (k) {

you can always switch(--k) with the same effect.

  • you could state the purpose of the code and provide at least one test value. If you are trying to get the number of days up to and including the user supplied one, you could have made it clear. I was just guessing.

  • note that Excel or Google sheets can operate on dates so it is a good free reliable check point.
    enter image description here

  • a small change on the default label, like

            printf("\n==> daynum is %d, k is %d and something went wrong\n", daynum, k);

would have print

==> daynum is 365, k is -1 and something went wrong

and it would be very helpful. Far better than just write something went wrong. make your program work for you.

  • you do not need a switch to compute always the same values. Use a table. And as the only change in days is in February just add the extra day there as we always do...
    char days_in_month[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    days_in_month[1] += ( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0; // 1 or 0
  • use the command line for the arguments. It is a bit boring enter the program name and then 3 parameters in 3 lines just to see a resulting number.

Upvotes: 1

Alan G
Alan G

Reputation: 1

Honestly have no idea what was the issue with my code, but i moved this section of code further up and its working as intended now. Somewhere months was getting reset to 0 before the loop despite this being the first time i referenced it besides in printf statements. thank you to @dbush who helped me find out the issue.

Upvotes: 0

Barmar
Barmar

Reputation: 780994

The problem is that you're decrementing k at the beginning of the loop body, but checking it at the end of the loop. So on the last iteration you'll decrement it to -1, print the error message, and then stop the loop.

Change the condition to while(k > 0).

Upvotes: 4

Related Questions