#include <stdio.h>
int main ()
{
char loop='y';
while(loop != 'n') {
printf("loop? ");
scanf("%c", &loop);
if(loop != 'y') {
loop='n';
}
}
return 0;
}
If I type in 'y' he restart the while-loop but ignores the scanf the second time and end the loop after that. Can anyone help?
Make sure the scanf discards the newline. Change it to:
scanf(" %c", &loop);
^
You probably had to enter a newline so the input goes to your program, right? The second time your loop executes it reads that newline character, which was "waiting" to be read and automatically exits the loop ('\n' != 'y'). You can make scanf ignore whitespace by using this format string instead:
" %c"
One solution can be the use fflush(stdin) after the scanf() statement to clear the input buffer.
Related
#include <stdio.h>
int main ()
{
char loop='y';
while(loop != 'n') {
printf("loop? ");
scanf("%c", &loop);
if(loop != 'y') {
loop='n';
}
}
return 0;
}
If I type in 'y' he restart the while-loop but ignores the scanf the second time and end the loop after that. Can anyone help?
Make sure the scanf discards the newline. Change it to:
scanf(" %c", &loop);
^
You probably had to enter a newline so the input goes to your program, right? The second time your loop executes it reads that newline character, which was "waiting" to be read and automatically exits the loop ('\n' != 'y'). You can make scanf ignore whitespace by using this format string instead:
" %c"
One solution can be the use fflush(stdin) after the scanf() statement to clear the input buffer.
I am new in C programming and I am currently learning about while loops. The problem I have is, that the while loop must continue until the user wishes to terminate the while loop. But when i run my code it seems that scanf() only once scans for input and the while loops terminates afterwards and I don't know why.
int main(void) {
setbuf(stdout, NULL);
char answer = 'y';
while (answer == 'y') {
printf("continue? (y/n): ");
scanf("%c", &answer);
}
return 0;
}
You need to consume the newline character.
Change
scanf("%c", &answer);
to
scanf(" %c", &answer);
Every character other than y terminates the loop. If you hit y<newline>, the y will cause the loop to run one more time and then the newline will terminate the loop.
The basic problem is that you are reading one character when you actually want to read one line.
Here is the while loop and switch in question (track1 defines a bigger loop not shown here):
while (track6 ==1)
{
printf ("would you like to play again? Y or N?\n");
scanf ("%c", &response);
switch (response)
{
case 'Y' : track6 = 2;
break;
case 'N' : printf ("thanks for playing!\n");
track6 = 2, track1 = 2;
break;
default : printf ("response is case-sensitive and must be either Y or N. your response is invalid. please reenter.\n");
}
}
The output I receive is:
would you like to play again? Y or N?
response is case-sensitive and must be either Y or N. your response is invalid. please reenter.
would you like to play again? Y or N?
(prompts for input and then executes correctly)
Seems like it is executing the first printf, skipping the scanf, executing the default, going back to the top of the loop and running properly from there. Any idea why? This is only my 3rd week programming so layman's terms are appreciated.
I think it's issue with scanf as it reads enter first - try getchar() or add a space character in scanf like " %c"
scanf (" %c", &response);
Seems like it is executing the first printf, skipping the scanf, executing the default, going back to the top of the loop and running properly from there. Any idea why?
No. Seems like it is executing the first printf statement, reading the newline (\n) character left behind by the previous scanf, going back to the top of the loop and running properly from there.
One possible solution to eat up this newline character is to change
scanf ("%c", &response);
to
scanf (" %c", &response);
^
|
add a space here to eat up '\n'
But this will work if and only if input from the user is either Y or N. If in case a user input YES or NO, more than one character (excluding \n) then your program should have to eat all these extra character to run the program properly. For this, use a loop just after the scanf in while;
printf ("would you like to play again? Y or N?\n");
scanf ("%c", &response);
while ((response = getchar()) != '\n' && response != EOF)
;
With
scanf ("%c", &response); you need to skip trailing newline from previous scanf
So use:
int ch;
while ((ch = getchar()) != '\n' && ch != EOF); //eats '\n'
after scanf
You need to flush the input buffer before using scanf to input a character.Use simply getchar().It'll eat the '\n' character present in the buffer.
#include <stdio.h>
void main()
{
char ans='n';
do
{
printf("\n Enter yes or no:");
scanf("%c",ans);
printf("\n entered %c",ans);
}while(ans == 'y');
}
As do while the loop is getting exccuted and that scanf is working and prnting my answer (say my answer is y) , its coming for 2nd time but not doing the scan and getting exited . May i know the reason for this ? why it is happening and what is the correct way to handle the infinite loop.
First up, you're missing a & in the scanf:
scanf("%c", &ans);
^
Second, you're not handling the newline, and the %c format specifier doesn't ignore blanks. So you read a character, press return, and the next scanf is immediately satisfied by that \n. To ignore blanks in scanf try:
scanf(" %c", &ans);
^
Not only are you missing the &address-of operator as indicated in other answers, but you're also missing the return value checks. Consider if a user presses CTRL+Z in Windows, or CTRL+d in Linux, to close stdin. Your loop would run infinitely and freeze your app ;)
if (scanf("%c", &ans) != 1) {
break;
}
Alternatively, I would suggest using getchar because it's far cleaner:
int main(void) { /* NOTE: There is no "void main()" entrance point in C. main should always return 'int'. */
int c;
do {
c = getchar();
} while (c == 'y');
return 0;
}
Use fflush(stdin) to flush those return feeds.
But when you are inputting a single character, then why not use getchar()?
EDIT:
As correctly pointed out by cnicutar here, fflush(stdin) has undefined behaviour. There doesn't seem to be any inbuilt function to take care of that and hence must be taken care of in the code itself.
One example could be:
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
Thanks for pointing that out there!
I am having trouble with inserting string in to char variable. Problem appeares when I put it into function. When I debug my program, it displays printf but it skipes gets
here is my code:
int uloz(SPRAVA *p){
char string[200];
printf("Your message here: ");
gets(string);
printf("You have entered: %s", string);
getchar();
return 0;
}
Use scanf(" %30[^\n]%*c",string);
[^\n] will accept anything till \n.
30 will limit the length of number of characters to max 30.
initial space(' ') will consume any \n already in stdin stream. (optional & i have not verified it)
& Finally, %*c will consume \n pressed after entering string.
I think, scanf(" %30[^\n]%*[^\n]%*c",string); would be a good option, to discard remaining characters (after 30) that were entered. However this is completely unverified. Just added as a possible idea. Test before use. :-)
There's a newline in the stdio buffer (left over by some previous scanf) so gets is immediately satisfied.
There's no easy way to fix it but you could try discarding input, before the fgets:
while((c = getchar()) != '\n' && c != EOF)
/* discard the character */;
The true solution is to avoid mixing scanf and fgets.
Use fgets instead of gets.