Re-enter a user input

This is a portion of my code for a game. If the user enters a number > 9 it does cout the response however it just keeps going. It does not stop the program and allow the user to re enter the number. My question is how do I allow the user to re enter the number whenever they enter the wrong thing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
{
 if ( number <= 9) // 
   {
    cout << "That number works. Next player" << endl;
    return 1; 
   } 

 else  if (num1 > 9);
   {
 cout << "That number is invalid. Please make sure you enter a number between 1 and 9" << endl;
return 0; 
   }
}


Also is there a way for me to stop the user from entering the same number twice without using a vector. For example if the user enters 9 and then then they enter 9 again it will cout something like " you entered that number already" and then allow them to enter a different number.
Hope I was clear enough thank you in advance.
Try using a while loop
1
2
3
4
5
while (number > 9){
cout << "That number is invalid. Please make sure you enter a number between 1 and 9" << endl;
}
cout << "That number works. Next player" << endl;
return 1; 
player 1 pick your number
10
That number is invalid. Please make sure you enter a number between 1 and 9
0 | 1 | 10
------------
9| 2 | 8
------------
6 | 3| 7
No winner yet! Next player
Player 2 pick your spot! (x,y)


Used the while loop @McNo but it still keeps playing after it states the number is invalid
Could you post a bit more of your code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <iostream>
#include "playgame.h"
#include "validate.h"
using namespace std; 

bool play(int number, int vortex[3][3]) 
{
int x, y;

  for (int i=0; i<3; i++) 
    {
      for (int j=0; j<3; j++) 
      {

cout << "Player 1 choose your position(x,y)" << endl;
cin >> x >> y;

cout << "player 1 pick your number" << endl; 
cin >> number; 

vortex [x][y]  = number;
validate (number);
display (vortex);
whowins(vortex);
	
cout << "Player 2 pick your coordinates! (x,y)" << endl;
cin >> x >> y; // Postion for player 2

cout << "Player 2 pick your number" << endl;
cin >> number; // number that is being played by player 2
 vortex[x][y] = num1;
validate (num1);
display (matrix);

if (checkforwin (vortex) == false);{
return 0; }
      }
    }
}

       
bool validate (int number) // Validate function
{
 while (num1 > 9){
cout << "That number is invalid. Please make sure you enter a number between 1 and 9" << endl;
 return 0;
}
cout << "That number works. Next player" << endl;
return 1; 
}
Thanks. Try instead of validate (number); using
1
2
3
while (!validate(number)) {
cin >> number;
}

And switch your bool validate (int number) function to the way you had it before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
{
 if ( number <= 9) // 
   {
    cout << "That number works. Next player" << endl;
    return 1; 
   } 

 else  if (num1 > 9);
   {
 cout << "That number is invalid. Please make sure you enter a number between 1 and 9" << endl;
return 0; 
   }
}
This way the user must enter a valid input before your program continues.
Last edited on
player 1 pick your number
Good job! That number works. Next player
0 | 0 | 0
------------
0 | 0 | 0
------------
0 | 0 | 0
No winner , keep playing!

Tried that but it won't let me input the number now
Bump still having this problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// I used doubles so it works for both int and double values
bool Validate(double value, double low, double high)
{
    if(value < low || value > high) return 0;
    return 1;
}

void GetInput(const std::string& prompt, int& value)
{
    while(std::cout << prompt << ": ", std::cin >> value)
    {
        if(Validate(value,1,9)) return;
    }
    std::cout << "Error: Wrong data type or failure on stdin!";
}

int main()
{
    int i;
    GetInput("Enter a number between 1 and 9",i);
    std::cout << "You entered << i << "\n";
} 


Hope this helps.
The code that i showed was only for one of my .cpp files I thought I could make it work by only editing my bool validate function. I'll try to implement what you said though.
Try reorganising your code, it seems very bloated to me and you make simple functions bulky.

Also could you explain what that for-loop is for in bool play()? I don't see i or j used anywhere?

Also you may want to check the input on cin when asking the user for coordinates. It's screaming out of bounds exception when you write vortex[x][y].

If you need any more help just ask.
Topic archived. No new replies allowed.