循环不提示用户输入(C )

while loop not prompting for user input (c++)

本文关键字:输入 用户 提示 循环      更新时间:2023-10-16

我的循环效果很好,直到输入" true"时,输入" true"时,循环忽略了cin的剩余循环,然后将文本打印在cout中,直到循环结束了,如何使循环要求在每个循环上要求输入,或者我在哪里犯了一个错误?PS:这是一项功课,所以我无法更改给出的结构。感谢您的建议。代码片段:

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

问题是当您阅读isMale时。bool确实是一个数字,false01的CC_5。您无法读取字符串。

发生的事情是,您最终会在流中有很多字符,而>>操作中的任何一个都无法读取,因此它们都只是连续失败。尝试在命令行上传递10,一切都很好。

如果您希望用户能够通过字符串,然后将其视为bool,则可以完全使用为此提供的实用程序,或者手动使用它只是为了帮助您理解。

要使用标准实用程序,称为std::boolalpha,并在<iomanip>标头中:

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年!学会编写好的程序不仅意味着语言的技术性,而且还意味着如何编写对用户有益的程序,因此很早就养成良好的习惯是一个好主意。

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

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

您无法将字符串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;
}

这里的问题是isMaleboolean,字符串"true"boolean不同!之所以不会崩溃,而只是简单地贯穿程序的其余部分是因为您的cin仍在读取输入流的旧字符。一个简单的解决方案是将True或False转换为Booleans,然后使用这些将其设置为isMale。这可以通过:

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