Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

How should I do with "std::cerr" and "std::cin.fail()" using c++?

Could you please help me with the small question?

Question: The program should return an error code(1) and print this exact string in case a failure is encountered: “Error encountered, exiting...”

My code:

#include <iostream>
#include <random>

int main(int argc, char const *argv[])
{

    int number, random;
    random = (std::rand() % 100);

    while (std::cin >> number)
    {
        if (number < random && number >= 0)
        {
            std::cout << "This number is larger." << std::endl;
        }
        else if (number > random && number <= 99)
        {
            std::cout << "This number is smaller." << std::endl;
        }
        else if (std::cin.fail())
        {
            std::cout << "Error encountered, exiting..." << std::endl;
        }
        else if (number < 0 || number > 99)
        {
            std::cout << "[WARNING] : Number must be between 0 and 99" << std::endl;
        }

        else if (number == random)
        {
            std::cout << "Great! You're right!" << std::endl;
            break;
        }
    };

    return 0;
}

After compilation, I will input e.g. 'write'. My programming is directly finished. I don't get 'Error encountered, exiting...' in my terminal. Could you give some suggestions about std::cin.fail()? How should I do? I also don't understand, could I change here std::cout using std::cerr? What is the big difference between std::cout and std::cerr?

Thanks so much

Best regards

question from:https://stackoverflow.com/questions/65853913/how-should-i-do-with-stdcerr-and-stdcin-fail-using-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should test std::cin.fail() after the while loop because if std::cin >> number fails, the body of the while loop will not be entered.

You could also restructure the tests a bit to not have to perform the same tests over and over again. Also, std::endl flushes the output stream which is unnecessary in the vast majority of cases and just takes extra time.

    // ...
    while (std::cin >> number)
    {
        if (number < 0 || number > 99)
        {
            std::cout << "[WARNING] : Number must be between 0 and 99
";
        }
        else if (number < random) // no need to test if it's out of bounds
        {
            std::cout << "This number is larger.
";
        }
        else if (number > random) // no need to test if it's out of bounds
        {
            std::cout << "This number is smaller.
";
        }
        else                       // no need to test anything
        {
            std::cout << "Great! You're right!
";
            break;
        }
    }
    if(std::cin.fail())
    {
        std::cout << "Error encountered, exiting...
";
        return 1;
    }
    return 0;
}

The difference between std::cout and std::cerr is which output stream it'll use. People running your program can read both streams separately if they want to do logging of errors for example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...