how to stop random number guessing

Page 1 of 1

5 Replies - 1699 Views - Last Post: 06 February 2009 - 10:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=84961&amp;s=d398e7422868c3bda62c186419c8c024&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 newjohnwu

Reputation: 1
• Posts: 27
• Joined: 16-October 08

how to stop random number guessing

Posted 05 February 2009 - 07:57 PM

so far my partner has try the guessing number game, and this is that he got so far, but not sure how to stop the user after 10 tries. I'm not sure how to go about this, as i've looked for it everywhere. at lest a basic outline would be nice

```#include<iostream>
#include<string>
#include <ctime>
#include <cstdlib>

using namespace std;
int main()
{
srand(time(0));
int randomnumber = rand() % 100 + 1;
int guess = 0;
int life =0;
cout <<"enter a number"<<endl;

do

{
cin>> guess;

if (guess<= 0)

return -1;

else if (guess < randomnumber)

cout<<"guess too low try again"<<endl;
else if (guess > randomnumber)
cout<<"guess too high try again"<<endl;
}
while (guess != randomnumber);

cout<<" you have guess the number"<<endl;

return 0;
}

```

Is This A Good Question/Topic? 0

Replies To: how to stop random number guessing

#2 bodom658

• Villiage Idiom

Reputation: 114
• Posts: 1,123
• Joined: 22-February 08

Re: how to stop random number guessing

Posted 05 February 2009 - 08:03 PM

well, how would you normally stop a while loop after 10 iterations?

you have some kind of counter, and another statement in the while. Remember, you can use || (or) and && (and) in any boolean statement (while, if, etc)

let's see if you can figure it out!

~Bodom

#3 no2pencil

• Professor Snuggly Pants

Reputation: 6642
• Posts: 30,931
• Joined: 10-May 07

Re: how to stop random number guessing

Posted 05 February 2009 - 08:03 PM

Rather than test if they got the number, for your while loop, I would run a while loop on the amount of times they try to guess.

```#include<iostream>
#include<string>
#include <ctime>
#include <cstdlib>

using namespace std;
int main(void) {
srand(time(0));
int randomnumber = rand() % 100 + 1;
int guess = 0;
int times_guessed=0, won=0;
int life =0;
cout <<"enter a number"<<endl;

while(times_guessed<10) {
cin>> guess;

if (guess<= 0) {
return -1;
}
else {
if (guess < randomnumber) {
cout<<"guess too low try again"<<endl;
}
else {
if (guess > randomnumber) {
cout<<"guess too high try again"<<endl;
}
}
}
times_guessed++;
if(guess == randomnumber) {
times_guessed=10;
won=1;
}
}

if(won==1) {
cout<<" you have guess the number"<<endl;
}
else {
cout<<" you went over 10 attempts."<<endl;
}
return 0;
}

```

This code is untested, although it's pretty straight forward.

#4 Plus

• D.I.C Regular

Reputation: 41
• Posts: 414
• Joined: 24-November 08

Re: how to stop random number guessing

Posted 05 February 2009 - 10:11 PM

:: looks like you're not well at all,

```if (guess<= 0)

return -1;

```

:: invalid Error-Code which should be positive,

#5 no2pencil

• Professor Snuggly Pants

Reputation: 6642
• Posts: 30,931
• Joined: 10-May 07

Re: how to stop random number guessing

Posted 05 February 2009 - 10:32 PM

Plus, on 5 Feb, 2009 - 11:11 PM, said:

:: looks like you're not well at all,

```if (guess<= 0)

return -1;

```

:: invalid Error-Code which should be positive,

I'm actually wondering why anyone would guess less than zero. But I suppose you do always want to code for the worse.

Although, can anyone actually input a negative number? Would cin read the dash as a numeric or char value?

#6 Plus

• D.I.C Regular

Reputation: 41
• Posts: 414
• Joined: 24-November 08

Re: how to stop random number guessing

Posted 06 February 2009 - 10:46 AM

no2pencil, on 5 Feb, 2009 - 09:32 PM, said:

Plus, on 5 Feb, 2009 - 11:11 PM, said:

:: looks like you're not well at all,

```if (guess<= 0)

return -1;

```

:: invalid Error-Code which should be positive,

I'm actually wondering why anyone would guess less than zero. But I suppose you do always want to code for the worse.

Although, can anyone actually input a negative number? Would cin read the dash as a numeric or char value?

:: you two are not well .. the problem is NOT about "will the user input negative number",
::
:: we are in the MAIN FUNCTION we MUST return NUMBER OF ERRORS ( 0 = NO ERRORS ) ...
::
:: what does in hell ( -1 errors ) mean !