C 编程井字游戏错误“更多 '%' 转换多于数据参数 [-Wformat-insufficient-args]”

问题描述 投票:0回答:1

我是 C 编程新手,这是我的 f (tic tac toe) ist C 程序 [请不要活生生地吃掉我,我已经搜索并滚动但找不到,似乎找到了解决此问题的方法] 我已经遵循 C 编程教程,我不确定哪里出错了。 有时宣布获胜者,有时不宣布获胜者。有时,列中接受 4-6 的值,有时则不接受(从设置为 3 中选择的数字)。

如果有人能指出我错在哪里,我将不胜感激。

还有:

我收到以下警告

 [{
    "resource": "/Users/programming/projects/cprogrammingprojects/tic-tac-toe/tictactoe.c",
    "owner": "cpptools",
    "severity": 4,
    **"message": "more '%' conversions than data arguments [-Wformat-insufficient-args]**",
    "source": "gcc",
    "startLineNumber": 73,
    "startColumn": 13,
    "endLineNumber": 73,
    "endColumn": 13
}]

**下面是我的代码**



#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>



char board [3][3];
const char PLAYER = 'X';
const char COMPUTER = 'O';


void resetBoard();
void printBoard();
int checkFreeSpaces();
void playerMove();
void computerMove();
char checkWinner();
void printWinner(char);


int main()
{

  char winner = ' ';
  char response;

  do
  {
    winner = ' ';
    response = ' ';

    resetBoard();

    while (winner == ' ' && checkFreeSpaces() != 0)
    {
      printBoard();
    
    
      playerMove();             
      winner = checkWinner(); 
      if (winner != ' ' || checkFreeSpaces() == 0) 
      {
        break;                     
      }                   
         
    
      computerMove();           
      winner = checkWinner(); 
      if (winner != ' ' || checkFreeSpaces() == 0)  
      {
        break;                     
      }                            
    
    }
  
    printBoard();
    printWinner(winner);

    printf("\nWould you like to play again? (Y/N): ");
    scanf("%c"); 
    scanf("%c", &response);
    response = toupper(response);

  } while (response == 'Y');

  printf("THANKS FOR PLAYING!\n");


  return 0;
}



void resetBoard()
{
  for(int i=0; i< 3; i++)
  {
      for(int j=0; j< 3; j++)
    {
        board[i][j] = ' ';

    }
  }
}

void printBoard()
{

  printf(" %c | %c | %c ", board[0][0], board[0][1], board[0][2]);
  printf("\n---|---|---\n");
  printf(" %c | %c | %c ", board[1][0], board[1][1], board[1][2]);
  printf("\n---|---|---\n");
  printf(" %c | %c | %c ", board[2][0], board[2][1], board[2][2]);
  printf("\n");

}

int checkFreeSpaces()

{
  int freeSpaces = 9;
  
  for(int i=0; i<3; i++)
  {
    for(int j=0; j<3; j++)
    {
      if (board[i][j] != ' ')
      {
        freeSpaces--;
      }    
    }
  }
  
  return freeSpaces;
}


void playerMove()
{
  
  int x;
  int y;

  do
  
  {
    printf("Enter Row # (1-3): ");
    scanf("%d", &x);
    x--;

    printf("Enter Column # (1-3): ");
    scanf("%d", &y);
    y--;

    if(board [x][y] != ' ')
    {
      printf("Invalid move!\n");
    }
    else 
    {
      board[x][y] = PLAYER;
      break; 
    }

  } while (board[x][y] != ' ');
 
}

void computerMove()   
{                     
 
 srand(time(0));
 
  int x;
  int y;

  if (checkFreeSpaces() > 0)

  {

    do
    {
     x = rand() %3;
     y = rand() %3;
    } while (board[x][y] != ' ');

    board[x][y] = COMPUTER;
  }

  else
  {
    printWinner(' ');   
  }

}

char checkWinner()  
{   

  for (int i = 0; i < 3; i++)
  {
    if (board[i][0] == board[i][1] && board[i][0] == board[i][2])
    
    {
      return board[i][0];    
    }
  }

  for (int i = 0; i < 3; i++)
  {
    if (board[0][i] == board[1][i] && board[0][i] == board[2][i])
    
    {
      return board[0][i];    
    }
  }

  
    if (board[0][0] == board[1][1] && board[0][0] == board[2][2])
    
    {
      return board[0][0];    
    }
  
  {
    if (board[0][2] == board[1][1] && board[0][2] == board[2][0])
    
    {
      return board[0][2];    
    }

    return ' ';
  } 

}


void printWinner(char winner)
{

  if(winner == PLAYER)
  {
    printf("YOU WIN !\n");
  }
  
  else if(winner == COMPUTER)

  {
    printf("YOU LOSE!\n");
  }
  else
  {
    printf("IT'S A TIE\n");
  }
 
}

这是我使用该程序时的终端输出。正如您所看到的,3 以上的数字有时会被接受,有时则不会。有时也没有宣布获胜者。如果有人可以帮助我,我将从你的回复中受益匪浅。坦克你


   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 6
Enter Column # (1-3): 6
Invalid move!
Enter Row # (1-3): 3
Enter Column # (1-3): 6
Invalid move!
Enter Row # (1-3): 5
Enter Column # (1-3): 2
Invalid move!
Enter Row # (1-3): 2
Enter Column # (1-3): 7
Invalid move!
Enter Row # (1-3): 2
Enter Column # (1-3): 2
 O |   |   
---|---|---
   | X |   
---|---|---
   |   |   
Enter Row # (1-3): 3
Enter Column # (1-3): 1
 O |   |   
---|---|---
 O | X |   
---|---|---
 X |   |   
Enter Row # (1-3): 1
Enter Column # (1-3): 3
 O |   | X 
---|---|---
 O | X |   
---|---|---
 X |   |   
YOU WIN !

Would you like to play again? (Y/N): y
   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 2
Enter Column # (1-3): 4
   |   |   
---|---|---
   | O |   
---|---|---
 X |   |   
Enter Row # (1-3): 6
Enter Column # (1-3): 6
Invalid move!
Enter Row # (1-3): 3
Enter Column # (1-3): 6
Invalid move!
Enter Row # (1-3): 2
Enter Column # (1-3): 7
Invalid move!
Enter Row # (1-3): 2
Enter Column # (1-3): 5
   |   |   
---|---|---
   | O |   
---|---|---
 X | X | O 
Enter Row # (1-3): 1
Enter Column # (1-3): 1
 X | O |   
---|---|---
   | O |   
---|---|---
 X | X | O 
Enter Row # (1-3): 2
Enter Column # (1-3): 3
 X | O |   
---|---|---
 O | O | X 
---|---|---
 X | X | O 
Enter Row # (1-3): 1
Enter Column # (1-3): 3
 X | O | X 
---|---|---
 O | O | X 
---|---|---
 X | X | O 
IT'S A TIE/

Would you like to play again? (Y/N): y
   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 2
Enter Column # (1-3): 4
   |   |   
---|---|---
   | O |   
---|---|---
 X |   |   
Enter Row # (1-3): 2
Enter Column # (1-3): 6
   |   |   
---|---|---
 O | O |   
---|---|---
 X |   | X 
Enter Row # (1-3): 2
Enter Column # (1-3): 5
   |   |   
---|---|---
 O | O | O 
---|---|---
 X | X | X 
Enter Row # (1-3): 1
Enter Column # (1-3): 1
 X |   |   
---|---|---
 O | O | O 
---|---|---
 X | X | X 
YOU LOSE!

Would you like to play again? (Y/N): y
   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 1
Enter Column # (1-3): 1
 X |   |   
---|---|---
   | O |   
---|---|---
   |   |   
Enter Row # (1-3): 1
Enter Column # (1-3): 2
 X | X |   
---|---|---
   | O |   
---|---|---
   |   | O 
Enter Row # (1-3): 3
Enter Column # (1-3): 3
Invalid move!
Enter Row # (1-3): 1
Enter Column # (1-3): 3
 X | X | X 
---|---|---
   | O |   
---|---|---
   |   | O 
YOU WIN !

Would you like to play again? (Y/N): y
   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 2
Enter Column # (1-3): 1
   | O |   
---|---|---
 X |   |   
---|---|---
   |   |   
Enter Row # (1-3): 2
Enter Column # (1-3): 2
   | O |   
---|---|---
 X | X |   
---|---|---
   |   | O 
Enter Row # (1-3): 2
Enter Column # (1-3): 3
   | O |   
---|---|---
 X | X | X 
---|---|---
   |   | O 
YOU WIN !

Would you like to play again? (Y/N): y
   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 3
Enter Column # (1-3): 1
   | O |   
---|---|---
   |   |   
---|---|---
 X |   |   
Enter Row # (1-3): 3
Enter Column # (1-3): 2
   | O |   
---|---|---
   |   |   
---|---|---
 X | X | O 
Enter Row # (1-3): 3
Enter Column # (1-3): 3
Invalid move!
Enter Row # (1-3): 1
Enter Column # (1-3): 1
 X | O |   
---|---|---
   |   | O 
---|---|---
 X | X | O 
Enter Row # (1-3): 2
Enter Column # (1-3): 1
 X | O |   
---|---|---
 X |   | O 
---|---|---
 X | X | O 
YOU WIN !

Would you like to play again? (Y/N): y
   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 3
Enter Column # (1-3): 3
   |   | O 
---|---|---
   |   |   
---|---|---
   |   | X 
Enter Row # (1-3): 3
Enter Column # (1-3): 2
   |   | O 
---|---|---
   |   |   
---|---|---
 O | X | X 
Enter Row # (1-3): 2
Enter Column # (1-3): 2
   |   | O 
---|---|---
 O | X |   
---|---|---
 O | X | X 
Enter Row # (1-3): 2
Enter Column # (1-3): 3
 O |   | O 
---|---|---
 O | X | X 
---|---|---
 O | X | X 
YOU LOSE!

Would you like to play again? (Y/N): y
   |   |   
---|---|---
   |   |   
---|---|---
   |   |   
Enter Row # (1-3): 4
Enter Column # (1-3): 1
Invalid move!
Enter Row # (1-3): 2
Enter Column # (1-3): 4
   |   |   
---|---|---
   |   | O 
---|---|---
 X |   |   
Enter Row # (1-3): 
c tic-tac-toe
1个回答
0
投票

编译器告诉你

scanf("%c")
写起来很糟糕。 我怀疑您正在尝试使用该行代码消耗一些空格(换行符),但这是错误的方法。 考虑一下这行代码将做什么。
scanf
将(尝试)从输入流中读取一个数据字节,然后将该字节写入某处。 由于您没有提供写入数据的位置的地址,编译器将生成......执行某些操作的代码。 (它的确切作用是“未定义”。实际上,它可能会生成将一个字节的数据写入内存中的任意位置的代码,这可能根本没有影响,或者可能会产生灾难性的后果并使程序崩溃行为未定义)

编译器向您发出警告,告诉您您编写的代码可能没有达到您想要的效果,您应该更改它。 特别是,如果您打算将

"%c"
格式字符串与
scanf
一起使用,则必须传递相应的参数,该参数是
scanf
应将其读取的字节写入到的内存位置的地址。

但是,如果您尝试使用空格,只需在下一个

scanf
调用中使用空格即可 (
" %c"
)。

© www.soinside.com 2019 - 2024. All rights reserved.