我在Mac终端上运行程序时遇到了一些问题。

huangapple go评论65阅读模式
英文:

i have some problem when i running program on mac terminal

问题

以下是代码的翻译部分:

我是一个初学者。我创建了一个小程序:

#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;
}

代码部分不翻译,如您所要求。关于代码的问题,您在终端中运行时出现不同的结果可能是因为在不同的环境下(例如Xcode和终端)编译器和运行时的行为有所不同。此外,日期计算和星期计算也可能会受到系统时间和区域设置的影响。如果代码在不同环境中表现不同,您可能需要进一步检查和调试代码以找出问题所在。

英文:

I am a beginner. I make a little program:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdbool.h&gt;
#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=&#39;e&#39;;
bool DoYouWantToRestart = true;
printf(&quot;day searching system (2023)\n&quot;);
while (DoYouWantToRestart == true)
{
k=0;
month=0;date=0;
restart = &#39;e&#39;;
day=0;week=0;
printf(&quot;what month is it:&quot;);
scanf(&quot;%d&quot;,&amp;month);
printf(&quot;what date is it:&quot;);
scanf(&quot;%d&quot;,&amp;date);
printf(&quot;loading......\n&quot;);
if(month&lt;1 || month&gt;12)
printf(&quot;error!\n&quot;);
else if (date&gt;31)
printf(&quot;error!\n&quot;);
else if ((month==4 || month==6 || month==9 || month==11) &amp;&amp; date&gt;30)
printf(&quot;error!\n&quot;);
else if (month==2 &amp;&amp; date&gt;28)
printf(&quot;error!\n&quot;);
else
{
for(k=0;k&lt;month-1;k++)
{
day=day+cmonth[0+k];
}
day=day+date;
week = day % 7 - 1;
switch(week)
{
case 0:
printf(&quot;%d/%d is on Sunday\n&quot;,month,date);
break;
case 1:
printf(&quot;%d/%d is on Monday\n&quot;,month,date);
break;
case 2:
printf(&quot;%d/%d is on Tuesday\n&quot;,month,date);
break;
case 3:
printf(&quot;%d/%d is on Wednesday\n&quot;,month,date);
break;
case 4:
printf(&quot;%d/%d is on Thursday\n&quot;,month,date);
break;
case 5:
printf(&quot;%d/%d is on Friday\n&quot;,month,date);
break;
case 6:
printf(&quot;%d/%d is on Saturday\n&quot;,month,date);
break;
}
}
while(restart == &#39;e&#39;)
{
printf(&quot;restart?(y/n):&quot;);
scanf(&quot;%s&quot;,&amp;restart);
if(restart == &#39;y&#39; || restart == &#39;Y&#39;)
DoYouWantToRestart=true;
else if (restart == &#39;n&#39; || restart == &#39;N&#39;)
DoYouWantToRestart=false;
else
{
printf(&quot;error\n&quot;);
restart = &#39;e&#39;;
}
}
}
printf(&quot;Have a good day!\n&quot;);
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

得分: 2

char restart[] = "e";  `scanf(" %c", &restart);` 是错误的。 使用 `%s`,`scanf` 将一个字符串放入目标地址,包括一个终止空字符。 当输入是“y”时,`scanf` 将一个“y”和一个空字符写入给定的地址。 这超出了 `restart` 的空间,因为它只被定义为一个单字符。

显然这会破坏你的 `cmonth` 数组,之后代码会将0添加到1月份,而不是31

你需要将 `restart` 改为一个具有更多空间的数组,或者将 `scanf` 更改为只存储一个单个字符。 将其改为 `scanf(" %c", &restart);` 将使其在跳过空白字符(如之前输入的换行符)后只读取一个字符。 请注意,引号字符串内部的空格很重要;这是告诉 `scanf` 在处理 `%c` 之前跳过空白字符的方式。

Xcode 测试和命令行测试之间的区别可能在于编译时使用的优化级别。
英文:

char restart=&#39;e&#39;; with scanf(&quot;%s&quot;,&amp;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(&quot; %c&quot;, &amp;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.

huangapple
  • 本文由 发表于 2023年6月1日 23:46:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定