0

The loop in the function require() takes 3 conditions, a > b or "a" or "b" aren't digits. Even when I don't satisfy the conditions and put 2 integers in, it just loops once again.

Also when I put in a character then it just endlessly loops "Enter minimum number Enter maximum number" ignoring the cins. Anyone know why? I'm a beginner so this is probably really obvious

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

int random(int minN, int maxN)   //generates random number within specified range
{
    srand (time(NULL));
    int x = (maxN - minN);
    int y = minN + (rand() % (x+1));
    return y;
}

int require()        //makes sure a < b and both are digits
{
    int a,b;
    do {
    cout << "Enter minimum number" << endl;
    cin >> a;
    cout << "Enter maximum number. Note: Has to be greater or equal to minimum." << endl;
    cin >> b;
    } while (a > b || !isdigit(a) || !isdigit(b));

    return random(a,b);
}

int main()
{
    cout << require() << endl;
}
8
  • Sorry fixed my wording Commented Feb 15, 2013 at 6:21
  • 1
    isdigit is for testing characters. If you pass it integers as arguments, it interprets them as character under a certain encoding (depending on locale). Commented Feb 15, 2013 at 6:22
  • Also, what do you mean even when I satisfy the conditions, [...] it loops again. That's how a do-while loop works: It loops as long as the condition is satisfied. It stops when the condition is no longer satisfied. Commented Feb 15, 2013 at 6:24
  • Sorry fixed that too, is there a function that interprets numbers instead of characters? Commented Feb 15, 2013 at 6:25
  • 1
    You only need to call srand (time(NULL)); once at the start of the program. Commented Feb 15, 2013 at 6:27

2 Answers 2

1

You should not use isdigit as this relates to a particular character is a digiti. Instead the loop should look like this:

int require()        //makes sure a < b and both are digits
{
    validNumbers = true;
    do
    {
       cout << "Enter minimum number" << endl;
       cin.clear();
       cin >> a;
    } while (cin.fail());

    do
    {
       cout << "Enter maximum number. Note: Has to be greater or equal to minimum."
            << endl;
       cin.clear();
       cin >> b;
    } while (cin.fail() || a > b);

    return random(a,b);
}

PS: You only need to call srand (time(NULL)); once at the start of the program.

Sign up to request clarification or add additional context in comments.

Comments

1

You are reading the numbers as, well, numbers not as characters as the isdigit function expects. If you are using a C++11 compliant standard library, the values of a and b will actually be zero if the input is not valid integer numbers, which means that e.g. !isdigit(a) will be true. If you are using a non-C++11 library, then the value of a and b will be random, and will most likely cause !isdigit(a) to be true as well as the amount of valid digit ASCII values in a full 32-bit integer range is quite small.


If you read a reference about the input operator, like this one you will see that if extraction fails, then the streams failbit will be set. This can either be tested "inline" like this:

if (!(std::cin >> a))
{
    std::cout << "Not a valid number, try again: ";
    continue;
}

Or it can be tested using the streams fail function.

2 Comments

I see, what can I do instead? I'm not too knowledgeable but would something like atoi work?
Thanks, I thought I was stupid for not figuring this out, but I just haven't learned enough syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.