c - 当我扫描一个数组时,它会更改另一个数组吗?

标签 c arrays

int main()
{

char word[]="elephant";
char display[]="********";
int size;
int chance=10;
char choice[1]="a";
int i=0;
int indic=0;
char choice_1;


size=strlen(word);


printf("Would you like to guess the word [w] or guess a letter [l]:");
scanf("%c", &choice_1);

while(chance>0)
{


i=0;

if(choice_1=='w')
{

printf("Enter the word:");
scanf(" %s", choice);

    if(strlen(choice)!=size)
    {

    printf("Enter a word with the correct number of letters\n");

    }

    if(strlen(choice)==size)

    {

        while(i<size)
        {

        if(choice[i]==word[i])
        {

        indic++;   

        }

        i++;

        }



    if(indic==size)
    {
        printf("You have guessed the word!\n");
        break;
    }

    if(indic!=size)
    {
        printf("You have incorrectly guessed the word\n");
        chance=chance-1;
    }

    }

}

i=0;

if(choice_1=='l')
{

printf("Enter a letter:");
scanf(" %s", choice);

i=0;
indic=0;

    while(i<=size)
    {

    if(choice[0]==word[i])
    {

    display[i]=word[i];
    indic=1;

    }

    i++;

    }

i=0;

if(indic==0)
{

printf("BAD CHOICE!\n");
chance=chance-1;

    while(i<=size)
    {

        if(i==size)
        {
        printf("%c\n", display[i]);
        }

    printf("%c", display[i]);
    i++;

    }

}

if(indic==1)
{

printf("GOOD CHOICE!\n");

    while(i<=size)
    {

        if(i==size)
        {
        printf("%c\n", display[i]);
        }

    printf("%c", display[i]);
    i++;

    }

}

}



printf("You have %d chances left\n", chance);

printf("Would you like to guess the word [w] or guess a letter [l]:");
scanf(" %c", &choice_1);

}

return 0;

}

我正在尝试为大学构建一个刽子手程序,我今年才开始编程。但我遇到了这个问题。当我将输入放入数组“choice[]”时,它也会由于某种原因更改数组“display[]”。例如,如果我为“choice_1”输入“w”,为“choice[]”输入“elephank” "(应该是大象)数组“display[]”更改为“lephank”。为什么要这样做?我什至不使用该循环中的显示数组来检查该单词是否正确。我知道这是一大段代码,但因为我不知道发生了什么 我不知道还能做什么。任何帮助是极大的赞赏。

最佳答案

I put in 'w' for "choice_1" and 'elephank' for " choice[]"

那是你的问题。 choice 是一个 char(大小为 1 的“数组”),因此只能存储一个字符。每个进一步的字符都存储在选择旁边,其中内存被保留用于显示

关于c - 当我扫描一个数组时,它会更改另一个数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33596617/

相关文章:

java - 无法访问数组对象内子类中的数据

javascript - 如何在javascript中对多维数组进行排序

c - 我的 malloc/realloc 有什么问题吗?

c - C 中的高斯消元法

c++ - 任何用于分析 SVG 路径的 C/C++ 库?

php - 如果图像数组为空,则显示替代图像

c++ - 在编译时对静态数组的元素求和

ios - 如何在 Swift 中为 UIImages 创建闪烁的指针效果

c++ - 在 C++ 中读取 COM 端口数据

c - 使用 g_malloc0 后释放内存