Question

In a program, I read in an unsigned long long, and that normally works fine. However, if someone decides to enter a negative number, The prompt, prints infinitely.

This is the part of my code, that it is relevant:

    unsigned long long betAmount = 0;
    cout << "You have " << chipCount << " chips currently!" << endl;
    cout << "How many chips would you like to bet?" << endl;
    cout << "Must be a whole number: ";
    cin >> betAmount;

It is pretty standard, unless given a negative.

Was it helpful?

Solution

You may use a string to get the input. Check the first byte. If it is a unsigned, use a stringstream to convert the string to unsigned long long.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top