How do I tell whether the users input has already been used in a 2d array?

My parameters are that the users input has to be from 1 to 9 and the same number can't be entered twice. How do i modify this code to make sure that the user did those things or an error message should appear that their input is invalid.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;



int main()
{
    int square[3][3];

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

        cout << "Row " << i+1 << " column " << j+1 << " : " ;
        cin >> square[i][j];
    }
}

1. Use an if statement to check if the input is in the desired range before storing in the array.
eg:
1
2
if(temp>=0 && temp<=9)  //note that temp is some integer variable
...


Aceix.
Ok that's easy enough, but how on earth can I make sure that a number hasn't been inputted already?
Ok not easy enough. First of all I'm pretty sure you don't need the temp spot I can just use square[i][j], but if I have it in the loop, it just tells the user the input is invalid and continues instead of going back to redo the previous input. How do I make that happen?
OK. Progress! I've decided to just use a 1d array instead of a 2d. This is a bit easier. I've been able to validate the input, but I don't know how to check to see if a number has already been inputted.
Here's what I have:

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
#include <iostream>
using namespace std;



int main()
{
    int square[9];
    int x =0;

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

        cout << "Row " << i+1 << " column " << j+1 << " : ";
        cin >> square[x];
        
        if (square[x] < 1 or square[x] > 9)
        {
        cout << "Invalid input! ";              
        j=j-1;              
        }
        x++;
        
    }
    
}
If you persist on the removal of the temp var idea, then...
You could store the input in the array, then iterate through the array and check for any match with current input. If positive, move back one element otherwise continue.
(would be a bit messy)

Aceix.
The way you have it, if there is an invalid input you might start causing some segmentation faults. Here is the normal way of accessing a 1D array as if its a 2D array:
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
#include <iostream>
#include <algorithm>

int main() {
    int square[3 * 3];
    for (int row = 0; row < 3; ++row) {
        for (int col = 0; col < 3; ++col) {
            int temp;

            std::cout << "Row: " << row << ", Col: " << col << " = ";
            std::cin >> temp;

            if (temp < 1 || temp > 9) {
                std::cout << "Invalid Input! Must be between 1 and 9!\n";
                --col;
            } else if (std::find(square, square + 9, temp) != square + 9) {
                std::cout << "Invalid Input! " << temp << " has already been given!\n";
                --col;
            } else
                square[col + 3*row] = temp;
        }
    }

    // etc.

    return 0;
}
Topic archived. No new replies allowed.