c语言scanf内存占用,c - C语言中的Scanf是否导致奇怪的控制台行为? - 堆栈内存溢出...

我知道scanf不好,而且是全部,但是我们不会对此进行无效输入的测试(假定始终有效)。 但是,当我调用具有scanf调用的promptPlayer() ,向控制台的输出停止(这没有意义,因为所有输出都应该在控制台中进行,而对scanf的调用甚至都在进行 。输入应为set由空格分隔的坐标(如输出所示)。

这是代码(重要的东西):

othello.c

#include

#include

#include

#include "othello_engine.h"

int main(int numArgs, const char *args[]) {

// -------------------------- Initialize the Othello routine --------------------------- //

// Create the board

int size = atoi(args[2]);

char board[size][size];

// Display game start message

if (args[4][0] == 'B' || args[4][0] == 'b')

printf("Othello game with a %dx%d board - Player %d with black disc to start\n\n", size, size, atoi(args[3]));

else

printf("Othello game with a %dx%d board - Player %d with white disc to start\n\n", size, size, atoi(args[3]));

// Initially display the game board

displayBoard(size, size, board);

// --------------------------- Start the game through user input --------------------------- //

Coordinate placed = promptPlayer(1);

printf("Place at [%d,%d].\n", placed.x, placed.y);

Coordinate placed2 = promptPlayer(1);

printf("Place at [%d,%d].\n", placed2.x, placed2.y);

return EXIT_SUCCESS;

}

othello_engine.c

#include "othello_engine.h"

#include

void displayBoard(int size, int size2, char board[size][size2]) {

// Print the column numbers

printf(" ");

for (int i = 0; i < size; i ++) {

printf(" %d", (i+1));

}

printf("\n");

// Print the rest of the matrix, including row numbers

for (int i = 0; i < size; i ++) {

printf("%d", (i+1));

for (int j = 0; j < size2; j ++) {

if (board[i][j] != 'B' && board[i][j] != 'W') printf(" -");

else printf(" %c", board[i][j]);

}

printf("\n");

}

printf("\n\n");

}

Coordinate promptPlayer(int player) {

Coordinate desiredLocation;

printf("Player %d's turn (enter row and column): ", player);

scanf("%d %d", &desiredLocation.x, &desiredLocation.y);

//scanf("%*[^\n]%*c");

return desiredLocation;

}

最后,othello_engine.h

#ifndef OTHELLO_ENGINE_H_

#define OTHELLO_ENGINE_H_

typedef struct {

int x;

int y;

} Coordinate;

#endif /* OTHELLO_ENGINE_H_ */

/*

* Displays the Othello board and row/column numbers.

*/

void displayBoard(int size, int size2, char board[size][size2]);

Coordinate promptPlayer(int player);

输出如下:基本上,整个程序的输出流都将暂停,直到评估完所有scanf为止。 我不知道是什么原因造成的。

1 2 // This is the first scanf input

3 4 // This is the second set

Othello game with a 6x6 board - Player 1 with black disc to start

1 2 3 4 5 6

1 - - - - - -

2 - - - - - -

3 - - B W - -

4 - - W B - -

5 - - - - - -

6 - - - - - -

Player 1's turn (enter row and column): Place at [1,2]. // Here is where the first scanf's values are output

Player 1's turn (enter row and column): Place at [3,4]. // Here is the second set

真的没有任何意义。 一切都完美地打印出来,直到调用promptPlayer方法-正确接收了值,所以我才真正看到问题...好像scanf停止了控制台输出(神奇地是?),直到输入了值-然后它打印了所有内容一次全部添加到控制台。 我只知道这很明显。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值