0

OK I am new to C++ so I decided I'd toy around with what I know and try and do some neat stuff. I'm trying to make a program that will try and make you guess the number it's thinking of. But every time I run the program it always thinks of 41. I don't know the rand() function I'm trying to use so if you could explain it to me that would be nice. Here is the code:

#include <iostream>
using namespace std;
int main(){
	int q;
	int n;
	//make the number the computer makes limitations. <=10 and >= 1.
	n <=10;
	n >=1;
	n = random();
	//prompt for q, the number the user guessed.
cout << "Please try and guess my random number!";
	cin >> q;
if (q==n) 
	cout << "You have guessed correctly! what are the odds!";
else {
	cout << "I'm sorry the correct number was: "<< n;
}
system ("pause");	
return 0;
}

Edited by FudgeCoder: Code

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by nbaztec
0

Hello FudgeCoder, try this:

edit: (dont forget the <ctime> header)

srand(time(0)); //this will ensure a random number every time
n = (rand() %10) + 1; //this will randomize numbers from 1 through 10.

you can change the numerical value (in the code which is 10) to any number to increase the max value.

hope this helps!

Edited by strwbry.coder: inputting more information

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.