当我扫描一个数组时,它会更改另一个数组吗? intmain() { 字符单词[]=“大象”; 字符显示[]=“*******”; 整数大小; 整数机会=10; 字符选择[1]=“a”; int i=0; int-indi=0; 字符选择_1; 大小=strlen(字); printf(“您想猜单词[w]还是猜字母[l]:”; scanf(“%c”和选项1); 而(机会>0) { i=0; 如果(选项1=='w') { printf(“输入单词:”); scanf(“%s”,选项); 如果(strlen(选项)!=尺寸) { printf(“输入字母数正确的单词\n”); } if(strlen(选项)=大小) { 而
我用“w”表示“choice_1”,用“elephank”表示“choice[]”当我扫描一个数组时,它会更改另一个数组吗? intmain() { 字符单词[]=“大象”; 字符显示[]=“*******”; 整数大小; 整数机会=10; 字符选择[1]=“a”; int i=0; int-indi=0; 字符选择_1; 大小=strlen(字); printf(“您想猜单词[w]还是猜字母[l]:”; scanf(“%c”和选项1); 而(机会>0) { i=0; 如果(选项1=='w') { printf(“输入单词:”); scanf(“%s”,选项); 如果(strlen(选项)!=尺寸) { printf(“输入字母数正确的单词\n”); } if(strlen(选项)=大小) { 而,c,arrays,C,Arrays,我用“w”表示“choice_1”,用“elephank”表示“choice[]” 这就是你的问题。选择是一个字符(“数组”大小为1),因此您只能存储一个字符。每一个字符都存储在choice旁边,choice保留了用于显示的内存。读一本C语言书!您会混淆字符、字符数组和C字符串。启用编译器警告并注意它们。您的编译器想对您大喊大叫。char choice[1]=“a”;无法容纳您猜测的字符串。它是一个没有终止符的单字符a。您将字符串输入一个单字节分配,它会破坏其他变量。这里您看到的是Olaf,而不
这就是你的问题。选择是一个字符(“数组”大小为1),因此您只能存储一个字符。每一个字符都存储在choice旁边,choice保留了用于显示的内存。读一本C语言书!您会混淆字符、字符数组和C字符串。启用编译器警告并注意它们。您的编译器想对您大喊大叫。
char choice[1]=“a”;
无法容纳您猜测的字符串。它是一个没有终止符的单字符a
。您将字符串输入一个单字节分配,它会破坏其他变量。这里您看到的是Olaf,而不是给@Kevin提供答案,您只需说他对cahr数组和C-Strings一无所知。我记得我将字符数组和指针读了10遍掌握它们谢谢你们的帮助伙计们我刚改变了它的选择[strlen(单词)],一切都很好。
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;
}