0

OK, here is my c++ program that i made with "Visual C++ 2005 Express Edition"....

#include<cstdlib>
#include<ctime>
#include<iostream>
using namespace std;
int main()
{
    cout << "Welcome To Danny's Game!.\n";
    cout << "See if you can beat me.\n";
    cout << "The First number below is your number.\n";
    srand((unsigned)time(0));
    int random_integerplayer;
    for(int index=0; index<1; index++)
    {
        random_integerplayer = (rand()%10)+1;
        cout << random_integerplayer << endl;
        cout << "\n";
        cout << "This is my number.\n";
        srand((unsigned)time(0));
        int random_integercomputer;
        for(int index=0; index<1; index++)
        {
            random_integercomputer = (rand()%10)+1;
            cout << random_integercomputer << endl;
            cout << "\n";
            if( random_integercomputer>=random_integerplayer )
                cout << "You Lose. ";
            else
                cout << "You Win! ";
        }
    }
}

OK, that was my code, but something won't work.
Whenever i use it, it always comes up with 2(two)
numbers(computer and player), that are both exactully the same!!!
Please help me make it so that when it generates the random numbers, they are both different...
thxz alot! =)

3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by ~s.o.s~
0

It is because you are reseeding the random number generator and in the attempt restarting it. You need to seed the random number generator only once for a single run. Remove the second instance of srand( ( unsigned ) time( 0 ) ) and you should be fine.

And btw, what's up with the redundant loops ? Maybe you wanted to make the program loop infinitely and to exit based on the choice entered by the user ? You can try something like:

int choice = 1 ;
while( choice )
{
    // your code here

   cout << "Do you want to continue ? ( 0 to exit ) " ;
   cin >> choice ;
}

And yeah, use proper code tags, don't color your code, the highlighting is automatically taken care by the code tags. Just paste your code within code tags.

Edited by happygeek: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.