-
OK, I fixed by creating a junk variable which is the one that %s will modify. With this change it will not affect the results.
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand ( time(NULL) );
int z, num, count;
char x, y;
do{
z = 0;
count = 0;
printf("Please pick a number between 1 and 10...\n");
printf("Ready to Play? ");
scanf("%s", &x);
num = rand() % 10 + 1;
while(x == 'y' || x == 'Y')
{
printf("Is your number %d? ", num);
scanf("%s", &x);
count++;
while(x == 'n' || x == 'N')
{ num = rand() % 10 + 1;
printf("Is your number %d? ", num);
scanf("%s", &x);
count++;
}
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');
return 0;
}
Anyway Thanks to Obelisk and tabstop!
-
Wow after some research I found that it was a lot easier to stop using scanf in while/for loops with %c or %s for characters, but instead use two getchar() Since in this case it doesn't overwrite any integer as in %s or establishing two characters as in %c (Because of the ENTER).
i.e.
Instead of using
Code:
printf("Shall we play again? ");
scanf("%s"), &answer;
USE
Code:
printf("Shall we play again? ");
answer = getchar();
getchar();
So the second character is only stored but not declared.