random number generator

My random number generator game.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{
	while (true)
	{
		int iSecret, iGuess;
		srand(time(NULL));
		iSecret = rand() % 10 + 1;
		cout << "Guess a number between 1 and 10" << endl;
		cin >> iGuess;
		if (iSecret == iGuess)
		{
			cout << "CORRECT" << endl;
		}
		else
		{
			cout << "INCORRECT" << endl;
		}
	}
}
Shouldnt it be something like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;
#include <cstdlib>
#include <ctime>
int main()
{
    int iSecret, iGuess;
    srand(time(0));
    iSecret = rand() % 10 + 1;
    int tries=5;
	while (tries)
	{
		cout << "Guess a number between 1 and 10. You have "<<tries-- <<" tries"<< endl;
		cin >> iGuess;
		if (iSecret == iGuess)
		{
			cout << "CORRECT" << endl;
			break;
		}
		else
		{
			cout << "INCORRECT" << endl;
		}
	}
}
Where is the question Also you probably want to seed outside the loop.
Last edited on
Topic archived. No new replies allowed.