簡體   English   中英

while循環沒有提示用戶輸入(c ++)

[英]while loop not prompting for user input (c++)

我的循環工作正常,直到我輸入最后一個值作為性別的輸入,當輸入“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實際上是一個數字, 0表示false1表示true 你無法讀入它的字符串。

會發生什么事情,你最終會在流中找到一堆無法通過任何>>操作讀取的字符,因此它們都會連續失敗。 嘗試在命令行中傳入10 ,一切正常。

如果您希望用戶能夠傳入字符串然后將其視為bool ,則可以使用為此提供的實用程序,或者手動執行以幫助您理解。

要使用標准實用程序,它被稱為std::boolalpha並且位於<iomanip>標頭中:

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

要手動執行此操作,請將其讀入std::string對象並std::string進行一些比較,如下所示:

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

正是出於這個原因,在嘗試讀取流之后檢查流的狀態也是一個好主意。 您可以將流直接放入if for this:

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年! 學習編寫好的程序並不僅僅意味着語言的技術性,而是學習如何編寫對用戶有益的程序,因此最好早點養成良好的習慣。

您不能將字符串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;
}

您可以使用流操縱器boolalpha來自己進行任何比較:

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

這里的問題是isMale是一個boolean ,字符串"true"boolean 它之所以沒有崩潰而只是運行程序的其余部分,是因為你的cin仍在從輸入流中讀取舊字符。 一個簡單的解決方案是將true或false轉換為布爾值,然后使用這些來設置isMale 這可以通過以下方式完成:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM