C中的Scanf会导致奇怪的控制台行为吗?

时间:2013-09-24 00:05:15

标签: c console scanf

我知道scanf很糟糕,但我们不会在此测试无效输入(假设始终有效)。但是,当我调用具有promptPlayer()调用的scanf时,输出到控制台会停止(这没有任何意义,因为所有输出应该在控制台之前 甚至对scanf的调用。输入应该是由空格分隔的一组坐标(如输出中所示)。

以下是代码(重要内容):

othello.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 <stdio.h>

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似乎停止了控制台输出(神奇地?)直到输入值 - 然后它将所有内容一次打印到控制台。我知道这是非常明显的事情。

2 个答案:

答案 0 :(得分:3)

我运行了一小部分你的代码promptPlayer并且我的眼睛无法观察到你的代码出现了什么问题,它做了预期的事情。我认为这是您的代码之外的问题,可能是您正在使用的编辑器及其与控制台的交互,例如EclipseMintty

尝试在编辑器工具之外运行已编译的exe,并找出是否遇到同样的问题。

假设它与上面类似,那么添加以下代码来刷新输出也可以解决问题:

    Coordinate desiredLocation;
    printf("Player %d's turn (enter row and column): ", player);
    fflush(stdout);
    scanf("%d %d", &desiredLocation.x, &desiredLocation.y);

答案 1 :(得分:1)

printf之类的声音正在缓冲您的输出。

在致电fflush(stdout);后使用printf或添加\n。换行符基本上做同样的事情。或者您甚至可以使用setbuf(stdout, NULL);禁用所有缓冲。