Hi Guys. i am trying to write a small guessing game program. The program asks a user for a range of values (max and min). The program generates a random number within this range and the player has to guess this number. A counter is also kept. I am having difficulty creating a random number between the two values. Please see my code below. Any suggestions? Thanks
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(int argc, char *argv[]) {
int randomNumber = 0;
int guess = 0;
int maxnumber;
int minnumber;
cout << "Please Enter a Max Number \n";
cin >> maxnumber;
cout << "Please Enter a Min Number \n";
cin >> minnumber;
// Create random number between min and max values
srand(time(NULL));
randomNumber = (rand() % (maxnumber - minnumber + 1)) + maxnumber;
// Loops forever until user finds the number
while (guess != randomNumber)
{
cout<<"Pick a number : ";
cin >> guess;
if (randomNumber > guess)
cout << "Higher!"<<endl<<endl;
else if (randomNumber < guess)
cout << "Lower!"<<endl<<endl;
else
cout << "Good job! You found the number!"<<endl<<endl;
}
return 0;
}
Small Number guessing gameNumber guessing game
Page 1 of 1
2 Replies - 2311 Views - Last Post: 13 October 2009 - 01:31 PM
Replies To: Small Number guessing game
#2
Re: Small Number guessing game
Posted 13 October 2009 - 09:23 AM
do this
int div = maxNumber-minNumber+1;
randomNumber = rand()%div;
randomNumber += minNumber;
How it works
lets suppose minNumber= 20;
maxnumber = 30;
now div = 11;
when i do rand()%11 a number between 0-10 is stored in randomNumber.
When i add it to minNumber randomNumber will be b/w 20-30.
Hope this works for u!
Do thank me if u find the post useful!
int div = maxNumber-minNumber+1;
randomNumber = rand()%div;
randomNumber += minNumber;
How it works
lets suppose minNumber= 20;
maxnumber = 30;
now div = 11;
when i do rand()%11 a number between 0-10 is stored in randomNumber.
When i add it to minNumber randomNumber will be b/w 20-30.
Hope this works for u!
Do thank me if u find the post useful!
This post has been edited by Shamayla: 13 October 2009 - 09:27 AM
#3
Re: Small Number guessing game
Posted 13 October 2009 - 01:31 PM
Shamayla, on 13 Oct, 2009 - 08:23 AM, said:
do this
int div = maxNumber-minNumber+1;
randomNumber = rand()%div;
randomNumber += minNumber;
How it works
lets suppose minNumber= 20;
maxnumber = 30;
now div = 11;
when i do rand()%11 a number between 0-10 is stored in randomNumber.
When i add it to minNumber randomNumber will be b/w 20-30.
Hope this works for u!
Do thank me if u find the post useful!
int div = maxNumber-minNumber+1;
randomNumber = rand()%div;
randomNumber += minNumber;
How it works
lets suppose minNumber= 20;
maxnumber = 30;
now div = 11;
when i do rand()%11 a number between 0-10 is stored in randomNumber.
When i add it to minNumber randomNumber will be b/w 20-30.
Hope this works for u!
Do thank me if u find the post useful!
Hey thanks for the reply. The code works but i don't understand it! Why are we assigning int div teh value of the max number minus the min number with one added to it?Also why is a number stored between 0-10 when u do rand()%11? Sorry im a newbie!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|