Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9311623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:25:08+00:00 2026-06-19T01:25:08+00:00

The loop in the function require() takes 3 conditions, a > b or a

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:25:10+00:00Added an answer on June 19, 2026 at 1:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a loop function based on the time. After the
I have a function creating entries via grid() like this: (replace while-loop with function,
There is a loop in the function I want to modify: foreach ( $form_fields
I have the following loop inside an function init(); which is executed onload, I
I am using the following function to loop through a couple of open CDB
I have a program in Octave that has a loop - running a function
No matter what I try I keep getting an infinite loop with this function:
In a multithreaded application. I have a bunch of function that loop through a
The function instance for ArrowLoop contains loop :: ((b,d) -> (c,d)) -> (b ->
My server.js is // server.js - the outer server loop var http = require('http')

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.