Cin并不等待输入

时间:2015-07-21 20:37:21

标签: c++ cin

这是该计划:

#wrapper {
  display: inline-block;
  width: 300px;
}
#images {
  display: inline-block;
  width: 100px;
}
#specs {
  display: inline-block;
  width: 100px;
}

问题在于,当我编译它时,progam只允许我插入前两个变量,然后它显示其余的问题(编译后):

王国的名字:史蒂夫

你被征服了(真/假):假

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

6 个答案:

答案 0 :(得分:7)

bool变量中输入字符串“true”不起作用。您应该输入10。你的“真实”不能被“消耗”,所以它留在缓冲区中。接下来,您尝试读取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;
}

答案 1 :(得分:2)

将它们全部串入字符串,并根据需要进行转换。

答案 2 :(得分:2)

出于某种原因(可能与旧代码兼容)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的值,如果它没有标准库中的错误。)

答案 3 :(得分:1)

您的所有读取命令都不会检查错误。所有这些都应该写成:

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 ...

答案 4 :(得分:1)

这一行:

cin >> conquered_me;

应该是这样的:

cin >> boolalpha >> conquered_me;

否则输入需要&#34; 0&#34;或者&#34; 1&#34;。

使用boolalpha您的输入可以是&#34; true&#34;或者&#34;假。

答案 5 :(得分: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;
}