DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

Thread: Random Number Selection For Game

1. Registered User
Join Date
Nov 2007
Posts
2

Random Number Selection For Game

I WAS GIVEN THIS ASSIGNMENT BUT IT IS NOT YET COMPLETE AND i NEED TO COMPLETE IE URGENTLY

Write an application that plays "Guess the number" as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The program then types:

I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.

The player then types a first guess. The program responds with one of the following:

1. Excellent! You guessed the number on the n th try!
would you like to play again (y/n)?
2. Too low. Try again.
3. Too high. Try again.

If the player's guess is incorrect, your program should display "Too high. Try again" or "Too low. Try again." to help the player. The program then asks for the next guess. When the user enters the correct answer, display "Excellent, you guessed the number on the n th try!", and allow the user to choose whether to play again. Replace n with the number of tries user needed to guess the number right.

WRITE AT LEAST TWO SEPARATE FUNCTIONS IN YOUR PROGRAM! Besides the main function you can for example have a function, which returns a random integer in the range 1 to 1000.

I stopped here but I need to continue

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
srand(time(NULL));
int randomnumber = 1+(rand()%1000);
int guess = 0;
printf("\nI have a number between 1 and 1000.");
printf("\nCan you guess my number?");
do
{
printf("\nPlease type your first guess between 1 and 1000: \n");
scanf("%d", &guess);
if (guess < randomnumber) // If guess is < than the random number, display
printf("Your guess was too low\n\n");

if (guess > randomnumber) // If guess is > than the random number, display
printf("Your guess was too high\n\n");

} while (guess != randomnumber); // While guess is not equal to the random number

printf("\nCongratulations! You've guessed the number.\n");
printf("\nWould you like to play again (Y/N)");
system("PAUSE");
return 0;
}

I need to finish the second part of it urgently

2. Split into its own thread from this thread.

3. Senior Member
Join Date
Dec 2003
Posts
3,366
hints:

rand() returns a random integer.
% is modulo operator, more or less.
Note that rand()%100 gives an integer between 0-99 inclusive.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source