I wrote this simple program (Number Guessing Name - Pastebin.com) where the computer try to guess which is the number insered by the user.
When the program ask to the user "The secret number is Higher or Lower than my guess %i?\n" the question is repeted two times on the screen. Someone can help me?
Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int num;
int guess;
int height=100;
int bottom=1;
char answ='a';
printf("Instruction: You have to insert a number from 1 to 100 and I have to guess it.\n"
"But you have to help me saying me if my guess is higher typing 'h'\n"
"or 'l' otherwise.\n\n");
printf("Please insert a number:\n");
num *scanf("%d", &num);
srand(time(NULL)); // initialize random number generator
guess = rand()%100 +1;
while(guess != num)
{
printf("The secret number is Higher or Lower than my guess %i?\n",guess);
answ=getchar();
if (answ=='l')
{
height=guess;
srand(time(NULL)); // initialize random number generator
guess = rand()%(height-1-bottom) +(bottom+1);
}
if (answ=='h')
{
bottom=guess;
srand(time(NULL)); // initialize random number generator
guess = rand()%(bottom-height-1) +(bottom+1);
}
}
printf("I found it! It's %i",guess);
return 0;
}