Thread: Please help me with this guessing game.

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    4

    Please help me with this guessing game.

    So I am going through the "Jumping into C++" ebook and I have hit a road block with random numbers.... I want to make two separate random number choosers, one that will be the answer and another to be a random guesser to keep guessing until it guesses that same number, but when I have my program run it just guesses the same number an infinite amount of times and I get hit into a infinite loop. Can anyone help me make the guesser choose different numbers every time it runs the loop... I am stumped
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        srand(time(0));
        int answer = (rand() % 10) + 1;
        int choice = (rand() % 10) + 1;
        int newChoice = 0;
        int attempts = 1;
    
        cout << "Hello, Computer. How many attempts will it take you to guess my number?" << endl;
        cout << answer << endl;
    
        while(answer != choice){
                choice
                cout << "Attempt: " << attempts << " You guessed: " << choice << endl;
                attempts++;
            }
    
            cout << "Congratulations Computer!" << endl;
            cout << "You guessed " << attempts << " times.";
    
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
                choice
    Please explain what you think line 19 does?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    4
    Sorry that was a typo. I tried seeing if I recalled it when the loop restarted if it would change anything then forgot to delete it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This should be a no-brainer. You know how generate a random number, so what is stopping you from putting it in the program?
    I mean, take a look at the logic and tell me how to achieve your objective, logically speaking.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    C++ isn't that sort of language. Expressions do not naturally bind to a variable so that "recalling" the variable executes a bound expression.

    C++ is a "multi-paradigm" language, but the language is largerly "imperative"; you must actually code the expression where you expect one to execute.

    [Edit]
    The fact that you think expressions work the way your code and comments imply suggest that you have moved to fast through "Jumping into C++" for your own good.

    You should start back at the beginning so that you may better pace your understanding of the language.
    [/Edit]

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. guessing game
    By GaitBait in forum C Programming
    Replies: 2
    Last Post: 10-03-2013, 12:29 AM
  2. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  3. guessing game
    By Sal79 in forum C Programming
    Replies: 14
    Last Post: 05-09-2007, 02:22 PM
  4. A guessing game
    By Lyanette in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 10:02 AM
  5. guessing game
    By wayko in forum C++ Programming
    Replies: 11
    Last Post: 09-19-2001, 06:10 PM