LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-13-2006, 03:31 PM   #1
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Rep: Reputation: 30
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?

Last edited by manishsingh4u; 05-13-2006 at 03:51 PM.
 
Old 05-13-2006, 03:51 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
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
 
Old 05-13-2006, 03:56 PM   #3
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Original Poster
Rep: Reputation: 30
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?
 
Old 05-14-2006, 04:28 AM   #4
nhydra
Member
 
Registered: May 2006
Distribution: Fedora Core, FreeSpire, PC-BSD, NetBSD
Posts: 96

Rep: Reputation: 15
Here is a useful information about Linux programming.
http://www.gnu.org/software/libc/man...ode/index.html
 
Old 05-14-2006, 09:37 AM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 05-14-2006, 09:45 AM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
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.
 
Old 05-15-2006, 03:55 AM   #7
manishsingh4u
Member
 
Registered: Oct 2005
Location: Bhopal, India
Distribution: RHEL 6
Posts: 422

Original Poster
Rep: Reputation: 30
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.

Last edited by manishsingh4u; 05-15-2006 at 03:56 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SDL problem... simple program. RHLinuxGUY Programming 1 07-22-2005 06:04 PM
simple C program problem speel Programming 14 02-27-2005 05:08 PM
simple C program problem mined Programming 2 05-08-2004 05:42 AM
simple program bprasanth_20 Programming 5 10-29-2003 12:55 AM
simple program big problem antken Programming 3 09-15-2002 10:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration