Hey
I've only been coding for a couple of days
and I got this code im working on
for some reason the computer only guesses the number 42 instead of a random number
can someone help me?

Heres the code:

/*========================================================================\
|This is basically the guess my number game                               |
|But the roles are reversed, well I'm going to try it anyway              |
|-------------------------------------------------------------------------|
|31/08/2010 - 9:00 started                                                |
|31/08/2010 - 9:51 finished                                               |
|Its slightly flawed as in the random generator only sticks to one number |
|once I learn another method I will update this script                    |
|-------------------------------------------------------------------------|
|By Chris Forgeard                                                        |
|Copyrighted 31/08/2010                                                   |
\========================================================================*/

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    
//first ill set up the random number generator etc.
        int number = rand() % 100 + 1;
        int tries = 0;
        char right = 'n';
        
        cout << "Welcome HP MINI, guess my number!" << endl;

//The game loop
         while (right == 'n')
        {
             cout << "Is it " << number << "?" << endl;
             cout << "(y/n)" << endl;
             cin >> right;
             
             if (right == 'y')
             cout << "Yes HP MINI, that is right!" << endl;
             
             if (right == 'n')
             cout << "Sorry HP MINI, that is wrong!";     
             ++tries;
        }

//The end scene         
        cout << "Congratulations HP MINI!" << endl;
        cout << "You did it in " << tries << " tries!";
        cin >> right;
        
        return 0;
}

Recommended Answers

All 2 Replies

I think that

SYSTEM ("set <variable>=%random%")

should be appropriate... But you'll need to include a file... Stdlib I think.

commented: This makes no sense at all. -3

rand is always seeded to 1 by default. You can change that by calling srand. May I suggest this article?

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.