English 中文(简体)
C 中环环和字符
原标题:for loop and char in C

我在试着学习C语言 我需要一些帮助

我正在尝试制作一个 tic-tac-toe 游戏的程序。 我尝试在主函数中做第一点, 但是它没有起作用。 但是, 当我使用另一个函数时, 它就起作用了。 它是如何和为什么起作用的? (两个程序都附在下面) 。

Also, I used "%s" instead of "%c" in scanf with "square 1" and "square 2". Why doesn t "%c" work fine in here ?

提前感谢您的帮助。

// 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
");  


scanf("%i", &player); 

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


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


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 
"
                   "----+-----+--- 
"
                   "%c  +  %c + %c 
"
                   "----+-----+--- 
"
                   "%c  +  %c + %c 
", 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 
"
                   "----+-----+--- 
"
                   "%c  +  %c + %c 
"
                   "----+-----+--- 
"
                   "%c  +  %c + %c 
", 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 
"
               "----+-----+--- 
"
               "%c  +  %c + %c 
"
               "----+-----+--- 
"
               "%c  +  %c + %c 
", 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
");  

scanf("%i", &player); 

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


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



 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 
"
               "----+-----+--- 
"
               "%c  +  %c + %c 
"
               "----+-----+--- 
"
               "%c  +  %c + %c 
", Nesta[0],Nesta[1], Nesta[2], Nesta[3],Nesta[4],Nesta[5],Nesta[6],Nesta[7],Nesta[8]);

gamecont() ;   
            }
}
  }
 return 0;
}
问题回答

当你说“不工作”的时候,你需要清楚。难道不工作在什么意义上吗?计算机融化了?还是发生了你没有预料到的具体的事情?那么具体一点。

WRT %c vs %s, keep in mind that when you enter data from the console you need to hit "enter", which feeds a character to the stdin buffer. That is still in the input buffer, waiting to be read by the next scanf.

当您使用 时, 如果在缓冲中出现新线, 则会跳过该线, 因为 字符串不包括白空, 而大多数类型的指定符都会跳过领先的白空 。 但是, 总是一个单字符 。 考虑 :

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

Everytime you enter a character (and hit enter) you ll get two numbers, the second of which is 10 (ascii ).

不幸的是,你对 %s 的使用不安全。请尝试一下:

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

表示忽略, 因此这将删除让标准缓冲为空的新线。 但是, 如果用户输入多个字母( 或一个空格), 仍然会留下一些东西 。 您可以将 < code> quare 变成字符串, 仅使用第一个字符, 但该字符也有陷阱。 一种强大的冲洗输入缓冲的方法是 :

while (fgetc(stdin) !=  
 );

如果你在使用扫描仪后这样做,它就会在抓获后吃掉缓冲中的任何东西,留作下次使用。

在第一个代码倾弃处,您会用 %s 扫描 C 字符串到字符变量( 方形1 和方形2)。 这是不安全的, 您应该扫描 C 字符串到字符数组, 而不是单个字符组 。





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签