简体   繁体   English

如何在 do...while 循环中验证输入,然后在必要时重新提问?

[英]How to validate input in a do...while loop and then re-ask question if necessary?

So far I have tried to make a do...while loop where it asks two questions.到目前为止,我已经尝试做一个 do...while 循环,它会问两个问题。 One which is how many miles and the second which is how much does the package weigh.一个是多少英里,第二个是包裹的重量。 If the number of miles is equal to 0 or less it should output an error and then re-ask the question.如果英里数等于或小于 0,它应该输出一个错误,然后重新提问。 Once validated it should move onto the weight question with the same requirements, and if the weight is invalid it should only repeat the weight question since the miles question is already valid.一旦通过验证,它应该转移到具有相同要求的重量问题,如果重量无效,它应该只重复重量问题,因为英里问题已经有效。

This is the code that I have so far:这是我到目前为止的代码:

int miles = 0;
int choice = 0;
double weight = 0.0;


do
{
    cout << "Enter the number of miles as a whole number: ";
    cin >> miles;
    if (miles > 0)
    {
        cout << "Enter the weight of the package in pounds: ";
        cin >> weight;

        if (weight > 0 && weight < 10)
        {
            cout << "etc etc etc";
        }
        else
        {
            cout << "\n\tError: Weight must be greater than zero and less than 10 pounds!\n" << endl;
        }

    }
    else
    {
        cout << "\n\tError: Miles must be greater than zero!\n" << endl;
    }

    cout << "Enter 1 to continue or 0 to quit: ";
    cin >> choice;
    cout << endl;

}
while (choice != 0); 
cout << "\nGood-bye!\n";

return 0;  

You can use multiple while loops, one for each input.您可以使用多个 while 循环,每个输入一个。

int miles = 0;
double weight = 0.0;
bool correct = false;

while (!correct)
{
    cout << "Enter the number of miles as a whole number: " << std::endl;
    bool success = cin >> miles;
    if (!success || miles < 0) {
        cout << "Invalid miles value -- must be nonnegative." << std::endl;
    }
    else {
        correct = true;
    }
}

correct = false;

while (!correct)
{
    cout << "Enter the weight in pounds: " << std::endl;
    bool success = cin >> weight;
    if (!success || weight < 0 || weight > 10) {
        cout << "Invalid weight value -- must be between 0 and 10." << std::endl;
    }
    else {
        correct = true;
    }
}

// do calculation

cout << "\nGood-bye!\n";

return 0;

You need additional loops to read from cin until the input is valid.您需要额外的循环才能从cin读取,直到输入有效。 And you need to ensure that cin is even successful in reading a value before you perform range checking on that value.并且在对该值执行范围检查之前,您需要确保cin甚至成功读取该值。

I would suggest doing the read+validation in a separate function, eg:我建议在单独的函数中进行读取+验证,例如:

#include <iostream>
#include <limits>

template<typename T>
T prompt(const char *msg, T maxValue = std::numeric_limits<T>::max())
{
    T value;

    do
    {
        cout << msg << ": ";

        if (!(cin >> value))
        {
            cout << "\n\tError: Invalid input!\n" << endl;
            cin.clear();
            cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        else if (value < 0)
        {
            cout << "\n\tError: Value must be greater than 0!\n" << endl;
        }
        else if (value >= maxValue)
        {
            cout << "\n\tError: Value must be less than " << maxValue << "!\n" << endl;
        }
        else
            break;
    }
    while (true);

    return value;
}

...

int miles;
double weight;
int choice;

do
{
    miles = prompt<int>("Enter the number of miles as a whole number");
    weight = prompt<double>("Enter the weight of the package in pounds", 10.0);
    choice = prompt<int>("Enter 1 to continue or 0 to quit", 2);
    cout << endl;
}
while (choice != 0); 

cout << "\nGood-bye!\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM