#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
usingnamespace std;
int main ()
{
int guess;
char answer;
srand (time (0));
for (int i = 0; i < 1000; i++)
{
int random = rand( );
int num = ((random % 1000) + 1);
cout << "Guess a number between 1 and 1,000: ";
cin >> guess;
if (guess > num)
{
cout << "The number is lower. Guess again:";
cin >> guess;
}
elseif (guess < num)
{
cout << "The number is higher. Guess again:";
cin >> guess;
}
if (guess == num)
{
cout << "You've guessed correctly!\n\n";
break;
}
}
return 0;
}
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
usingnamespace std;
int main ()
{
int guess;
char answer;
srand (time (0));
// moved these lines out of 1000 loop.
// we want to pick random number once, not 1000 times.
int random = rand( );
int num = ((random % 1000) + 1);
cout << "Guess a number between 1 and 1,000: ";
for (int i = 0; i < 1000; i++)
{
// stuff in here will be done 1000 times.
// moved these lines up there.
//int random = rand( );
//int num = ((random % 1000) + 1);
//cout << "Guess a number between 1 and 1,000: ";
// one and only input guess
cin >> guess;
if (guess > num)
{
cout << "The number is lower. Guess again:";
//cin >> guess;
// we will get new guess when loop goes back up
}
elseif (guess < num)
{
cout << "The number is higher. Guess again:";
//cin >> guess;
// we will get new guess when loop goes back up
}
else
//if (guess == num)
// changed this to else.
// if guess isn't greater or less than num,
// then it must be equal to num.
{
cout << "You've guessed correctly!\n\n";
break;
}
}
return 0;
}