4 Replies - 2522 Views - Last Post: 23 July 2007 - 07:34 AM Rate Topic: -----

#1 monkjessica  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-July 07

Number Guessing Game Program

Post icon  Posted 19 July 2007 - 01:19 PM

I am having problems making my program for a number guessing game run. I keep getting hung up on line 19 ( the( rand/time) line. I would appreciate any help debugging this and making it run properly. Thanks!!


//Assignment 3 Part II

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	int num;
	int guess;
	int diff;
	bool done;
	int noOfGuesses= 0;
   

[color=#FF0000]	num = (rand + time(0))% 100;[/color]
	done = false;
 

	while ((noOfGuesses < 5) && (!done))
	{
		cout << "Enter an integer greater than or equal to 0 and less than 100: ";
		cin >> guess;
		cout << endl;
		noOfGuesses++;

		if (guess == num)
		{
			cout << "Winner!. You guessed the correct number." << endl;
			done = true;
		}
		else
		{
			diff == abs(num - guess);
			if (guess < num);
			{
				if (diff >= 50)
					cout << "Your guess is very low." << endl;
				else if (30 <= diff && diff < 50)
					cout << "Your guess is low." << endl;
				else if (15 <= diff && diff < 30)
					cout << "Your guess is moderately low." << endl;
				else if (0 < diff && diff < 15)
					cout << "Your guess is somewhat low." << endl;
			   }			   
			   {
				   if (diff >= 50)
				   	   cout << "Your guess is very high." << endl;
				   else if (30 <= diff && diff < 50)
						cout << "Your guess is high." << endl;
				   else if (15 <= diff && diff < 30)
						cout << "Your guess is moderately high." << endl;
				   else if (0 < diff && diff < 15)
				   		cout << "Your guess is somewhat high." << endl;
			   }

			   if (noOfGuesses != 5)
				   cout << "Guess again!" << endl;
		}
	}//end while

	if (!done);
		cout << "You lose! The correct number is " << num << endl;

	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Number Guessing Game Program

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Number Guessing Game Program

Posted 19 July 2007 - 01:40 PM

hi, try num = (rand() + time(0))% 100;
Was This Post Helpful? 0
  • +
  • -

#3 zyruz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 13-August 05

Re: Number Guessing Game Program

Posted 19 July 2007 - 02:00 PM

you need to seed rand, with srand(time(0));
then just call rand() normaly..

so, instead of:
num = (rand + time(0))% 100;


do:
   srand(time(0));

	num = rand() % 100;


one other probleme I see is:
diff == abs(num - guess);

this wont give diff annything as, it only check if diff is the same as abs(num - guess).

so, remove one of the '=' and it will probarly be better :)

zy

This post has been edited by zyruz: 19 July 2007 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 crzyfro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-January 07

Re: Number Guessing Game Program

Posted 22 July 2007 - 06:02 PM

View Postzyruz, on 19 Jul, 2007 - 02:00 PM, said:

you need to seed rand, with srand(time(0));
then just call rand() normaly..

so, instead of:
num = (rand + time(0))% 100;


do:
   srand(time(0));

	num = rand() % 100;


one other probleme I see is:
diff == abs(num - guess);

this wont give diff annything as, it only check if diff is the same as abs(num - guess).

so, remove one of the '=' and it will probarly be better :)

zy



so what would the code look like all together?
Was This Post Helpful? 0
  • +
  • -

#5 zyruz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 13-August 05

Re: Number Guessing Game Program

Posted 23 July 2007 - 07:34 AM

it is 2-3 things to change, wont be a challange I bet...
but here it is:
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	int num;
	int guess;
	int diff;
	bool done;
	int noOfGuesses= 0;
   

  srand(time(0));   // seed the rand function

	num = rand() % 100; // call rand.
	cout << num <<'\n';
	
	done = false;


	while ((noOfGuesses < 5) && (!done))
	{
		cout << "Enter an integer greater than or equal to 0 and less than 100: ";
		cin >> guess;
		cout << endl;
		noOfGuesses++;

		if (guess == num)
		{
			cout << "Winner!. You guessed the correct number." << endl;
			done = true;
		}
		else
		{
			diff = abs(num - guess); // == will check if a statment is corect or not.
			if (guess < num) // dont use; after a if statment.
			{
				if (diff >= 50)
					cout << "Your guess is very low." << endl;
				else if (30 <= diff && diff < 50)
					cout << "Your guess is low." << endl;
				else if (15 <= diff && diff < 30)
					cout << "Your guess is moderately low." << endl;
				else if (0 < diff && diff < 15)
					cout << "Your guess is somewhat low." << endl;
			   }
			   else			
			   {
				   if (diff >= 50)
						  cout << "Your guess is very high." << endl;
				   else if (30 <= diff && diff < 50)
						cout << "Your guess is high." << endl;
				   else if (15 <= diff && diff < 30)
						cout << "Your guess is moderately high." << endl;
				   else if (0 < diff && diff < 15)
						   cout << "Your guess is somewhat high." << endl;
			   }

			   if (noOfGuesses != 5)
				   cout << "Guess again!" << endl;
		}
	}//end while

	if (!done);
		cout << "You lose! The correct number is " << num << endl;

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1