Question

I am trying to make a cin where the user can only enter 0 to 1. If the user doesnt enter those numbers then he should get an error saying "Please enter within the range of 0 to 1."

But its not working.

What am i doing wrong?

   int alphaval = -1;
    do
    {
        std::cout << "Enter Alpha between [0, 1]:  ";
        while (!(std::cin >> alphaval)) // while the input is invalid
        {
            std::cin.clear(); // clear the fail bit
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore the invalid entry
            std::cout << "Invalid Entry!  Please Enter a valid value:  ";
        }
    }
    while (0 > alphaval || 1 < alphaval);

    Alpha = alphaval;
Was it helpful?

Solution

Try this:

int alphaval;
cout << "Enter a number between 0 and 1: ";
cin >> alphaval;
while (alphaval < 0 || alphaval > 1)
{
        cout << "Invalid entry! Please enter a valid value: ";
        cin >> alphaval;
}

OTHER TIPS

If you want to trap empty lines I'd use std::getline and then parse the string to see if the input is valid.

Something like this:

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    int alphaval = -1;
    for(;;)
    {
        std::cout << "Enter Alpha between [0, 1]:  ";

        std::string line;
        std::getline(std::cin, line);
        if(!line.empty())
        {
            std::stringstream s(line);
            //If an int was parsed, the stream is now empty, and it fits the range break out of the loop.
            if(s >> alphaval && s.eof() && (alphaval >= 0 && alphaval <= 1))
            {
                break;
            }
        }
        std::cout << "Invalid Entry!\n";
    }
    std::cout << "Alpha = " << alphaval << "\n";

    return 0;
}

If you want a different prompt on error then I'd put the initial prompt outside the loop and change the inner prompt to what you prefer.

Week one of C++, starting with Peggy Fisher's Learning C++ on Lynda.com. This is what I came up with. Love to receive feedback.

int GetIntFromRange(int lower, int upper){
    //variable that we'll assign input to
    int input; 
    //clear any previous inputs so that we don't take anything from previous lines
    cin.clear(); 
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    //First error catch. If it's not an integer, don't even let it get to bounds control
    while(!(cin>>input)) {
        cout << "Wrong Input Type. Please try again.\n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
   }

    //Bounds control
    while(input < lower || input > upper) {
        cout << "Out of Range. Re-enter option: ";
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        //Second error catch. If out of range integer was entered, and then a non-integer this second one shall catch it
        while(!(cin>>input)) {
            cout << "Wrong Input Type. Please try again.\n";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }

    //return the cin input
    return input;
}

As the exercise was to order Hamburgers, this is how I ask for the amount:

int main(){
    amount=GetIntFromRange(0,20);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top