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>
usingnamespace 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++;
}
elseif(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]
#include<iostream>
#include <cstdlib> // rand, srand
#include<time.h>
usingnamespace 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.
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
usingnamespace 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()
{
constint 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