Cin 没有等待输入

Cin isn't waiting for input

本文关键字:输入 等待 Cin      更新时间:2023-10-16

程序如下:

#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的操纵符,它将设置流使用truefalse(或本地化的等价物)。

所以,代码如下:

std::cout << 1 == 0;               // produces `0`
std::cout << boolalpha << 1 == 0;  // produces `false`

这也适用于输入,所以你可以把你的代码改成这样:

cin >> boolalpha >> conquered_me;

…它应该像预期的那样工作(并且在:它应该接受falsetrue的输入,并从中产生falsetrue的值,如果没有,那就是标准库中的错误)。

没有读取命令检查错误。它们都应该像这样写:

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;
}