Hello, I am new at c++ and need some help.

The project we were given is to 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 rnind, 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 conthue to guess until the correct number is reached.

I am really stumped on this project. I have the basic logic down but I am missing things, and am not sure how to even contiune or what to put where.. If you can help me figure this out in anyway it would be much appreciated. Thanks c:

This is what I have so far...

// Project 8.4. Guess the number the user enters between 1-100.

#include <iostream>
using namespace std;

int UsersNum, YesNo, HighLow, High, Low, Yes, No;

int main ()
{
    cout << "Welcome to the number guessing game. I will try and guess the number you will enter. \n";
    cout << "Please enter a number between 1 -100. \n";
    cin >> UsersNum;

    cout << "Is your number 50? Yes or No? \n";
    cin >> YesNo;// enter if 50 is correct 

        if ( YesNo = Yes) // if user answers yes
            cout << "I guessed your number! Game Over \n";

        if ( YesNo = No)// if user answers no
            cout << "Is 50 too high or too low? \n";
            cin >> HighLow;

            if (HighLow = High)
                // Guess a lower number
                cout << "Is this the number you entered? Yes or No? \n";
                cin >> YesNo;

            if (HighLow = Low)
                // Guess a lower number
                cout << "Is this the number you entered? Yes or No? \n";
                cin >> YesNo;

                        if (YesNo = Yes)
                                cout << "I have guessed your number! \n";                         

                        if (YesNo = No)
                            // repeat high or low part  

    return 0;
}

Let's start with the two most obvious bugs:

  1. You compare two values with the == operator, not the = operator. The = operator is for assignment.
  2. if statements and loops with more than one statement in the body must be wrapped in parentheses. It'd be a good idea at first to always use parens to avoid subtle bugs. C is not Python, indentation means absolutely nothing to the compiler.

There's quite a bit wrong in your code.

  1. Note that 'int' is for storing integers. You almost seem to want to use it as an enumeration (enum keyword) but you don't initialize the values. When querying for input using '>>' it will try to read an integer because the type is int. if you want to read text you should use "string" instead.

  2. Comparing values is done with the '==' operator, not with the '=' (assignment) operator.

    if ( YesNo = Yes) // if user answers yes

This will assign the value of 'Yes' to 'YesNo' and this expression will return the value of 'Yes'.

  1. The idea of naming options bound in a certain catagory isn't bad, it's generally actually quite good. (for readability) You would use an enumeration for that though. It looks as follows (look it up):

    enum YesNo { Yes, No };

You would then use YesNo as a type. In this specific case I would not create an enum though as C++ has the 'bool' type just for cases like this. It can be 'true' or 'false'.

  1. As far as logic goes it seems somewhat alright. You really miss a loop though. Look for "for" and "while". In pseudocode you program could look as follows:
Let the user enter it's number. (0-100)
Set the computer's guess to 50 the lower boundary to 0 and upper boundary to 100.

As long as the user didn't say the number is guessed:

    Ask the user if the current computer's guess is the right guess.
  
    if the computer's guess is incorrect:
  
        Ask the user if the guessed number is higher or lower.
  
        if the computer's guess is higher:
            set the upper bound equal to the computer's guess.
      
        if the computer's guess is lower:
            set the lower bound equal to the computer's guess.
  
        set the new computer guess equal to the middle of the lower and upper boundary.

Note that this is just quickly setup pseudocode and might contain some flaws, but the general outline should work and serve as a good template.

Edited 4 Years Ago by Gonbe

This article has been dead for over six months. Start a new discussion instead.