Hello
I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code:
and why does it break in this code(only after the first prompt):
The only difference between the two codes is in the scanf().
Thanks!
I'am a returning coder to C programming language and would appreciate if you could help me understand why does my while loop behave normally in the following code:
Code:
int loop = 1;
char userIN;
while(loop)
{
printf("\nIf you want to retrieve tuples prompt y/n: \t");
//items_read = scanf("%s",&userIN);
scanf("%s",&userIN);
if(userIN=='n'||userIN=='N')
{
loop = 0;
printf("\n user prompt: %c \n",userIN);
break;
}
else if((userIN=='y')||(userIN=='Y'))
{
loop = 1;
printf("\n user prompt: %c \n",userIN);
}
else
{
printf("Invalid Response, exiting:\t");
printf("\n user prompt: %c \n",userIN);
break;
}
}
and why does it break in this code(only after the first prompt):
Code:
int loop = 1;
char userIN;
while(loop)
{
printf("\nIf you want to retrieve tuples prompt y/n: \t");
//items_read = scanf("%s",&userIN);
scanf("%c",&userIN);
if(userIN=='n'||userIN=='N')
{
loop = 0;
printf("\n user prompt: %c \n",userIN);
break;
}
else if((userIN=='y')||(userIN=='Y'))
{
loop = 1;
printf("\n user prompt: %c \n",userIN);
}
else
{
printf("Invalid Response, exiting:\t");
printf("\n user prompt: %c \n",userIN);
break;
}
}
Thanks!
Comment