Loop within function doesn't seem to matter?

Hi! I'm creating a program which has a section that the user is prompted to input a number. The number has to be from 1-10 or the program loops an error message which prompts the user to re-enter an input. Once the user enters a valid number from 1-10, the program then proceeds.

The actual validation that the number is 1-10 takes place in a separate function, titled "int validate". This function is called from "int main" like so:

1
2
3
4
5
6
        system("CLS");
        cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n"   
             << "\t\t Please enter a number from 1-10: ";
        cin >> number;             
        validate(number);



and the actual code inside int validate is:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int validate(int number)
{

    //loop until a valid score is entered...
    while (number < 1 || number > 4)
    {
          system("CLS");
          cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
          cout << "\t\t\t\t  ";                
          cout << number << " is invalid!";
          cout << "\n\t\t   Please enter a value in the range 1-10: ";
          cin >> number;
          cout << "\n\n\n\n\n\t\t"; 
    }

}



What ends up happening, however, is that the user's first input from int main will be remembered. Even though int validate will go on forever until the user enters something from 1-10, the program will continue to run as if that never happened, and it just responds to the first (can be invalid) input.

Does anyone have suggestions? Thank you!
Last edited on
You would have to pass the number by reference, otherwise (as you saw) you will be modifying a copy.

And also...don't use system(), it's not standard across all OSs so system("cls"); might not work everywhere.
I don't understand how I would achieve this, unfortunately.

Would I type
return number;
at the end of int validate or something?

I'm having troubles...
Last edited on
Topic archived. No new replies allowed.