如何更正此程序?(已编辑)
How to correct this program? (EDITED)
(我刚刚编辑了我的帖子,很抱歉造成混淆)你能帮我处理这段代码吗?我期待着一系列的回答槽。//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。这会导致循环输出,然后重复,从而产生两个输出。
由于您的代码在输入之间不需要输入,因此不应键入输入。在同一行键入所有答案,代码就会正常工作。或者,将代码更改为,静默忽略行尾字符。
相关文章:
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- Python中的for循环与C++有何不同
- 编辑文件中的文本
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 更正 CMakeList.txt 用于谷歌测试的文件?
- SetDlgItemInt 不会更改嵌入资源的编辑框
- 在C++中释放内存期间,迭代器与指针有何不同
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- C++,Qt:仅在双击后编辑qtablewidget项目
- 编辑字符串以包含括号 c++ 之间的变量
- Arduino函数无法编辑数组
- 调试符号中缺少 QObject 类信息(编辑但存在其他 Qt 类)
- "未定义对'WinMain@16'的引用"gcc 编辑器中的错误
- C++MFC编辑框处理双击
- C++:寻找"returning address of local variable..."的更正
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 是否可以使用指针访问变量以避免直接编辑变量?
- 如何更正此程序?(已编辑)
- Visual Studio 2010编辑器意外自动更正