How to stop user from entering a same number twice?

Write your question here.

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  #include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;


const int MIN_NUMBER = 1;
const int MAX_NUMBER = 100;
const int EXIT_VALUE = -1;
const int MAX_GAMES = 256;
const int loopexit = 2;
int input;
double games = 0;
double wins = 0;
double guesses = 0;
int loopnumber = 0;
char yn;
int answer;


int main(int, char**) {
    
    srand(time(NULL));
     int random_number = rand() % MAX_NUMBER + 1;
    
    cout << "*** You are playing the CSC100 Guessing Game ***" << endl;
    cout << endl;
    
    
    do {
        cout << "Enter a number between 1 and 100 (-1 to give up):" << endl;
        cin >> input;
        
            if(input == EXIT_VALUE){
            games += 1;
            cout << "*** QUITTER ***" << endl;
            cout << endl;
            cout << "Do you want to play again? (y/n): " << endl;
            cin >> yn;
            if (yn == 'y'){
                break;
            }else{
                loopnumber +=2;
                
            }
            
            }else if(games == MAX_GAMES){
                loopnumber += 2;
                
                
            }else if(input < EXIT_VALUE){
            cout << input << " is too small..." << endl;
            
        }else if(input > MAX_NUMBER){
            cout << input << " is too big..." << endl;
            
        }else if(input <= MAX_NUMBER && input > random_number){
            guesses += 1;
            
            if(guesses > 5){
                cout << "nope...lower" << endl;
                
            }else if(){   //this is the part that I need help with..
                guesses -= 1;
                cout << "what part of nope don't you understand?" << endl;
                
            }else{
                cout << "nope..." << endl;
            }
            
            
        }else if(input >= MIN_NUMBER && input < random_number){
            guesses += 1;
            
            if(guesses >5){
                cout << "nope...higher" << endl;
                
            }else if(){   //this is the part that I need help with..
                guesses -= 1;
                cout << "what part of nope don't you understand?" << endl;
                
            }else{
                cout << "nope..." << endl;
            }
        }else{
            games += 1;
            wins += 1;
            cout << "you won" << endl;
            cout << "it took you " << guesses << " guesses" << endl;
            guesses = 0;
            cout << endl;
            cout << "Do you want to play again? (y/n): " << endl;
            cin >> yn;
            if (yn == 'y'){
                continue;
            }else if (yn == 'n'){
                loopnumber +=2;
                
            }
            
        }
        
        
        
        
        
        
        
    }while(loopnumber < loopexit);
    
    cout << "Thanks for playing the CSC100 guessing game." << endl;
    cout << endl;
    cout << "You played " << games <<" games and won " << wins << " of them."
    << endl;
    cout << "Your winning percentage is " << wins/games*100 << "%" << endl;
    
    
    
    
    
    
}
Last edited on
Sorry, I forgot to write my question again in the top.
I couldn't find a way to make the program identify when a number is entered twice in a row.
Please help...
Use a std::set<int> to hold the numbers which were already entered, and check if the new number which was entered is present in the set. Something like this:

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
#include <iostream>
#include <set> // http://en.cppreference.com/w/cpp/container/set

int get_unique_number( int minv, int maxv, int exit_value, const std::set<int>& numbers_already_entered )
{
    // http://en.cppreference.com/w/cpp/container/set/empty
    if( !numbers_already_entered.empty() ) // if some numbers were already entered
    {
        // display those numbers
        std::cout << "these numbers have been already entered: [ " ;
        // http://www.stroustrup.com/C++11FAQ.html#for
        for( int n : numbers_already_entered ) std::cout << n << ' ' ;
        std::cout << "]\n" ;
    }

    std::cout << "enter a number between " << minv << " and " << maxv
              << " ( " << exit_value << " to give up): " ;
    int number ;
    if( std::cin >> number ) // if the user entered an integer
    {
        if( number == exit_value ) return number ; // if the user entered exit_value return it

        // otherwise, check for out of range number
        else if( number < minv ) std::cout << number << " is too small\n" ;
        else if( number > maxv ) std::cout << number << " is too big\n" ;

        // otherwise check if this number was already entered earlier
        // http://en.cppreference.com/w/cpp/container/set/find
        else if( numbers_already_entered.find(number) != numbers_already_entered.end() )
            std::cout << number << " was already entered earlier\n" ;

        else return number ; // ok, entered value is unique and within range, return it
    }

    else // bad non-integer input
    {
        std::cout << "that is not a number\n" ;

        // http://en.cppreference.com/w/cpp/io/basic_ios/clear
        std::cin.clear() ; // clear the failed state of the input stream

        // http://en.cppreference.com/w/cpp/io/basic_istream/ignore
        std::cin.ignore( 1000, '\n' ) ; // throw away the bad input
    }

    return get_unique_number( minv, maxv, exit_value, numbers_already_entered ) ;
}

int main() // simple test driver
{
    const int MIN_NUMBER = 1;
    const int MAX_NUMBER = 100;
    const int EXIT_VALUE = -1;

    std::set<int> numbers_already_entered ;

    int number ;
    // loop till exit_value is entered
    while( ( number = get_unique_number( MIN_NUMBER, MAX_NUMBER, EXIT_VALUE, numbers_already_entered ) ) != -1 )
    {
        // add this number to the set of already entered numbers
        // http://en.cppreference.com/w/cpp/container/set/insert
        numbers_already_entered.insert(number) ;

        std::cout << "you entered " << number << '\n' ;
    }
}
Topic archived. No new replies allowed.