Code:
if (random > guess)
cout << "Guess was too big\n";
else if (random < guess)
cout << "Guess was too small\n";
else if (random == guess)
cout << "You got it!\n";
You need to put 'guess' first.
As in:
Code:
if (guess > random)
cout << "Guess was too big\n";
else if (guess < random)
cout << "Guess was too small\n";
else if (guess == random)
cout << "You got it!\n";
And your random number is going to be between 1 and 101, not 1 and 100.
A formula for random numbers is: rand() % (max - min) + min
So that would be: rand() % (100 - 1) + 1
which could be simplified to rand() % 99 + 1
You could use a while loop to make the program continue to ask for your guess.
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int guess;
double random;
int i;
srand(time(0)); //Don't use NULL?
cout << " I'm guessing of a number between 1 and 100 can you guess it?\n";
random = rand() % 99 + 1;
while (true) //Loop infinitely (or until a 'break' command is called)
{
cin >> guess; //Ask for input
if (guess > random)
cout << "Guess was too big\n";
else if (guess < random)
cout << "Guess was too small\n";
else
{
cout << "You got it!\n";
break; //Break out of the while loop!
}
} //End bracket for while loop
system("PAUSE");
return 0;
}