C++ Game with Random number Generator. HELP!!!

Jan 9, 2019 at 1:51am
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
Here is a guess my number game program.
I've been trying to change this program so that instead of telling the user if their number guess is too high or low, it tells them how many digits in the guess is correct.

Here's what I have so far...

#include<iostream>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main(){

    srand(time(NULL));
    int rnum = rand()%1000 + 1;
    int usernum;
    int tries = 0;

    cout << " I am thinking of a number between 1 and 1000. Enter a number you 
    think I'm thinking of!" << endl;

    while(usernum != rnum){

        cin >> usernum;

        if(usernum ){
            cout << " Your guess is to high! Guess again." << endl;
            tries++;
        }
        else if(usernum < rnum){
            cout << " Your number is to low! Guess again." << endl;
            tries++;
        }
    if(usernum == rnum){
        cout << " You are correct!!! That took you " << tries + 1 << " tries. << endl;
        break;
    }

}

}





[code] 
[/code]
[/code]
Last edited on Jan 9, 2019 at 7:01am
Jan 9, 2019 at 7:58am
I would suggest that you turn the numbers into strings and compare them character by character.
1
2
    std::string rnum = std::to_string(rand()%1000 + 1);
    std::string usernum;
Jan 9, 2019 at 2:27pm
You could try something like this maybe:

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
#include<iostream>
#include <cstdlib>   // rand, srand
#include<time.h>

using namespace std;

int main(){

        srand(time(NULL));
        string randy = to_string(rand()%1000 + 1);

        cout << "\nThe random number is:\t" << randy << endl;

        char usernum[4]={'*','*','*','*'};

        cout << "Enter number (1-1000):\t";
        cin >> usernum;

        short match=0, count=0;

        do {
         size_t found = randy.find(usernum[count]);
         if (found!=std::string::npos) match++;
         count++;
        } while (count <=3 && usernum[count]!='*');

        cout << "There were " << match << " number matches\n\n";

return 0;
}


Example output:
The random number is:	960
Enter number (1-1000):	967
There were 2 number matches

The random number is:	204
Enter number (1-1000):	401
There were 2 number matches

The random number is:	883
Enter number (1-1000):	109
There were 0 number matches

I'm sure there are probably some better and/or different ways of doing it but maybe it'll give you some ideas when you write your own code.
Last edited on Jan 9, 2019 at 7:38pm
Jan 9, 2019 at 3:25pm
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
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
using namespace std;


mt19937 gen( chrono::system_clock::now().time_since_epoch().count() );


class Game
{
   int target;
   vector<int> digits;

public:
   Game( int nmax );
   int compare( int n );
   int getTarget(){ return target; };
};


Game::Game( int nmax )
{
   uniform_int_distribution<int> dist( 1, nmax );
   target = dist( gen );
   digits = vector<int>( 10, 0 );
   for ( int n = target; n; n /= 10 ) digits[ n % 10 ]++;
}


int Game::compare( int n )
{
   int result = 0;
   vector<int> temp = digits;
   for ( ; n; n /= 10 ) 
   {
      int d = n % 10;
      if ( temp[d] ) { result++; temp[d]--; }
   }
   return result;
}


int main()
{
   const int NMAX = 1000000;
   int guess;
   Game g( NMAX );

   while ( true )
   {
      cout << "Enter a guess in the range 1 - " << NMAX << " (0 to finish): ";
      cin >> guess;   if ( guess <= 0 ) break;
      cout << "You got " << g.compare( guess ) << " digits right\n\n";
   }

   cout << "The correct answer is " << g.getTarget() << '\n';
}




Enter a guess in the range 1 - 1000000 (0 to finish): 123456
You got 3 digits right

Enter a guess in the range 1 - 1000000 (0 to finish): 789789
You got 0 digits right

Enter a guess in the range 1 - 1000000 (0 to finish): 343434
You got 1 digits right

Enter a guess in the range 1 - 1000000 (0 to finish): 56
You got 1 digits right

Enter a guess in the range 1 - 1000000 (0 to finish): 0
The correct answer is 245255
Last edited on Jan 9, 2019 at 3:31pm
Jan 10, 2019 at 2:31am
Ok, working on it. Thanks you guys!
Topic archived. No new replies allowed.