Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 字符串值=";“粘土”;自动更改为"/千层;?_C - Fatal编程技术网

C 字符串值=";“粘土”;自动更改为"/千层;?

C 字符串值=";“粘土”;自动更改为"/千层;?,c,C,我正试图用word版本制作一个C程序 这是我的密码:- #include<stdio.h> #include<conio.h> void main() { char word[4],guess[4]; int i,j,check=0,b=0,c=0,d[4]; system("cls"); printf("Welcome to the Cows and Bulls game!\n\nEnter a 4 letter word to be g

我正试图用word版本制作一个C程序

这是我的密码:-

#include<stdio.h>
#include<conio.h>
void main()
{
    char word[4],guess[4];
    int i,j,check=0,b=0,c=0,d[4];
    system("cls");
    printf("Welcome to the Cows and Bulls game!\n\nEnter a 4 letter word to be guessed :- ");
    scanf("%s",word);
    while(check==0)
    {
        printf("\nEnter your guess :- ");
        scanf("%s",guess);
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                if(i==j && word[i]==guess[j])
                {
                    b++;
                    d[j]=j;
                    break;
                }
                else if(word[i]==guess[j] && j!=d[j])
                {
                    c++;
                    break;
                }
            }
        }
        printf("%d cow(s) and %d bull(s)!\n",c,b);
        if(b==4)
        {
            printf("Congratulations! You have correctly guessed %s",word);
            check=1;
        }
        else
        {
            check=0;
        }
    }
    getch();
}
#包括
#包括
void main()
{
字符词[4],猜测[4];
int i,j,check=0,b=0,c=0,d[4];
系统(“cls”);
printf(“欢迎参加牛和牛的游戏!\n\n输入一个4个字母的单词以供猜测:-”);
scanf(“%s”,单词);
while(检查==0)
{
printf(“\n输入您的猜测:-”;
scanf(“%s”,猜测);

对于(i=0;i您不在末尾为NUL终止符(
'\0'
)保留空间,当输入大于或等于4个字符时,最终通过
scanf
调用未定义的行为。更改

char word[4],guess[4];


其他建议和意见:

  • system
    需要
    stdlib.h
    。包括它
  • conio.h
    (和
    getch
    )是非标准的
  • void main()
    不是
    main
    的标准签名。请改用
    int main(void)
    。您还需要在
    main
    末尾添加
    返回0;
    (这在C99+中是隐式的)
  • 不要使用scanf(“%s”,word);
,而是使用

if(1 != scanf("%4s", word)) /* Check if `scanf` is successful; The `4` prevents buffer overflows */
{
    fputs("scanf for word failed; Exiting...", stderr)
    exit(-1);
}
同样,对于
scanf
guess

  • 最好初始化所有变量

  • 我说你是救命恩人,是上帝!非常感谢:)但是为什么guess变量没有出现这种情况呢?@MihirKandoi Undefined Behavior意味着任何事情都可能是结果。当UB攻击时,你不能确定任何事情。@MihirKandoi你在使用turboC IDE吗?它看起来像
    单词
    数组直接位于
    猜测
    数组之后,当你读取猜测单词时,它就会写入s缓冲区
    的第一个字节中的空终止符。[sig]另一个bean计数器:)除非您在ram不足的嵌入式环境中,否则不要为文本声明小缓冲区。只需将[256]作为最小值。
    
    if(1 != scanf("%4s", word)) /* Check if `scanf` is successful; The `4` prevents buffer overflows */
    {
        fputs("scanf for word failed; Exiting...", stderr)
        exit(-1);
    }