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 个变量,然后它显示其余的问题(编译后):

王国名称:史蒂夫

你被征服了吗(true/false): false

你有多少金币?: 你有多少食物?: 你有多少公民?: 你有多少士兵?:

bool 变量输入字符串 "true" 无效。您应该输入 10。您的 "true" 不能是 "consumed",所以它留在缓冲区中。接下来,您尝试读取 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;
}

全部带入字符串,根据需要进行转换。

您需要使用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;
}

None 个读取命令检查错误。所有这些都应该写成这样:

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。

出于某种原因(可能与旧代码兼容)iostream 默认在 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 的值来自他们,如果不是,那就是标准库中的错误)。