C 是什么导致了这种奇怪的输出?
我有以下代码:C 是什么导致了这种奇怪的输出?,c,C,我有以下代码: void doPlayerMove(void) { bool moved = false; while (!moved) { printf("\nWhere is the piece you want to move?(rc):"); int r = getchar() - '0';// gets the row number int c = getchar() - '0';// gets the col
void doPlayerMove(void)
{
bool moved = false;
while (!moved)
{
printf("\nWhere is the piece you want to move?(rc):");
int r = getchar() - '0';// gets the row number
int c = getchar() - '0';// gets the column number
printf("%d:%d", r, c);// prints the chosen row/column
clearInput();
printf("\nWhere is the space you want to move to?(rc):");
int r2 = getchar() - '0';
int c2 = getchar() - '0';
printf("%d:%d", r2, c2);
...
}
}
void clearInput(void)
{
while(getchar() != '\n');
}
这是我得到的输出:
Where is the piece you want to move?(rc):51
5:1
Where is the space you want to move to?(rc):40
4:00
额外的0是怎么回事?有人看到问题出在哪里了吗?正如OP在评论中所说:
问题解决了,这是我调用的某个函数的输出 这个对不起,误报了
从下一次
printf
call?:Dc00kie可能正在处理该问题;如果将%d:%d
更改为%d:%d\n
多余的零是否消失?请调用clearInput()代码>在第二次输入之后。如果您在打印的字符串末尾添加\n
,第二个零可能会出现在下一行:)问题解决,这是我在中调用的某个函数的一些输出…
为错误警报感到抱歉!