0

I have an assignment in CSC 101 where I must write a program where the user must think of a number between 1 and 19 inclusively, and the computer must guess it within 5 tries. Right now the program prompts me twice to enter high or low.

I have sort of gotten a template built using if statements, but I believe switch will work better. I can't think of another way to give the program the conditional to check if I gave it high or low. I don't want it to default to an error statement if the user forgot to hit space or something. That's why I have the prompt again.

  cout << "is this your guess? Answer yes or no:" << guess << endl; 
    cin >> yesno;
    if (yesno != "yes" || "no") {
        cout << "Please answer only yes or no" << endl;
        cin >> yesno;
    }

    if (yesno == "no") 
    {
        cout << "Too high or too low? Answer too high or too low" << endl;
        cin >> highlow;
        if (highlow == "too high")
            guess = guess - 5;
        if (highlow == "too low")
            guess = guess + 5;
        if (highlow != "too high" || "too low")
        {
            cout << "Please anwer only too high or too low" << endl;
            cin >> highlow;
        }

It seems to always enter the last if statement in the yes==no block and it prompts me twice to enter too high or too low. It doesn't do it in my top block. I expect it to only ask me again if I don't input "too high" or "too low" Thanks in advance guys, any help is really appreciated.

  • Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Nov 12 '19 at 16:31
  • 7
    `if (yesno != "yes" || "no") {` This doesn't do what you think it does – hlscalon Nov 12 '19 at 16:35
  • 2
    Your question lacks [mcve], but a couple of issues stand out: `cin >> highlow;` will not read two words, it will only read up to first whitespace and `highlow != "too high" || "too low"` is always true. – Yksisarvinen Nov 12 '19 at 16:36
  • Oh! thanks guys, didn't think about the space in the input. I'll change that and see where I can go from there. – killerkody gaming Nov 12 '19 at 16:38
  • Before you proceed to using `getline` to read full line, see [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) (unless you have your own idea how to get two words from input, then disregard this comment :) ) – Yksisarvinen Nov 12 '19 at 16:46
  • If my number is 18, and the computer guesses 17, is the next guess 22? Or if I my number is 2 and the computer guesses 3, is the next guess -2? – sweenish Nov 12 '19 at 17:42

1 Answers1

1
  cout << "is this your guess? Answer yes or no:" << guess << endl; 
    cin >> yesno;
    if (yesno != "yes" || "no") {  // ***Problem here
        cout << "Please answer only yes or no" << endl;
        cin >> yesno;
    }

    if (yesno == "no") 
    {
        cout << "Too high or too low? Answer too high or too low" << endl;
        cin >> highlow;
        if (highlow == "too high")
            guess = guess - 5;
        if (highlow == "too low")
            guess = guess + 5;
        if (highlow != "too high" || "too low")  // ***Same problem
        {
            cout << "Please anwer only too high or too low" << endl;
            cin >> highlow;
        }

To answer just your question, the issues lie mostly in those Boolean expressions. There are two major issues. In this line:
highlow != "too high" || "too low"
you are asking is the expression highlow != "too high true, OR is the expression "too low" true. The issue comes from that second one. Both sides of your logical OR must be complete expressions. Otherwise what's happening is that "too low" is just evaluated as true because it is non-zero/non-null. So that Boolean expression will always evaluate to true.

The quickest fix is to change those lines (NOTE: Still broken at this point):

yesno != "yes" || yesno != "no"  // Using || on this line is wrong

highlow != "too high" || highlow != "too low"  // || is wrong here

The second issue is your use of ||. Recall that for logical OR, only one argument needs to be true for the entire thing to be true. So consider:
yesno != "yes" || yesno != "no"

If I type no, then it is true that yesno does not equal "yes". I will get prompted to put in something valid even though I did. So you are currently flagging valid and invalid input as invalid. There are a couple ways to fix this. The fastest is to change || to &&.
yesno != "yes" && yesno != "no"

Now if I type no, the first case is still true, but the second case is false. And because logical AND is false if any argument is false, it returns false. Which is correct because no is a valid input. Another approach is to avoid overly negative logic.
!(yesno == "yes" || yesno == "no")

This just an application of DeMorgan's Law to the above expression (If your current course hasn't covered logical expressions, I'm assuming another course will). The Boolean expression is easier to understand, "Is yesno any one of my valid values?" And then it's negated, because you're checking if the input is invalid.

The final issue is when you asking about "too low" or "too high". std::cin only reads until it hits whitespace. You will want to use std::getline(std::cin, std::string) to read that in. To avoid the headaches that come with mixing std::cin and std::getline, just use std::getline all the time for this assignment.

That should answer the question you've asked about. There are other issues in your logic, like adding or subracting 5 blindly. There's a smarter approach that guarantees the computer always guesses your number in at most 5 attempts. It involves keeping track of your valid range and making smart(er) guesses each time.

sweenish
  • 3,215
  • 2
  • 10
  • 19