C++ How does one use Array Input Checking?

Hey guys, as I'm googling for help on my issue, I came across a user saying that if I wanted to check if an user input has already been inputted, I'd have to put the user input variable into an array, iterate it through, and check with any current input; and, if positive, I would have to move one element back to continue. How would I go about doing this? Are there similar examples that I can analyze and model off from? If so, please provide the links!

Just for a reference, I'm trying to make a basic number guessing game, where program generates a random number and the user must guess its number.

Also here's my current code for reference:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 #include <iomanip>
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

struct Guess
{
    int userGuess;
}; //Guess

int main()
{
    const int MAX_GUESSES = 100;  //list capacity
    int nGUESS = 0; // initially empty list
    Guess userGuess[MAX_GUESSES]; // the array

    //variables
    int randomNumber = rand() % 100 + 1; //generating random number
    int userinput;  //user's input

    cout << "Welcome to the Random Number Guessing Game!" << endl;
    cout << "Guess a number from [1 - 100] to begin playing! " << endl;


    do
    {
        Guess aGuess; //temporary variable

        cin >> userinput; // inputting user input

        if (userinput > randomNumber)
        {
            cout << "That's too high!" << endl;

            if (nGUESS < MAX_GUESSES)
            {
                userGuess[nGUESS++] = aGuess;
            } // Adding record to list if it's not full
        }// if
        else if (userinput < randomNumber)
        {
            cout << "That's too low!" << endl;
            if (nGUESS < MAX_GUESSES)
            {
                userGuess[nGUESS++] = aGuess;
            } // Adding record to list if it's not full
        } // else if
        else
        {
            cout << "That's correct!" << endl;
            if (nGUESS < MAX_GUESSES)
            {
                userGuess[nGUESS++] = aGuess;
            } // Adding record to list if it's not full
        } // else

        //Sorting if user input as been inputted already 
        for (int i = 0; i < nGUESS; i++)
        {
            for (int j = i + 1; j < nGUESS; j++)
            {
                if (userGuess[i].userGuess > userGuess[j].userGuess)
                {
                    Guess temp = userGuess[i];
                    userGuess[i] = userGuess[j];
                    userGuess[j] = temp;

                    if (temp = userinput)
                    {
                        cout << "You already guessed that" << endl;
                    }
                }
            }
        }
    }while (userinput != randomNumber); //do

} //main 


Constructive criticism is welcomed!
You can use a std::set which already prevents duplicates and is designed for the job:
http://www.cplusplus.com/reference/set/set/
http://en.cppreference.com/w/cpp/container/set

I strongly recomment never using plain arrays in C++ - at the very least use std::vector instead of a plain array:
http://www.cplusplus.com/reference/vector/vector/
http://en.cppreference.com/w/cpp/container/vector

std::list seems like it would be what you want but it is not - it's actually a poorly named linked list and is generally slower than std::vector (which is also poorly named).
Last edited on
Unfortunately, I can't use the aforementioned shortcuts. My professor won't accept anything "complex" and "to- the-point." I just need a simple model or example of array input validation being used in a code, so I can analyze and model mine after it.
Thanks for the feedback though!
Plain arrays are far more complex and tricky to use than simply using a vector or a set. Has your professor explicitly forbidden using std::vector and std::set? If so, you are effectively asking for C help on a C++ forum.
Last edited on
Topic archived. No new replies allowed.