英文:
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 <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:
terminal:
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='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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论