Hi you can customize it the problem is that when i put 5 it say to low then when i put 6 it say to high or any numbers pls help me..

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
 using namespace std;
 
 char ans;
 
 int main()
{
 do
{
     	srand(time(0));          
	    int randomNumber = rand() % 10+1;
	    int guess = 0; 
{
        system ("cls");
        cout << "\tThe Number Guessing Game\n\n";
		cout << "Enter your guess (#1-10): ";
		cin >> guess;
{ 
		if (guess < randomNumber)
			cout << "Your guess was too low\n\n"; 
 
		if (guess > randomNumber) 
		cout << "Your guess was too high\n\n";
		
		if (guess != randomNumber)
		cout << "Try Again\n";
		
		if (guess == randomNumber)
	    cout << "\nCongratulations! You've guessed the number.\n\n";
}
}
 cout <<"You want to try again??";
 cin >> ans;
} 
while ((ans=='y')||(ans=='Y'));
cout <<"Thank You!!";
getch();
}

Recommended Answers

All 8 Replies

Hiya,

The issue as i have identified it was that you generated a new random number for each guess (thats a pretty hard game if u ask me)

I have added a bool and an if around the generating number section of the code to prevent this. see my code below

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>

using namespace std;

char ans;

int main()
{
    bool guessed;
    guessed = false;

    do
    {
        if(guessed) //if they guessed the number need a new one
        {
            srand(time(0));
            int randomNumber = rand() % 10+1;
            guessed = false; //reset flag as new number now
        }

	    int guess = 0;

        system ("cls");
        cout << "\tThe Number Guessing Game\n\n";
        cout << "Enter your guess (#1-10): ";
        cin >> guess;

        if (guess < randomNumber)
            cout << "Your guess was too low\n\n";

        if (guess > randomNumber)
            cout << "Your guess was too high\n\n";

        if (guess != randomNumber)
			cout << "Try Again\n";

        if (guess == randomNumber)
        {
            cout << "\nCongratulations! You've guessed the number.\n\n";
            guessed = true;
        }



	cout <<"You want to try again??";
	cin >> ans;

    } while ((ans=='y')||(ans=='Y'));

    cout <<"Thank You!!";
    getch();
}

This makes your game work now and i hope you can see what was happening now too

Good luck in the future :)

edit: Also consider moving srand() to the start of main as you only really need to seed the randomiser once.

commented: Nice answer! +4
commented: I disagree, it will not compile on standard compilers and has poor formatting -3

the randomNumber is undeclared it says it don't run

What he meant to say was,

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>

using namespace std;


int main()
{
	char ans;
	srand(time(0));
	int randomNumber = rand() % 10+1;
	bool guessed = false;

    do
    {
		guessed = false;
		system ("cls");
		cout << "\tThe Number Guessing Game\n\n";
                randomNumber = rand() % 10+1;

		do
		{
			int guess = 0;
			
			cout << "Enter your guess (#1-10): ";
			cin >> guess;

			if (guess < randomNumber)
				cout << "Your guess was too low\n\n";

			if (guess > randomNumber)
				cout << "Your guess was too high\n\n";

			if (guess != randomNumber)
				cout << "Try Again\n";

			if (guess == randomNumber)
			{
				cout << "\nCongratulations! You've guessed the number.\n\n";
				guessed = true;
			}
		}while (!guessed);



	cout <<"You want to try again??";
	cin >> ans;

    } while ((ans=='y')||(ans=='Y'));

    cout <<"Thank You!!";
    getch();

  return 0;
}

You don't want to seed srand() more than once during your program - just a bad habit to get into. You only need to seed it once.

the randomNumber is undeclared it says it don't run

It doesn't work because of this:

if(guessed) //if they guessed the number need a new one
        {
            srand(time(0));
            int randomNumber = rand() % 10+1;
            guessed = false; //reset flag as new number now
        }

randomNumber is declared inside the statement block that executes only if the if statement is true. As a result, it also dies (goes out of scope) at the end of the same statement block.

To prevent this pre-mature destruction, Lines 3 and 4 (of this snippet) should be near the beginning of main() before any other statement block begins.

@duki and fbody, thanks for pointing that out, i overlooked it myself. i guess this is what happens when you modify someone elses code without looking at the changes very carefully.

Agreed with thier posts moving the decleration of the var to the start of the program then just seting them in the if would solve the problem.

Aplogies for that mistake.

srand(time(NULL));

it can also be used

The problem i can see is that the if statements are not maintained in braces eg.

if(guess==randomnumber)
{
   all the statements....
}

The problem i can see is that the if statements are not maintained in braces eg.

if(guess==randomnumber)
{
   all the statements....
}

end quote.

In this case, that's not really a big deal. If you write your conditions properly, which the OP has, that structure is perfectly valid.
An if statement can be written 2 different ways

if (condition)
  statement1
statement2
...
statementN

or

if (condition) {
  statement1
  statement2
  ...
  statementN
}

In the first version, only statement1 executes. In the second version, statement1 through statementN all execute.

Using an if-else-if ladder would probably have been better, but the the way it's written currently does work. For this particular issue, the problem is variable scoping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.