Cin 没有等待输入
Cin isn't waiting for input
程序如下:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(){
string strKingdom = "";
bool conquered_me;//see if was conquered, was going to use this on other program and true = game over.
int gold;
int food;
int citizens;
int soldiers;
cout << endl <<"Name of kingdom: ";
cin >> strKingdom;
cout << endl << "were you conquered (true/false): ";
cin >> conquered_me;
cout << endl << "How many gold do you have?:";
cin>>gold;
cout << endl << "How many food do you have?:";
cin >> food;
cout << endl << "How many citizens do you have?:";
cin >> citizens;
cout << endl << "How many soldiers do you have?:";
cin >> soldiers;
return 0;
}
问题是,当我编译它的程序只让我插入前2个变量,然后它显示其余的问题(编译后):
王国名称:steve
你被征服了吗(真/假):假
你有多少金币?:你有多少食物?:你们有多少公民?:你有多少士兵?:
向bool
变量输入字符串"true"不起作用。输入"1
"或"0
"。你的"true"不能被"消耗",所以它被留在缓冲区中。接下来,您将尝试读取int
值,因此"true"也不匹配。等等......直到节目结束
我就是这么做的:
#include <string>
#include <iostream>
#include <errno.h>
#include <stdlib.h>
using namespace std;
void askForString(string aPrompt, string &aValue) {
cout << aPrompt << " ";
cin >> aValue;
}
void askForBool(string aPrompt, bool &aValue) {
string tString;
while (1) {
cout << aPrompt << " ";
cin >> tString;
if (tString == "true") {
aValue = true;
break;
} else if (tString == "false") {
aValue = false;
break;
} else {
cout << "Repeat, please?" << endl;
}
}
}
void askForInt(string aPrompt, int &aValue) {
string tString;
char *endptr;
while (1) {
cout << aPrompt << " ";
cin >> tString;
errno = 0;
aValue = strtol(tString.c_str(), &endptr, 10);
if (errno || tString.c_str() == endptr || (endptr != NULL && *endptr != 0)) {
cout << "Repeat, please?" << endl;
} else {
break;
}
}
}
int main(void) {
string strKingdom;
bool conquered_me;
int gold;
int food;
int citizens;
int soldiers;
askForString("Name of kingdom:", strKingdom);
askForBool("were you conquered (true/false):", conquered_me);
askForInt("How many gold do you have?:", gold);
askForInt("How many food do you have?:", food);
askForInt("How many citizens do you have?:", citizens);
askForInt("How many soldiers do you have?:", soldiers);
cout << "Kingdom: " << strKingdom << endl;
cout << "Conquered: " << (conquered_me ? "true" : "false") << endl;
cout << "Gold: " << gold << endl;
cout << "Food: " << food << endl;
cout << "Citizens: " << citizens << endl;
cout << "Soldiers: " << soldiers << endl;
return 0;
}
将它们全部转换为字符串,并根据需要进行转换。
出于某种原因(可能与旧代码兼容),iostreams在I/o期间默认将true
转换为1
,将false
转换为0
。
这只是默认值——有一个名为boolalpha
的操纵符,它将设置流使用true
和false
(或本地化的等价物)。
所以,代码如下:
std::cout << 1 == 0; // produces `0`
std::cout << boolalpha << 1 == 0; // produces `false`
这也适用于输入,所以你可以把你的代码改成这样:
cin >> boolalpha >> conquered_me;
…它应该像预期的那样工作(并且在:它应该接受false
或true
的输入,并从中产生false
和true
的值,如果没有,那就是标准库中的错误)。
没有读取命令检查错误。它们都应该像这样写:
while (!(std::cin >> strKingdom)) {
std::cerr << 'Bad input' << std::endl;
std::cin.clear(); // clear the error
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n'); // ignore the rest of the line
// output the same prompt again?
}
为了使这更容易,您可能需要编写一个辅助函数:
template<typename T> void get_input(const char *prompt, T &result) {
std::cout << prompt << std::endl;
while (!(std::cin >> result)) {
std::cerr << 'Bad input' << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
std::cout << prompt << std::endl; } }
你可以专门为bool类型正确读取true/false…
这一行:
cin >> conquered_me;
应该是这样的:
cin >> boolalpha >> conquered_me;
否则输入的值要么是"0",要么是"1"。
通过使用boolalpha
,您的输入可以是"true"或"false "
您需要使用std::getline和std::string来读取各种值。(然后可以使用像atoi这样的函数来解析它们。)下面是使用std::getline函数的代码示例。
#include <iostream>
#include <string>
#include <cstdlib>
int main(){
std::string strKingdom = "";
bool conquered_me;//see if was conquered, was going to use this on other program and true = game over.
int gold;
int food;
int citizens;
int soldiers;
std::string tString = ""; // Used to read and subsequently parse the string.
std::cout << std::endl <<"Name of kingdom: ";
std::getline(std::cin,strKingdom);
std::cout << std::endl << "were you conquered (true/false): ";
std::getline(std::cin,tString);
conquered_me = (tString == "true");
std::cout << std::endl << "How many gold do you have?:";
std::getline(std::cin,tString);
gold = std::atoi(tString.c_str());
std::cout << std::endl << "How many food do you have?:";
std::getline(std::cin,tString);
food = std::atoi(tString.c_str());
std::cout << std::endl << "How many citizens do you have?:";
std::getline(std::cin,tString);
citizens = std::atoi(tString.c_str());
std::cout << std::endl << "How many soldiers do you have?:";
std::getline(std::cin,tString);
soldiers = std::atoi(tString.c_str());
return 0;
}
相关文章:
- Bison/flex 在识别规则后等待输入
- 在Visual Studio中运行/调试C ++时,如何在结束时等待输入
- 在线编译器 - 等待输入并自动馈送输入时拦截程序
- 如何在不冻结程序的主线程的情况下等待条形码读取器输入
- "cin >>变量"不等待输入
- 如何将数据写入 stdin,以便由等待来自 stdin 输入的单独线程使用?
- QInputDialog & Ththreader(worker,show dialog,等待输入,继续)
- 我将如何连续更新变量,并同时等待输入
- 为什么"cin >> noskipws"不等待输入?
- 确定QProcess是否已完成执行或正在等待用户输入(或响应)
- C++等待用户输入的中断线程
- 尽管Cin.ignore(),CIN不等待输入
- 使用 crontab 运行 C++ 程序,"cin"不会等待我的输入
- 服务器进程等待客户端输入/消息的最佳方法是什么?
- cin.getline() 等待输入
- 为什么"keep_window_open()"不等待输入字符?
- 在等待输入时执行无限循环
- C 等待用户输入
- istream_iterator CIN 初始化等待输入
- 如何让程序等待键盘输入