0

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;
}
SnazR
  • 29
  • 3
  • Not exactly a duplicate but should provide an answer: https://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it – Galik Apr 12 '18 at 15:40
  • How about taking the input as a string and then validating it yourself, something like ```for (char c : str) assert(isdigit(c))``` – lakshayg Apr 12 '18 at 15:52
  • @LakshayGarg I would prefer if it could stay as an integer as this is within a function and it is then returned as * bet or -bet depending on the outcome – SnazR Apr 12 '18 at 15:57
  • You can create a new function to read an integer from cin. Let's call it `read_int` which does the work I described in the earlier comment. – lakshayg Apr 12 '18 at 16:04

2 Answers2

0
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.

Stan Dresden
  • 51
  • 1
  • 6
0
  1. Do getline to read one whole line of input from cin.
  2. Create a stringstream to parse the string you got.
  3. In this parser, read the number; if it fails - error
  4. Read whitespace; if it doesn't arrive to the end of string - error
#include <sstream>

...

int bet = 0;
std::cout << " Place your bet: ";
while (true)
{
    std::string temp_str;
    std::getline(cin, temp_str);
    std::stringstream parser(temp_str);
    if (parser >> bet && (parser >> std::ws).eof())
        break; // success
    cout << endl << "Please enter a valid number" << endl;
}

This code keeps printing the error message until it receives valid input. Not sure this is exactly what you want, but it's pretty customary UI.

Here >> ws means "read all the whitespace". And eof ("end of file") means "end of the input string".

anatolyg
  • 26,506
  • 9
  • 60
  • 134