如何更正此程序?(已编辑)

How to correct this program? (EDITED)

本文关键字:编辑 何更正 程序      更新时间:2023-10-16

(我刚刚编辑了我的帖子,很抱歉造成混淆)你能帮我处理这段代码吗?我期待着一系列的回答槽。//2-12-14.cpp:定义控制台应用程序的入口点。//

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
int x = 0;
char answer[25];
for (x = 0; x < 25; x++) {
do {
cout << x + 1 << ".";
scanf("%c", answer + x);
} while (
(answer[x] != 'A')
&& (answer[x] != 'B')
&& (answer[x] != 'C')
&& (answer[x] != 'D')
);  
}
system("PAUSE");
return 0;
}

为什么第二个输出是双数字?

scanf("%c", answer + x);

当我用替换它时

cin>>answer[x]; //this was previously cout<<answer[x] **sorry for the confusion**

它运行良好。

为什么第二个输出给出双数字?

因为您提供的是双重输入。如果键入"A<enter>",则第一个字符为A,第二个字符为enter。这会导致循环输出,然后重复,从而产生两个输出。

由于您的代码在输入之间不需要输入,因此不应键入输入。在同一行键入所有答案,代码就会正常工作。或者,将代码更改为,静默忽略行尾字符。