input validation pt 2

how can I go about adding verification to make sure the user doesn't enter the same number twice.

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
  //main program
int main()
{
  //Data
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  string objective = "play the overUnder game."; //program objective
  string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
  int userGuess; //users guess
  int compGuess = rand() % 100; //random number 

  //user introduction
  introduction(objective, instructions);
  
  do
  {
    //user input
    cout << "What is your guess? " << endl;
    cin >> userGuess; 
    cin.ignore(1000, 10);

    if (userGuess == compGuess)
    {
      cout <<"Thats right -- it's " << compGuess << endl;
      break;
    }
    else if (userGuess > compGuess)
    {
      cout <<"Thats too high -- guess again " << endl;
    }  
    else if (userGuess < compGuess)
    {
      cout <<"Thats too low -- guess again " << endl;
    }
    else
      cout <<"Invalid -- guess again " << endl; 
  }while (userGuess != compGuess);
Keep a vector of previous guesses to check against.
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
// Example program
#include <iostream>
#include <vector>
using namespace std;

// returns true if vec contains value
bool contains(const vector<int>& vec, int value)
{
    for (int num : vec)
    {
        if (num == value)
            return true;
    }
    return false;
}

int main()
{
    vector<int> guesses;
    
    int secret_number = 42;
    
    int num_guesses = 0;   
    while (num_guesses < 5)
    {
        int guess;
        cout << "Guess: ";
        cin >> guess;
        if (contains(guesses, guess))
        {
            cout << "You already guessed " << guess << "!\n";   
        }
        else if (guess == secret_number)
        {
            cout << "You win!\n";
            return 0;
        }
        else
        {
            cout << "Wrong. ";
            guesses.push_back(guess);
            num_guesses++;
        }
    }
    
    cout << "You lose!\n";
}
i haven't learned about how to use vectors at all, is there a way to do it with just arrays and list?
easily.

bool used[101] = {false};
...
get guess..
if(used[guess] == false)
used[guess] = true;
else already seen it
can you explain a little more?

my understanding so far is that i want to

get the guess
see if user has already guessed it

if they have already tried:
try again

else if its a new number they haven't guessed yet
store it into the array of guessed numbers

THEN
check if the answer was to high or too low

im kinda heading in the right direction?

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
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  string objective = "play the overUnder game."; //program objective
  string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
  int gameLog[SIZE];
  int game_count = 0;
  int userGuess; //users guess
  int compGuess = rand() % 10; //random number 

  //user introduction
  introduction(objective, instructions);
  
  while (userGuess != compGuess)
  {
    //user input
    cout << "What is your guess? " << endl;
    cin >> userGuess; 
    cin.ignore(1000, 10);
    gameLog[game_count++] = userGuess; //
    
    //for loop (did user already use that number)
    for (int u = 0; u < SIZE; u++)
    {
      if (userGuess == gameLog[u])
        cout <<"Already entered that number. " << endl;
        break;
    } //for

    //
    if (userGuess == compGuess)
    {
      cout <<"Thats right -- it's " << compGuess << endl;
      break;
    }
    else if (userGuess > compGuess)
    {
      cout <<"Thats too high -- guess again " << endl;
    }  
    else if (userGuess < compGuess)
    {
      cout <<"Thats too low -- guess again " << endl;
    }
    else
    {
      cout <<"Invalid -- guess again " << endl;
    } 
  }
I am just saying to make an array that contains a representative of each possible value, from 1 to 100 but because its indexed from 0 you need 101, see? Now you have one array location for each possible value, all set to false to begin with. as the user puts in values, you set the location to true, so you have seen that one before. They put it in again, you trapped it, you see that you already have used it and can do whatever.

your idea will work too, it is not wrong, but you don't need to loop over a giant list of inputs, you can check it in a single not-looped statement -- is the represented value true or false is all you need to know. Tracking it your way is more work all the way around.

also you have at least one bug:
1
2
3
4
5
6
7
8

for (int u = 0; u < SIZE; u++)
    {
      if (userGuess == gameLog[u])
        cout <<"Already entered that number. " << endl;
        break; //this happens no matter what, ending the loop after 0 is checked, regardless 
       //this break is NOT inside the IF statement!
    } //for 
Last edited on
jonnin's method is better, but it requires that you range-check guess before using it as an index to used.
not working as i want it to when it comes to searching through the array any tips?

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
  srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
  string objective = "play the overUnder game."; //program objective
  string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
  int gameLog[SIZE];
  int userGuess; //users guess
  int nmbSave; //index saver
  int compGuess = rand() % 100; //random number
  bool found = true;
  
  //user introduction
  introduction(objective, instructions);

  do
  {
    //user input
    cout << "What is your guess? " << endl;
    cin >> userGuess; 
    cin.ignore(1000, 10);
     
    //for loop (did user already use that number)
    for (int u = 0; u < SIZE; u++)
    {
      if (userGuess == gameLog[u])
      {
        cout <<"You already entered " << gameLog[u] << "-- Try again " << endl;
        found = true;
        nmbSave = u;
      }
      else
      {
        found = false;
      }
    }     
    
    //
    if (userGuess == compGuess)
      {
        cout <<"Thats right -- it's " << compGuess << endl;
        found = true;
      }
      else if (userGuess > compGuess)
      {
        gameLog[nmbSave] = userGuess;
        cout <<"Thats too high -- guess again " << endl;
        found = true;
      }  
      else if (userGuess < compGuess)
      {
        gameLog[nmbSave] = userGuess;
        cout <<"Thats too low -- guess again " << endl;
        found = true;
      }
  }while (userGuess != compGuess);
}//main
Last edited on
it keeps saying I've entered 2 & 6 before after my first time entering them. It also still shows if its too high or too low even if I have entered it before, how can I stop that?
Make it be bool gameLog[100] = {}; like how jonnin said.

Then you check it like
1
2
3
4
5
6
7
8
9
if (gameLog[userGuess])
{
    // Already entered
}
else
{
    gameLog[userGuess] = true;
    // Handle new guess ...
}


Alternatively, keep it the way you have it, but don't set found = false on line 31. Set found to false on line 19, and then only after set it to true within the for loop. If, after the for loop is over, it is still set to false, that means it's a new guess.
Last edited on
thank you guys for all the help
Last edited on
Topic archived. No new replies allowed.