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.
Sorry friends for starting a new thread for such a simple problem.
I have a very simple program which is not running fine as expected. I know there's some mistake from my side but where?
Code:
#include<stdio.h>
struct s
{
char name[20];
int age;
int fare;
char gender;
}s[3];
int main()
{
int i,sum=0;
for(i=0;i<3;i++)
{
printf("\nenter the name of passenger ");
scanf("%s",s[i].name);
printf("\nenter the age of the passenger ");
scanf("%d",&s[i].age);
printf("\nenter the fare to collect ");
scanf("%d",&s[i].fare);
printf("\nenter the gender of passenger ");
scanf("%c",&s[i].gender);
if(s[i].gender=='m')
{
sum=sum+s[i].fare;
}
}
printf("\nthe totale fare collected from male is : %d",sum);
return (0);
}
The program runs like this.
Code:
mann@Manish:~/Desktop$ cc struc.c -o struc
mann@Manish:~/Desktop$ ./struc
enter the name of passenger Manish
enter the age of the passenger 25
enter the fare to collect 111
enter the gender of passenger
enter the name of passenger
I don't know why the program does not allow me to input for gender. that is it goes straight to name for next value for i.
Any suggesttions about where I am doing it wrong?
Last edited by manishsingh4u; 05-13-2006 at 03:51 PM.
The problem is that when you tell scanf to read the numbers it gets all the digits but leaves the newline ('\n') at the end of the line on the stream. So when you try to read a character next time, it just reads the newline character. scanf is messed up like that
Yea the fflush works diffrent in linux then it does in dos. Not sure why this happens but i suspect its how linux buffers the data and how dos buffers the data.
On occasions you may need to clear unwanted data in an input stream, most commonly keyboard input. One frequently suggested way of doing this is by using fflush(stdin). This is incorrect, and should be avoided, here is why. To learn how to flush the input buffer correctly, read this FAQ entry.
stdin is a standard FILE* variable that points to the input stream normally used for keyboard input. The fflush() function is deemed to flush buffers. Put the two together and you have a method for clearing the input stream easily, right? WRONG! This is a common misconception in C and C++ programming, an extract from the C standard will help explain:
int fflush(FILE *ostream);
ostream points to an output stream or an update stream in which the
most recent operation was not input, the fflush function causes any
unwritten data for that stream to be delivered to the host environment to
be written to the file; otherwise, the behavior is undefined.
So, if the file stream is for input use, as stdin is, the behaviour is undefined, therefore it is not acceptable to use fflush() for clearing keyboard input.
As usual, there are some exceptions, check your compiler's documentation to see if it has a (non-portable) method for flushing input.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.