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;
}

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!

n = random();

If your using this, make sure to call randomize(); before it.

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.