# Thread: how do i program a guessing game?

1. ## how do i program a guessing game?

Hye guys am new to programming. i rely need to noe how do i code the following requirements:

• Each digit of the number cannot be repeated
• If the number entered by the user has repeating digits, the user has to be asked to re-enter a new number
• Each attempt of guessing need to be numbered
• The user will be told how many digits correctly positioned and how many digits wrongly positioned after each guess
• The user will be congratulated if the user successfully guessed the number.
• The user will be asked whether to replay or not at the end of the game

Plz help me

2. Firstly, you need to know the basics of the language. Return with your attempt and we will resume...

3. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
int    guess;
int play_again;
int r_num;
srand (time (NULL));
r_num = (999-100)+1;

play_again=0;

do
{
printf("Hey am thinkin of a number!! Wanna guess?? ;) \n");
scanf("%d\n", &guess);

guess=0;
do
{
if (guess != r_num)
{
}

else
{
}

guess++;
}
while (r_num != guess);

printf("The correct answer is %d\n", rand () % r_num + 100);
printf("Do you wish to play again?? (Y/N)");
scanf("%c", &play_again);

play_again++;
}
while (play_again== 'y'||play_again=='Y');

return 0;
}

this is my attempt... :)```

4. Ok, you can do a much better job on indentation but putting that aside, your code is almost complete. Just remember that in order to get a random number in the [LOW, HIGH] range, you do:
Code:
`r_num = rand() % (HIGH - LOW + 1) + LOW;`

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
int    guess;					// init to 0
int play_again;					// set this as char !!!!!!
int r_num;
srand (time (NULL));
r_num = (999-100)+1;			// 100 is probably too big, your user would get very bored. - try 10!

play_again=0;				//this should be a char and initialised to 'n' or some meaningful value

do
{
printf("Hey am thinkin of a number!! Wanna guess?? ;) \n");
scanf("%d\n", &guess);

guess=0;							//pointless, you have just asked the user for a value and then scrubbed it out with a 0 immediately
do
{
if (guess != r_num)
{
}
else
{
printf("Correct answer!!\n");		// move this outside this loop, else statement not neccesary - that would be my preference
}

guess++;					// why this ??? - has no meaning in the context, is not required, plus it is supposed to hold your user input.
}
while (r_num != guess);

printf("The correct answer is %d\n", rand () % r_num + 100);			//why dont you output guess here? since the user got it right?
printf("Do you wish to play again?? (Y/N)");
scanf("%c", &play_again);						//here you are asking the user for a value, then immediately making it 0 below ??????

play_again++;									//this makes no sense either - it is supposed to be your user inut char and should not be touched
}													// until the condition in while loop can evaluate it
while (play_again== 'y'||play_again=='Y');

return 0;
}```

6. Know the difference between C and C++. This belongs in the C section.

7. Originally Posted by rogster001
// 100 is probably too big, your user would get very bored. - try 10!
Try binary search the next time someone plays this game with you. It doesn't matter how big the search space is.

8. haha i swear it was 100 first!

9. Code:
` thank u so much guys!! i will work more on it come bk wit a btr coding :)`

10. Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
int play_again;
int	guess;
int result[3];
int i;
int r_num;
srand (time (NULL));
r_num = 9 + 1;

play_again=0;
for (i = 0; i < 3; i++)
{
result[i] = rand( ) % 3 + 1;

}
do
{
guess=0;
do
{
printf("Hey am thinkin of a number!! Wanna guess?? ;) \n");
scanf("%d", &guess);

if (guess != r_num)
{
}

else
{
}

guess++;
}
while (r_num != guess);

printf("The correct answer is %d\n", rand () % r_num);
printf("Do you wish to play again?? (Y/N)");
scanf("%c", &play_again);
}
while (play_again== 'y'||play_again=='Y');

return 0;
}

i manage to come up with this... wat i dun understand now is, how i make the program tell the user on how many digits correctly positioned and how many digits wrongly positioned after each guess... help?? any guidance on this? thank u... :)```

11. guys anyone can help me with this??