# Thread: Invalid Output in certain Integers

1. ## Invalid Output in certain Integers

Hi!

For this program the computer will be guessing the number that the user have chosen, from 1 to 10.
It will need to keep track of the number of guesses that the computer makes before it guesses correctly.

This is what I have done by now:

Code:
```int main() {
int num, count;
char x, y;
do{
count = 0;
printf("Please pick a number between 1 and 10...\n");
scanf("%s", &x);

while(x == 'y' || x == 'Y')
{   num = 1;
printf("Is your number %d? ", num);
scanf("%s", &x);
num++;
count++;
while(x == 'n' || x == 'N')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
count++;
num++;
}
printf("You chose %d and it took me %d attempts.\n", num, count);
printf("\n");
printf("Shall we play again? ");
scanf("%s", &y);
printf("\n");
}
}while(y == 'y' || y == 'Y');```
The output should look like this:

Code:
```Please pick a number between 1 and 10...
You chose 5 and it took me 3 attempts.

Shall we play again? Y

Please pick a number between 1 and 10...
You chose 3 and it took me 1 attempt.

Shall we play again? N```
But in this case I got an invalid output in the integer that displays the number since it doesn't increase the integer, here is the example:

Code:
```Please pick a number between 1 and 10...
You chose 1 and it took me 3 attempts.

Shall we play again? Y

Please pick a number between 1 and 10...
You chose 1 and it took me 1 attempts.

Shall we play again? N```
As you can see, the number doesn't increase...

Also it will be very appreciated if someone can gave me some advises of how to make random or not sequential numbers.
i.e. Instead of going 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, etc. The output be 3, 8, 5, 6, 2, 4, 1, 7, 10, 9.

Thanks for your help and happy Programming!

2. Don't reset num to 1 at the start of the loop, if you don't want it to always be one. If you want random numbers, use the pseudo-random number generator rand().

3. Originally Posted by tabstop
Don't reset num to 1 at the start of the loop, if you don't want it to always be one. If you want random numbers, use the pseudo-random number generator rand().
Where should I put the integer? Since if I remove it from where it is I get the next output:

Code:
```Please pick a number between 1 and 10...
You chose 1 and it took me 4 attempts.

Shall we play again? N```

5. Originally Posted by tabstop
OK, I tried 3 different ways outside the loop without success, can you specify exactly where between which lines should I declare the integer.

Thank you.

6. The line that currently says:
Code:

right before the loop.

7. Originally Posted by tabstop
The line that currently says:
Code:

right before the loop.
Okay, well if you try to run the code in an editor, probably you would find that is not that easy.

8. Originally Posted by georgio777
Okay, well if you try to run the code in an editor, probably you would find that is not that easy.
Look at your first post. There is only one blank line in your code. How hard can it be to find the only blank line?

9. Originally Posted by tabstop
Look at your first post. There is only one blank line in your code. How hard can it be to find the only blank line?
Okay, I tried that, still getting ones

What is wrong? I don't get it

10. Compare and contrast: If you make the change as mentioned, you should have this:
Code:
```int main() {
int num, count;
char x, y;
do{
count = 0;
printf("Please pick a number between 1 and 10...\n");
scanf("%s", &x);
num = 1;
while(x == 'y' || x == 'Y')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
num++;
count++;
while(x == 'n' || x == 'N')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
count++;
num++;
}
printf("You chose %d and it took me %d attempts.\n", num, count);
printf("\n");
printf("Shall we play again? ");
scanf("%s", &y);
printf("\n");
}
}while(y == 'y' || y == 'Y');```
Now there are other issues here, specifically the fact that using %s with the address of a character will overwrite one byte of another variable with the value 0. Presumably you want to use
Code:
`scanf(" %c", &y);`
instead (and the same with x).

11. Originally Posted by tabstop
Compare and contrast: If you make the change as mentioned, you should have this:
Code:
```int main() {
int num, count;
char x, y;
do{
count = 0;
printf("Please pick a number between 1 and 10...\n");
scanf("%s", &x);
num = 1;
while(x == 'y' || x == 'Y')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
num++;
count++;
while(x == 'n' || x == 'N')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
count++;
num++;
}
printf("You chose %d and it took me %d attempts.\n", num, count);
printf("\n");
printf("Shall we play again? ");
scanf("%s", &y);
printf("\n");
}
}while(y == 'y' || y == 'Y');```
Now there are other issues here, specifically the fact that using %s with the address of a character will overwrite one byte of another variable with the value 0. Presumably you want to use
Code:
`scanf(" %c", &y);`
instead (and the same with x).
I tried that and I got something unexpected, the program terminates by itself after the input is declared. Are you sure about this?

12. Code:
```int main() {
int num, count;
char x, y;
do{
count = 0;
printf("Please pick a number between 1 and 10...\n");
scanf("%s", &x);

while(x == 'y' || x == 'Y')
{   num = 1;
printf("Is your number %d? ", num);
scanf("%s", &x);
num++;
count++;
while(x == 'n' || x == 'N')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
count++;
num++;
}
printf("You chose %d and it took me %d attempts.\n", num, count);
printf("\n");
printf("Shall we play again? ");
scanf("%s", &y);
printf("\n");
}
}while(y == 'y' || y == 'Y');```
so,

Code:
```	scanf("%s", &x);

while(x == 'y' || x == 'Y')
{   num = 1;
printf("Is your number %d? ", num);
scanf("%s", &x);```
put that num =1 here and try

Code:
```	scanf("%s", &x);
num = 1;

while(x == 'y' || x == 'Y')
{
printf("Is your number %d? ", num);
scanf("%s", &x);```

13. Originally Posted by georgio777
I tried that and I got something unexpected, the program terminates by itself after the input is declared. Are you sure about this?
Yes, because the program does not terminate by itself after the input is declared. Compare what you have with what is posted, carefully.

14. Originally Posted by tabstop
Yes, because the program does not terminate by itself after the input is declared. Compare what you have with what is posted, carefully.
With %c I got more faulty behaviors, it seems to skip the scanf function.

15. dont use the %c......use %s.....that seems to work..

Code:
```int main()
{
int num=1, count=0;
char x, y;
do{
printf("Please pick a number between 1 and 10...\n");
scanf("%s", &x);

while(x == 'y' || x == 'Y')
{
printf("Is your number %d? ", num);
scanf("%s", &x);

while(x == 'n' || x == 'N')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
num++;
count++
}
}
printf("You chose %d and it took me %d attempts.\n", num-1, count); // the while loop exits
printf("\n");
printf("Shall we play again? ");
scanf("%s", &y);
printf("\n");
}
}while(y == 'y' || y == 'Y');

}```
its still an unfinished code......still you need to fix the count