for循环和C中的char

时间:2012-05-28 09:41:34

标签: c string for-loop char

我正在尝试学习C语言,我需要一些帮助。

我正在尝试为井字游戏制作一个程序。我尝试在main函数中做第一部分,但它没有用。但是,当我使用其他功能时,它起作用了。它是如何以及为什么有效? (这两个程序都附在下面)。

另外,我在scanf中用“%s”代替“%c”,“square 1”和“square 2”。 为什么“%c”在这里不能正常工作?

提前感谢您的帮助。

// first program


#include <stdio.h>  

int main ()

{

int player;                                         
char square1; 
char square2;
char Nesta[9]={'1','2','3','4','5','6','7','8','9'}; 
int player1;
int player2;


printf("choose one for x or two for o\n");  


scanf("%i", &player); 

if (player==1) 
{
    player1='x';
    player2='o';
}
else
{
    player1 ='o';
    player2='x';
}


printf(
       "1  +  2 + 3   \n"
       "---+----+-----\n"
       "4  +  5 + 6   \n"
       "---+----+-----\n"
       "7  +  8 + 9   \n"
       );


for (int i=0; i<3; ++i)
{
    printf("please enter the number of the sqaure "); 
    scanf("%s",&square1 );

    printf("please enter the number of the sqaure "); 
    scanf("%s",&square2 );

    for (int j=0; j<9; ++j)
    {
        if (square1 == Nesta[j])

        {
            Nesta[j]=player1;
            printf(
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],Nesta[5],Nesta[6],Nesta[7],Nesta[8]);
        }
    }


    for (int k=0; k<9; ++k)
    {
        if (square2 == Nesta[k])

        {
            Nesta[k]=player2;
            printf(
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n"
                   "----+-----+--- \n"
                   "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],
                                         Nesta[5],Nesta[6],Nesta[7],Nesta[8]);
         }

       }
     }
      return 0; 
   }

使用gamecont函数时效果很好!!

// the second program
#include <stdio.h> 

char Nesta[9]={'1','2','3','4','5','6','7','8','9'}; 
int player1;
int player2;

int gamecont ()
{

char square2;

printf("please enter the number of the square "); 
scanf("%s",&square2 ); 

for (int j=0; j<9; ++j)

    if (square2 == Nesta[j])
      {
        Nesta[j]=player2;
        printf(
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],
                                    Nesta[5],Nesta[6],Nesta[7],Nesta[8]);
        }
    }


int main ()
{

int player;                                         
char square1; 

printf("choose one for x or two for o\n");  

scanf("%i", &player); 

if (player==1) 
{
    player1='x';
    player2='o';
}
else
{
    player1 ='o';
    player2='x';
}


printf(
       "1  +  2 + 3   \n"
       "---+----+-----\n"
       "4  +  5 + 6   \n"
       "---+----+-----\n"
       "7  +  8 + 9   \n"
       );



 for (int i=0; i<3; ++i)
 {
 printf("please enter the number of the square  "); 
 scanf("%s",&square1 ); 

 for (int j=0; j<9; ++j)
 {
   if (square1 == Nesta[j])
    {  
        Nesta[j]=player1;

        printf(
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n"
               "----+-----+--- \n"
               "%c  +  %c + %c \n", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],Nesta[5],Nesta[6],Nesta[7],Nesta[8]);

gamecont() ;   
            }
}
  }
 return 0;
}

2 个答案:

答案 0 :(得分:2)

当你说“不起作用”时,你需要清楚。在什么意义上不起作用?电脑融化了吗?或者某些特定的事情发生了你没想到的?然后具体。

WRT %c vs %s,请记住,当您从控制台输入数据时,需要点击“输入”,这会将“\ n”字符输入到stdin缓冲区。 那个'\ n'仍然在输入缓冲区中,等待下一个scanf读取。

使用%s时,如果缓冲区中有换行符,则会跳过该换行符,因为%s字符串不包含空格,大多数类型说明符会跳过前导空格。但是,%c始终是单个字符。考虑:

char c;
while (scanf("%c", &c)) printf("%d\n", c);

每当你输入一个字符(然后按回车键),你将得到两个数字,其中第二个是10(ascii \n)。

不幸的是,您使用%s是不安全的。试试这个:

scanf("%c%*c", &square);

*表示忽略,因此这将删除新行,使stdin缓冲区为空。但是,如果用户输入多个字母(或空格),仍会留下一些东西。你可以让square成为一个字符串,只使用第一个字符,但这也有陷阱。刷新输入缓冲区的一种有效方法是:

while (fgetc(stdin) != '\n');

如果你在使用scanf之后这样做,它会在捕获后在缓冲区中吃掉任何东西,并在下次清除它。

答案 1 :(得分:0)

在第一个代码转储中,将带有%s的c字符串扫描到字符变量(square1和square2)。这是不安全的,您应该将c字符串扫描到字符数组,而不是单个字符。