7 Replies - 3072 Views - Last Post: 30 September 2010 - 06:47 AM Rate Topic: -----

#1 xcloud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-September 10

Help w/ writing if statements in my guess game program

Posted 29 September 2010 - 06:31 PM

Hi! I'm having trouble w/ writing this guessing game program I am writing on c++, these the instructions:

# Let diff = the absolute value of (guess - theNumber). If diff is 0, then guess is correct and the program outputs a message indicating that the user guessed the correct number (and this part is unchanged from the original code). But if diff is not 0, then the program outputs the message as follows.

1. If diff is greater than or equal to 50, the program outputs the message indicating that the guess is either "very high" or "very low" depending on if the guess is higher/lower than theNumber.

2. If diff is greater than or equal to 30 (and less than 50), the program outputs the message indicating that the guess is either "high" or "low" depending on if the guess is higher/lower than theNumber.

3. If diff is greater than or equal to 15 (and less than 30), the program outputs the message indicating that the guess is either "moderately high" or "moderately low" depending on if the guess is higher/lower than theNumber.

4. If diff is greater than 0 (and less than 15), the program outputs the message indicating that the guess is either "somewhat high" or "somewhat low" depending on if the guess is higher/lower than theNumber.

# Give the user at most five tries to guess the number.

This is the neutral/ functioning code:


----------------------------------------------------
#include <iostream>
#include <cstdlib>  // to use a random number generator
#include <ctime>    // to get the current time
using namespace std;

int main()
{
  srand(time(0)); // seed randm number generator

  int theNumber = rand() % 100 + 1; // random number between 1 and 100
  int tries = 0; // variable for the number of tries
  int guess;     // variable for the user's guess
  bool done = false; // a bool variable which indicates the game is/isn't done

  // Welcome message first.
  cout << "***********  Welcome to Guess My Number **********\n\n";

  while (done == false)
  {
    cout << "Enter a guess (between 1 and 100): ";
    cin >> guess;

    if (guess == theNumber)
    {
      cout << "  Congratulations!! You guessed the correct number!\n";
      done = true;
    }
    else if (guess < theNumber)
    {
      cout << "==> Too low.  Guess again.\n";
    }
    else
    {
      cout << "==> Too high.  Guess again.\n";
    }
  }

  // Closing message
  cout << endl 
       << "****** THANK YOU. COME BACK AGAIN SOON.******\n";

  system("pause");
  return 0;
}

-----------------------------------------------------------------------
This is where I am stuck, if anyone can point me in the right directions, it would be greatly appreciated!

#include <iostream>
#include <cstdlib>  // to use a random number generator
#include <ctime>    // to get the current time
using namespace std;

int main()
{
  srand(time(0)); // seed randm number generator

  int theNumber = rand() % 100 + 1; // random number between 1 and 100
  int tries = 0; // variable for the number of tries
  int guess;     // variable for the user's guess
  bool done = false; // a bool variable which indicates the game is/isn't done

  // Welcome message first.
  cout << "***********  Welcome to Guess My Number **********\n\n";

  while (done == false)
  {
    cout << "Enter a guess (between 1 and 100): ";
    cin >> guess;

    if (guess == theNumber)
    {
      cout << "  Congratulations!! You guessed the correct number!\n";
      done = true;
    }
    else if (guess <= 50)
    {
      cout << "==> Very Low.  Guess again.\n";
    }
    else
    {
      cout << "==> Very High.  Guess again.\n";
    }
    else if (guess <= 30 > 50)
    {
      cout << "==> Low.  Guess again.\n";
    }
    else
    {
      cout << "==> High.  Guess again.\n";
    }
    else if (guess <= 15 > 30)
    {
      cout << "==> Moderately Low.  Guess again.\n";
    }
    else
    {
      cout << "==> Moderately High.  Guess again.\n";
    }
    else if (guess <= 0 > 15)
    {
      cout << "==> Somewhat Low.  Guess again.\n";
    }
    else
    {
      cout << "==> Somewhat High.  Guess again.\n";
    }
  }

  // Closing message
  cout << endl 
       << "****** THANK YOU. COME BACK AGAIN SOON.******\n";

  system("pause");
  return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 30 September 2010 - 03:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help w/ writing if statements in my guess game program

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Help w/ writing if statements in my guess game program

Posted 29 September 2010 - 06:56 PM

Welcome to DIC!

Please edit your posting (see the "EDIT" button in lower right).
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.



Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.
( e ) What have you already tried to fix it?

This post has been edited by janotte: 29 September 2010 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Help w/ writing if statements in my guess game program

Posted 29 September 2010 - 07:39 PM

Like janotte said you didn't say what was wrong.

Please post in code tags :code:

and a better alternative to system("pause"); is cin.get(); and that sometimes doesn't work if there already input in the buffer than use

cin.ignore();
cin.get();

Read this it explains why system("pause"); is bad http://www.gidnetwork.com/b-61.html
I've made a really nice guess a number game and could help you with this if you tell us what's wrong.

Hope this was helpful :-)

This post has been edited by Alex6788: 29 September 2010 - 07:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: Help w/ writing if statements in my guess game program

Posted 30 September 2010 - 04:14 AM

Your logic in general is flawed because you're testing against the guess, rather than the difference, but you definitely can't do this kind of thing:
else if (guess <= 30 > 50)


you have to use multiple boolean statements, like this:
else if (guess <= 30 || guess > 50)

Was This Post Helpful? 0
  • +
  • -

#5 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Help w/ writing if statements in my guess game program

Posted 30 September 2010 - 04:25 AM

Look at your else if chain:
 if (guess == theNumber)
    {
      cout << "  Congratulations!! You guessed the correct number!\n";
      done = true;
    }
    else if (guess <= 50)
    {
      cout << "==> Very Low.  Guess again.\n";
    }
    else
    {
      cout << "==> Very High.  Guess again.\n";
    }
--->else if (guess <= 30 ||guess> 50)
    {
      cout << "==> Low.  Guess again.\n";
    }


Here you are not following the else if chain properly.After an else you can't use else if,an else if followed by if statement.
An else if chain start it with a if statement and end with else statement with else if chain inbetween them,like this:
if(exp)
 statement
else if(exp)
 statement
else if(exp)
 statement
.
.
else
 statement


This post has been edited by aaa111: 30 September 2010 - 04:28 AM

Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1764
  • View blog
  • Posts: 3,419
  • Joined: 30-May 10

Re: Help w/ writing if statements in my guess game program

Posted 30 September 2010 - 04:40 AM

Consider some examples:
theNumber is 50
guess is 30, print low
guess is 10, print very low
guess is 50, print correct

Now,
theNumber is 90
guess is 50, print very low

You need to measure the distance between theNumber and guess.
Was This Post Helpful? 0
  • +
  • -

#7 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Help w/ writing if statements in my guess game program

Posted 30 September 2010 - 06:39 AM

would a switch statement not be better here?
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Help w/ writing if statements in my guess game program

Posted 30 September 2010 - 06:47 AM

View PostLarsonator, on 30 September 2010 - 10:39 PM, said:

would a switch statement not be better here?

No. Not really.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1