5 Replies - 189 Views - Last Post: 03 February 2018 - 03:24 PM Rate Topic: -----

#1 kianyt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 18

Need Help with Higher-Lower game

Posted 03 February 2018 - 01:36 PM

Hi, I'm trying to make a higher lower game, but whenever I put a number higher than the right number it won't say what I've told it to, help pls

#include <iostream>
using namespace std;

int main(){
int loop = 0;

    cout << "Choose a number between 1 and 100" << endl;
    while(loop!=69)
    cin >> loop;
        if (loop<69){
            cout << "Too Low" << endl;}      // This Doesn't work
            else if (loop>69){
                cout << "Too High" << endl;} // This Doesn't work
                else {
                    cout << "Thats the RIGHT NUMBER!";}




    return 0;

}



Is This A Good Question/Topic? 0
  • +

Replies To: Need Help with Higher-Lower game

#2 Thomas1965   User is offline

  • D.I.C Regular

Reputation: 95
  • View blog
  • Posts: 365
  • Joined: 09-September 16

Re: Need Help with Higher-Lower game

Posted 03 February 2018 - 02:09 PM

If you format you code properly it would be pretty obvious.
#include <iostream>

using namespace std;

int main()
{
    int loop = 0;

    cout << "Choose a number between 1 and 100" << endl;
    while(loop != 69)
      cin >> loop;
        
    if (loop<69)
    {
      cout << "Too Low" << endl;
    }      // This Doesn't work
    else if (loop > 69)
    {
      cout << "Too High" << endl;
    } // This Doesn't work
    else 
    {
      cout << "Thats the RIGHT NUMBER!";
    }
    return 0;
}


What is this value of 69 for?
How is this code supposed to work ?
Was This Post Helpful? 2
  • +
  • -

#3 kianyt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 18

Re: Need Help with Higher-Lower game

Posted 03 February 2018 - 02:19 PM

i did a quick google search, and it appears that a "higher lower game" is usually the computer trying to guess your number, but this is the other way around, the player is trying to guess the number, and just to start out i made it a 1 time play, as the number is the same every time. what i'm trying to do is if you input a number less than 69 it tells you "too low" and if you input a number more than 69 it tells you "too high"
Was This Post Helpful? 0
  • +
  • -

#4 tony jay   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: Need Help with Higher-Lower game

Posted 03 February 2018 - 03:01 PM

I understand that you are a beginner so here are some suggestions:

Never use 'magic numbers in code' - that is the point of Thomas1965's question about the role of the number 69.
The fact that you were forced to explain it meant that your code does not.
Rather, create a variable and give it a name that explains its role in the code.'

While it is easy to see what your code is trying to do - it simply does not do it.
Why?
The reason is your while loop.
Again Thomas1965 has correctly, if enigmatically, told you the answer.
Hint: What is meant to be within that while loop and what is meant to be outside the loop? What can be easily done to make this explicit?
Go and and review the syntax of while loops and it all becomes obvious.

I ran your code with one very simple alteration...
Was This Post Helpful? 1
  • +
  • -

#5 kianyt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-February 18

Re: Need Help with Higher-Lower game

Posted 03 February 2018 - 03:18 PM

haha, i just realized the stupid mistake i made. However, I also improved my code a bit by removing the "magic number" and gave the variables better names. here is my current code:
#include <iostream>

using namespace std;

int main()
{
    int answerAttempt;
    int correctAnswer = 69;

    cout << "Choose a number between 1 and 100" << endl;
    while(answerAttempt!=correctAnswer){
       cin >> answerAttempt;

    if (answerAttempt<correctAnswer)
    {
        cout << "Too Low" << endl;
    }
    else if (answerAttempt>correctAnswer)
    {
        cout << "Too High" << endl;
    }
    else
    {
        cout << "Thats the RIGHT NUMBER!";
    }
    }
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 tony jay   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: Need Help with Higher-Lower game

Posted 03 February 2018 - 03:24 PM

Well done!

Extra credit: re-engineer this code to use switch logic instead of if...else statements. Does it make your code easier to follow and understand. No right or wrong answer here - just see what you think...
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1