Random Number Selection For Game


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Random Number Selection For Game

  1. #1
    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. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Split into its own thread from this thread.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    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.

Similar Threads

  1. Replies: 0
    Last Post: 07-05-2007, 11:12 AM
  2. Random number help??
    By vbjunkie1 in forum C++
    Replies: 5
    Last Post: 03-24-2007, 03:13 AM
  3. Replies: 18
    Last Post: 12-13-2006, 02:49 PM
  4. Help with Random number and text Box
    By cmvw in forum VB Classic
    Replies: 7
    Last Post: 06-28-2006, 10:04 AM
  5. Random Number Script
    By a8le in forum .NET
    Replies: 0
    Last Post: 10-16-2005, 01:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center