Thread: loops

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    126

    loops

    some help with this one would be good.


    Write a program that asks the user to think of a number between 1 and 100, then attempts to guess the number. The program should make an initial guess of 50. The program should then ask the user if 50 is the number the user has in mind, or if 50 is too high or too low. Based on the response given by the user, the program should make another guess. Your program must continue to guess until the correct number is reached.

  2. #2
    Unregistered
    Guest
    Your best bet it to write a recursive function to do this. Make sure you have a base case where the function does not call itself.

    function(int UserGuess, int CompGuess)
    {
    if( /*when they are the same*/)
    {
    /*they are the same*/
    }
    else
    {
    /*they are't the same so guess again*/
    function(/*new guesses*/);
    }
    }


    Make sure you write the function prototype before you write the function. Have fun

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Why recursive? Do this inside of a do {} while loop, not recursively.

    Use recursive functions for things like fractals, terrain generation, subdivisions, trees, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 12:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM