LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem - A simple C program using Structures (https://www.linuxquestions.org/questions/programming-9/problem-a-simple-c-program-using-structures-444484/)

manishsingh4u 05-13-2006 03:31 PM

Problem - A simple C program using Structures
 
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?

spooon 05-13-2006 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

manishsingh4u 05-13-2006 03:56 PM

Oh thanks. So, how to overcome this problem. I compiled this program on one of my friends windows machine by adding a line
Code:

fflush (stdin);
before reading the character for gender. And the program ran fine. So, how to do this on linux?

nhydra 05-14-2006 04:28 AM

Here is a useful information about Linux programming.
http://www.gnu.org/software/libc/man...ode/index.html

exvor 05-14-2006 09:37 AM

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.

exvor 05-14-2006 09:45 AM

I found out why.

here is the excerpt from Cprogramming.com
Quote:


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.


manishsingh4u 05-15-2006 03:55 AM

Quote:

Originally Posted by exvor
I found out why.
here is the excerpt from Cprogramming.com
...
int fflush(FILE *ostream);
...

Thanks for the information. It is really helpful.


All times are GMT -5. The time now is 06:06 PM.