0

I am a beginner. I make a little program:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define BOOL bool
int main(void)
{
    int month, date, day = 0, week = 0;
    int Jan=31, Feb=28, Mar=31, Apr=30, May=31, Jun=30, Jul=31, Aug=31, Sep=30, Oct=31, Nov=30, Dec=31;
    int cmonth[12]={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
    int k;
    char restart='e';
    bool DoYouWantToRestart = true;
    
    printf("day searching system (2023)\n");
    while (DoYouWantToRestart == true)
    {
        k=0;
        month=0;date=0;
        restart = 'e';
        day=0;week=0;
        printf("what month is it:");
        scanf("%d",&month);
        printf("what date is it:");
        scanf("%d",&date);
        printf("loading......\n");
        
        if(month<1 || month>12)
            printf("error!\n");
        else if (date>31)
            printf("error!\n");
        else if ((month==4 || month==6 || month==9 || month==11) && date>30)
            printf("error!\n");
        else if (month==2 && date>28)
            printf("error!\n");
        else
        {
            for(k=0;k<month-1;k++)
            {
                day=day+cmonth[0+k];
            }

            day=day+date;
            week = day % 7 - 1;
            
            switch(week)
            {
                case 0:
                    printf("%d/%d is on Sunday\n",month,date);
                    break;
                case 1:
                    printf("%d/%d is on Monday\n",month,date);
                    break;
                case 2:
                    printf("%d/%d is on Tuesday\n",month,date);
                    break;
                case 3:
                    printf("%d/%d is on Wednesday\n",month,date);
                    break;
                case 4:
                    printf("%d/%d is on Thursday\n",month,date);
                    break;
                case 5:
                    printf("%d/%d is on Friday\n",month,date);
                    break;
                case 6:
                    printf("%d/%d is on Saturday\n",month,date);
                    break;
            }
        }
        
        while(restart == 'e')
        {
            printf("restart?(y/n):");
            scanf("%s",&restart);
            if(restart == 'y' || restart == 'Y')
                DoYouWantToRestart=true;
            else if (restart == 'n' || restart == 'N')
                DoYouWantToRestart=false;
            else
            {
                printf("error\n");
                restart = 'e';
            }
        }
    }
    printf("Have a good day!\n");
    return 0;
}

It can work when i use xcode, but not in terminal

xcode:

enter image description here

terminal:

enter image description here

I have tried some online C program tools, it's also works. But not work at Mac terminal.

The correct answer is Thursday, but when i do second time on terminal, it shows Monday.

Is it a bug or i made any mistake on it?

1 Answers1

2

char restart='e'; with scanf("%s",&restart); is wrong. With %s, scanf puts a string in the destination, including a terminating null character. When the input is “y”, scanf writes a “y” and a null character to the address given. That overruns the space for restart because it is defined to be only a single character.

Apparently that is corrupting your cmonth array, after which the code adds 0 for January instead of 31.

You need to change restart to be an array with more space in it or change the scanf to store only a single character. Changing it to scanf(" %c", &restart); will change it to read only a single character after white-space characters (such as the new-line from preceding input) are skipped. Note the space inside the quoted string is important; that is what tells scanf to skip white-space before the %c is processed.

The difference between the Xcode test and the command-line test might be in optimization levels used in compilation.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Thank you! It’s really works but a little hard too understand lol. – 臺灣魚Taiwan Fish Jun 01 '23 at 16:23
  • 1
    @臺灣魚TaiwanFish What's hard to understand is `scanf`, which is simultaneously the easiest — *seemingly* easiest — and hardest function for beginners to use. It's got all sorts of traps, foibles, and pitfalls. You might be interested in [this list](https://stackoverflow.com/questions/72178518#72178652) (especially points 3 and 8). – Steve Summit Jun 01 '23 at 16:24