Quick guesseing game

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{


int iSecret, iGuess;

srand (time(NULL));


iSecret = rand() % 100;

do {
printf ("Guess the number (1 to 100): ");
scanf ("%d",&iGuess);
if (iSecret<iGuess) puts ("The secret number is lower");
else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);

puts ("Mice job you guessed the number");
return 0;
}
Is there a question here?

FYI, iSecret will be a number from 0 to 99 inclusive, not from 1 to 100 inclusive as you seem to imply.
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
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(static_cast<unsigned int>(time(NULL)));
int randomnum = rand() %100 + 1; //generates number from 1 to 100.
int choice,tries = 0;
cout<<"Welcome to guess my number.\n";
do
{
cout<<"Enter a number (1 - 100): " <<endl;
cin>>choice;
++tries;
if ( choice > randomnum)
{
cout<<"Too high!\n";
}
if ( choice < randomnum)
{
cout<<"Too low!\n";
}
else
{
cout<<"That's it! you've guessed it in just " <<tries<< " tries!\n";
cout<<"The secret number was " <<randomnum<< " Well done!\n";
}
}while ( tries != 5); //Player loses after 5 incorrect guesses.
return 0;
}


here's a better version. :)
Topic archived. No new replies allowed.