ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include<stdio.h>
int main()
{
int x=10;
char ch;
do
{
printf("%d",x);
printf("\nDo you want it again? ");
scanf("%c",&ch); //problem is here
}while(ch=='y'||ch=='Y');
return 0;
}
on compiling and running it, the output is as shown
[bryan@laptop ~]$ gcc test_do_while.c -o test_do_while.o
[bryan@laptop ~]$ ./test_do_while.o
10
Do you want it again? y
10
Do you want it again? [bryan@laptop ~]$
The second time it does not wait for a character to be entered not even with getch() and fgetc(stdin)
but if i add a space in the scanf statement of ch as shown
#include<stdio.h>
int main()
{
int x=10;
char ch;
do
{
printf("%d",x);
printf("\nDo you want it again? ");
scanf(" %c",&ch); //space added before %c inside double quotes
}while(ch=='y'||ch=='Y');
return 0;
}
the program works fine.............
the problem is same with other loops too............. adding the space there removes the problem...........
can anyone tell me the reason for this......please.........
Because in the first case it reads the newline character too, when you press Enter.
man scanf explains it here:
Code:
c Matches a sequence of characters whose length is specified by
the maximum field width (default 1); the next pointer must be a
pointer to char, and there must be enough room for all the char-
acters (no terminating null byte is added). The usual skip of
leading white space is suppressed. To skip white space first,
use an explicit space in the format.
Just remember that the newline character is a "white space character".
Distribution: Fedora 7, RHEL5, CentOS 5 and Backtrack 2.0
Posts: 73
Rep:
uncle_theoddre (sorry for spelling mistake) is right. the '\n' character gets qued up and is accepted as the arguement when the second time scanf statement is executed.
you can add the statement
fflush(stdin);
this would flush up the standard input buffer so u won't have any problem but this is not a permanent solution as fflush statement sometimes does not seem to function properly.....okay.....so use at your own risk!!!!!!!!!!!
Distribution: Fedora 7, RHEL5, CentOS 5 and Backtrack 2.0
Posts: 73
Rep:
well mimithebrain,
its not so u can use fflush for stdin also but sometimes it may happen dat fflush is unable to flush the input buffer, i dont know the reason but its true.......
Yours is a variation on the popular 'character-at-time' terminal IO problem, or what I like to call the 'getch()' problem. Google search for "character-at-a-time IO" for a long list of solutions and descriptions of the issues involved. Searching for 'getch' in this forum will also lead to informative threads.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.