C++ - cin to only take one integer

SnazR

This code works fine if I enter something that isn't a number in, e.g. F: it will print the error message. However, if I enter e.g. 2F2 or , it will take the 2 and pass the check, continue in my code and on the next cin >> statement it will put the F in, and then it loops back and puts the 2 in.

How do I make it so it only accepts a single number e.g. 2 and not e.g. 2F2 or 2.2?

int bet = 0;
// User input for bet 
cout << " Place your bet: ";
cin >> bet;
cout <<
// Check if the bet is a number
if (!cin.good())
{
    cin.clear();
    cin.ignore();
    cout << endl << "Please enter a valid number" << endl;
    return;
}
Stan Dresden
bool Checknum(std::string line) {
bool isnum = true;
int decimalpoint = 0;
for (unsigned int i = 0; i < line.length(); ++i) {

    if (isdigit(line[i]) == false) {
        if (line[i] == '.') {
            ++decimalpoint;  // Checks if the input has a decimal point that is causing the error.
        } 

        else {
            isnum = false;
            break;
        }
    }
}
if (decimalpoint > 1) // If it has more than one decimal point.
    isnum = false;
return isnum;
}

If you take a string from the user, this should work. You can convert the string to an integer or a float(stoi or stof, respectively). It may not be the best solution there is, but this is what I have. Excuse the indentation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to make cin only take integer inputs

getopt to take only one argument

Date input only one integer not day,month and year in C

How to take only integer value in pandas dataframe

cin and getline in one program, and only one of them takes effect?

take control of cin in main()

c++ hangs when cin double but proceeds if entering an integer

Why does operator[] only take one argument?

How to take only one connection from a matrix

How do I take in one input when I use cin for two inputs?

Can CSS3 Flex take only integer values?

Take integer only from a nested list and apply function to it

Regex: matching for only one number in an integer R

Make only one child take 100% height of parent with display flex?

LEFT JOIN but take only one row from right side

Why does the (/d+) regex take only one digit?

Take only one entry from SQL based on two columns?

using switchMap to unsubscribe previous http request and only take latest one

dplyr filter to get only the values that one of the groups take

If there are several Positions, then find only the main one, otherwise take another

Take only local value not global one, when calling a global variable

Sort values in a dataframe by a column and take second one only if equal

Can the scikit-learn .fit method take in only one dataset?

How to only take 1 value in one row in fetchall()

How do we sign a char an integer value in C and take a sum?

How to take input 128 bit unsigned integer in c++

Stop cin from continuing to take in inputs

Take time values into integer?

To take an integer input after character I used " %c " but does not run when no integer given as input