HELP for incorrect input

Hi, I am wanting to have it so that if a player does not enter a number between 1-9 then it should output "incorrect input", I am unsure on the way of going about this and am hoping for insight by someone who does.

1
2
3
4
5
6
7
8
9
10
11
	//integer for input
	int choice4;
	//players turn
	cout << endl;
	cout << "It's " << player << "'s turn. " << "Type the number of the field: ";
	cin >> choice4;

	choice4--;
	grid[choice4 / 3][choice4 % 3] = player;
        
        //incorrect input statement 
what i would do is create awhile loop nested with a for loop


1
2
3
4
5
6
7
8
9
10
while(choice4)
{
      if(choice4 > 1 && choice4 < 9)
        {
            cout << "invalid " << endl;
             // continue with the adjustments

           }
}
Sorry, but I don't see what you mean?
Just use an if statement.

1
2
3
4
5
6
 if(choice4 < 1 || choice4 > 9)
        {
            cout << "invalid " << endl;
             // continue with the adjustments

           }


If you want to force them to give a valid input use a while loop.
Ok this is what I have, although there's a problem, with if a letter is inputted then the program repeats the Input() function infinitely, any way around this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	//integer for input
	int choice4;
	//players turn
	cout << endl;
	cout << "It's " << player << "'s turn. " << "Type the number of the field: ";
	cin >> choice4;

	if (choice4 < 1 || choice4 > 9)
	{
		cout << "Invalid Input." << endl;
		Input();
	}
	else
	{
		choice4--;
		grid[choice4 / 3][choice4 % 3] = player;
	}
Is there a way I can get it so that only the number 1-9 can be inputted, anything else will output Invalid Input?
Last edited on

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
#include<iostream>

int getChoice(int min, int max, const std::string &prompt)
{
    int choice;
    bool valid = false;
    while(!valid)
    { 
        std::cout << prompt;
        std::cin >> choice;
        if(choice >= min && choice <= max)
        {
            valid = true;
        }
        else
        {
            std::cout << "Invalid input: " << choice << std::endl;
        }
    }
}

int main()
{
    int user_choice = getChoice(1, 9, "Pick a number between 1-9: ");
}


There's a generic solution assuming you're only asking for integers, and your valid input is a contiguous range of integers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Input()
{
	//integer for input
	int choice4;
	//players turn
	cout << endl;
	cout << "It's " << player << "'s turn. " << "Type the number of the field: ";
	cin >> choice4;

	if (choice4/*Not between 1 and 9*/)
	{
		cout << "Invalid Input." << endl;
		Input();
	}
	else
	{ 
		//edits grid, if 2 replaces 2 field with X or O
		choice4--;
		grid[choice4 / 3][choice4 % 3] = player;
	}

}

This is the input function where a player must enter between 1 and 9 and if not then it states that it is not valid input, I don't see how I can adjust my code in the way you have displayed
I literally gave a generic simple function to do what you want to do, and then gave you an example of how to use it. All you have to do is copy getChoice essentially.
Last edited on
Topic archived. No new replies allowed.