im working on a program that generates a random number and asks the user to guess what it is. right now im stuck on checking to see if the number is between 1-20. how would i make it continuously loop back until the user enters a number between 1-20? the way im doing it only checks once.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int getData(void);
int getRandom(void);
bool findRange(int);

int main()
{
    int guessNum, random;
    bool checkRange;
    
    random = getRandom();
    guessNum = getData();
    checkRange = findRange(guessNum);
    if (checkRange)
        guessNum = getData();
    
    
    cout << guessNum << " " << random << endl;
    
    system("pause");
    return 0;
}    

int getData(void)
{
    int guessNum;
    
    cout << "I am thinking of a number between 1 and 20.\nCan you guess what it is? : ";
    cin >> guessNum;
    
    return guessNum;
}

bool findRange(int guessNum)
{
    bool answer;
    
    if (guessNum < 1 || guessNum > 20)
    {    
        cout << "\nYour guess is not in the range of 1 - 20.\n\n";
        answer = true;
    }    
    
    return answer;
}    

int getRandom(void)
{
    int ranNum;
    
    ranNum = rand() % 22 + 1;

    return ranNum;
}

Recommended Answers

All 2 Replies

>how would i make it continuously loop back
Do you really want me to answer this question? Because if I do, I can guarantee that it won't be pleasant for you.

ohhh lol.... ok i wasnt thinking

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.