Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I've learned more from your forums in 3 days than 3 months at school and on the job combined..."

Geography

Where in the world do Tek-Tips members come from?

Trying to teach myself C++, please help

joec3rd (Programmer)
22 Oct 07 14:37
below is a simple program i am trying to figure out as i teach my self c++ from this wonderful book i bought. currently my biggest problem is the loop it creates. Once i figure that out i know there is going to be a problem with the math in it, but that should be easier. This is suppose to make a program/game where the player picks a number between 1 and 100 and the computer tries to guess it. my problem is when it while loop starts it never stops and waits for the user to input any of the cin variables. How do i make it wait for those?
thanks in advance for any info and help

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));//seeds random number generator

    int compguess = rand() % 100 + 1; //generates random number between 1 and 100
    
    bool playeranswer;
    bool yes = true, no = false;
    playeranswer = no;
    
    int reguess = 1;
    
    int newcompGuess;
    
    bool updown;
    bool high = true, low = false;
    updown = high;
    
    char playerIsReady;
    
    int newcompguess;

    cout << "Pick a number between 1 and 100 and I will try to guess it!\n\n"<<endl;
    cout << "Do you have your number?\n";
    cin >> playerIsReady;

    while (playeranswer == no)
    {
        cout << "Is your number " << compguess << "?";
        cin >> playeranswer;
            if (playeranswer == yes)
                break;
            cout << "Am I to High or to Low?\n"<<endl;
            cin >> updown;    
        
            if (updown == 1)
            {
            newcompguess = rand() % compguess + 1; //generates random number between 1 and previous guess
            compguess = newcompguess;
            }

            if (updown == 0)
            {
            newcompguess = rand() % 100 + compguess; //generates random number between previous and 100
            compguess = newcompguess;
            }  
    }

    cout << "I guessed it!!!!\n";

    return 0;
}
uolj (Programmer)
22 Oct 07 17:59
Are you expecting the user to type "yes" or "no"? If so, you want the answer to be a string (use the C++ std::string type in <string>). Then compare to "no" or "yes" with the double quotes that indicate a string.

playeranswer is a bool variable. The only acceptable input for a bool variable is 0 or 1 (or possibly "true" or "false" if you use boolalpha). Anything else and you will get a read error and playeranswer will not be set. Since you didn't initialize it, it could be set to anything, including false like no is. If you type 1 or 0 instead of "yes" or "no", your current code works better.

Also, please use [code][/code] tags when posting code.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close