Help w/ writing if statements in my guess game program

Page 1 of 1

7 Replies - 3256 Views - Last Post: 30 September 2010 - 06:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192732&amp;s=affa9c0e91ed77510de417a895f2fe1e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 xcloud

Reputation: 0
• 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!!!

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

• code > sword

Reputation: 990
• 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!

( 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
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.

( 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

#3 Alex6788

Reputation: 144
• 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.

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();

I've made a really nice guess a number game and could help you with this if you tell us what's wrong.

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

• Saucy!

Reputation: 6208
• Posts: 23,953
• 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)
```

#5 aaa111

• D.I.C Regular

Reputation: 88
• 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

#6 Salem_c

• void main'ers are DOOMED

Reputation: 1934
• Posts: 3,811
• 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.

#7 Larsonator

Reputation: 6
• 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?

#8 janotte

• code > sword

Reputation: 990
• 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

Larsonator, on 30 September 2010 - 10:39 PM, said:

would a switch statement not be better here?

No. Not really.