【问题标题】:while loop not prompting for user input (c++)while循环不提示用户输入(c ++)
【发布时间】:2019-08-03 03:02:12
【问题描述】:

我的循环运行良好,直到我输入最后一个值作为性别的输入,当输入“true”时,循环忽略其余循环的 cin,只在 cout 中打印文本,直到循环结束,任何想法如何让循环在每个循环上都要求输入,或者我在哪里犯了错误? ps:这是功课,所以我不能改变给定的结构。感谢您的任何建议。代码sn-p:

int main()
{
    struct Patient
    {
        double height;
        double weight;
        int age;
        bool isMale;
    };
    Patient ListOfPatients[4];
    int iii = 0;

    while (iii < 4)
    {
        cout << "enter the height (eg. 1.80 metres) of patient number  " << iii + 1 << " :" << endl;
        cin >> ListOfPatients[iii].height;
        cout << "enter the weight (eg. 80kg) of patient number " << iii + 1 << " :" << endl;
        cin >> ListOfPatients[iii].weight;
        cout << "enter the age of patient number " << iii + 1 << " :" << endl;
        cin >> ListOfPatients[iii].age;
        cout << "is the patient a male? (true = male or false = female) " << endl;
        cin >> ListOfPatients[iii].isMale;

        iii++;
    }

    return 0;
}

【问题讨论】:

标签: c++ arrays loops input


【解决方案1】:

您不能将字符串 true 分配给布尔字段。请检查更正的示例。

int main()
{
    struct Patient
    {
        double height;
        double weight;
        int age;
        bool isMale;
    };
    Patient ListOfPatients[4];
    int iii = 0;

    while (iii < 4)
    {
        string sIsMale = "";

        cout << "enter the height (eg. 1.80 metres) of patient number  " << iii + 1 << " :" << endl;
        cin >> ListOfPatients[iii].height;
        cout << "enter the weight (eg. 80kg) of patient number " << iii + 1 << " :" << endl;
        cin >> ListOfPatients[iii].weight;
        cout << "enter the age of patient number " << iii + 1 << " :" << endl;
        cin >> ListOfPatients[iii].age;
        cout << "is the patient a male? (true = male or false = female) " << endl;
        cin >> sIsMale;
        ListOfPatients[iii].isMale = sIsMale == "true";

        iii++;
    }

    return 0;
}

【讨论】:

    【解决方案2】:

    问题出在您阅读isMale 时。 bool 实际上是一个数字,0 代表 false1 代表 true。您无法将字符串读入其中。

    发生的情况是,您最终会在流中得到一堆字符,这些字符无法被任何 &gt;&gt; 操作读取,因此它们都会连续失败。尝试在命令行中传入10,一切正常。

    如果您希望用户能够传入一个字符串,然后将其视为bool,您可以使用专门为此提供的实用程序,或者手动执行以帮助您理解。

    要使用标准实用程序,它被称为 std::boolalpha 并位于 &lt;iomanip&gt; 标头中:

    std::cin >> std::boolalpha >> ListOfPatients[i].isMale;
    

    要手动进行,请将其读入std::string 对象并自己进行一些比较,如下所示:

    std::string tmpIsMale;
    std::cin >> tmpIsMale;
    ListOfPatients[i].isMale = (tmpIsMale == "true");
    

    正是出于这个原因,在尝试读取流后检查流的状态也是一个好主意。为此,您可以将流直接放入if

    std::cin >> someVariable;
    if (!cin) { // cin is not in a good state
        std::cerr << "Failed to read someVariable, please try again with a proper value!\n";
        return 1;
    }
    

    您可能希望避免的一些旁白和不良做法:

    1. using namespace std; 被广泛认为是不好的做法。
    2. std::endl 有点争议,但你使用它的方式表明你不理解它。
    3. 性别不是布尔选项。我知道这不是一个严格的技术问题,如果结构是由你的老师提供的,你会有点卡住,但是来吧,现在是 2019 年!学习编写好的程序不仅仅意味着语言的技术性,还包括学习如何编写对用户有益的程序,所以尽早养成良好的习惯是个好主意。

    【讨论】:

    • std::boolalpha 就是为了这个。
    • 谢谢@SombreroChicken - 我不记得这是否适用于cin 以及cout,并且尚未检查。
    • 解决您的第三个问题...首先,这是一个非常简单的应用程序,仅测量医疗患者的 4 个特征,所以我不明白为什么 OP 应该包括广泛的性别.其次,该衡量标准不是性别,它是一种医学应用,因此很可能是一种性别衡量标准,这在很大程度上是二元的。没错,这不是技术问题,恕我直言,我认为您应该将其排除在下一个答案之外。
    【解决方案3】:

    您可以使用流操纵器boolalpha 自己不进行任何比较:

    cin >> std::boolalpha >> cListOfPatients[iii].isMale >> std::noboolalpha;
    

    【讨论】:

      【解决方案4】:

      这里的问题是isMaleboolean,而字符串"true"boolean 不同!它不会崩溃而只是运行程序的其余部分的原因是因为您的cin 仍在从输入流中读取旧字符。一个简单的解决方案是将 true 或 false 转换为布尔值,然后使用它们来设置 isMale。这可以通过以下方式完成:

      String toBeConverted;
      cin >> toBeConverted;
      ListOfPatients[iii].isMale = toBeConverted == "true";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-16
        • 2020-02-15
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多