5 Replies - 1212 Views - Last Post: 06 February 2009 - 10:46 AM Rate Topic: -----

#1 newjohnwu  Icon User is offline

  • New D.I.C Head

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

how to stop random number guessing

Post icon  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

	{
		cout<<" enter your # 1-100"<<endl;
		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  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5068
  • View blog
  • Posts: 26,450
  • 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) {
        cout<<" enter your # 1-100"<<endl;
        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.
Was This Post Helpful? 0
  • +
  • -

#4 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • 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,
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Head MFIC
  • member icon

Reputation: 5068
  • View blog
  • Posts: 26,450
  • Joined: 10-May 07

Re: how to stop random number guessing

Posted 05 February 2009 - 10:32 PM

View PostPlus, 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?
Was This Post Helpful? 0
  • +
  • -

#6 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: how to stop random number guessing

Posted 06 February 2009 - 10:46 AM

View Postno2pencil, on 5 Feb, 2009 - 09:32 PM, said:

View PostPlus, 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 !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1