954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pls help me with this Struct function

Hi there ... I need help with the following program.It is giving me error. Thanks very much.

/*Write a structure function to enter Roll number, Name, Department, Course, Year of joining and print them out */

#include <stdio.h>
struct student
{
	char name;
	char dep;
	char course;
	int roll;
	int year;
};
main( )
{
	struct student d1[5];
	name(d1) ;
}

name(struct student d2[5])
{
	int i;
	printf("Enter Roll Number,Name, Department, Course, Year of Joining\n");
	
	for(i=0;i<=2;i++){
	fflush(stdin);
	scanf("%d %s %s %s %d",&d2[i].roll,&d2[i].name,&d2[i].dep,&d2[i].course,&d2[i].year);
	}
	for(i=0;i<=2;i++)
	printf("\nRoll Number:%d Name:%s Department:%s Course:%s Year of Joining:%d",d2[i].roll,d2[i].name,d2[i].dep,d2[i].course,d2[i].year);
}
J-son
Newbie Poster
20 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

A few things:

it's int main() , and you have to return 0;

The other thing is, that you didn't tell the compiler what the function "name" should return. I'll asume it's void?
The last thing is that you didn't define the function "name" before you use it. So you'll have to move the function above main().

Also read this about using scanf and this about fflush

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Some additions:
1. If no returned type explicitly defined in C and C++, int assumed (backward compatibility with Stone Age of the C language). Of course, now it's bad style.
2. Effect of input stream fflush is not defined in the C language.
3. Declare name and dep members as arrays of char! Now you crash your program with attempt to input strings into single chars!
4. Use code structured indentation!
5. Never ask user to type a line without prompting.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Thanks very much for ur helps but sorry ... I don't get what u mean. I m just a beginner. Here I removed function and tested, it run, compiled but error with result.
What should I do? Thanks anyway.

#include <stdio.h>
int main( )
{
	struct student
		{
			char name;
			char dep;
			char course;
			int roll;
			int year;
		};
	struct student d2[3];
	int i;
	printf("Enter Roll Number,Name, Department, Course, Year of Joining\n");
	for (i=0;i<=2;i++){
			scanf("%d %c %c %c %d",&d2[i].roll,&d2[i].name,&d2[i].dep,&d2[i].course,&d2[i].year);
		}
	for (i=0;i<=2;i++)
		printf("\nRoll Number:%d Name:%c Department:%c Course:%c Year of Joining:%d",d2[i].roll,d2[i].name,d2[i].dep,d2[i].course,d2[i].year);
	return 0;
}
J-son
Newbie Poster
20 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

Hi there,

for (i=0;i<=2;i++){
		fflush(stdin);
			scanf("%d %c %c %c %d",&d2[i].roll,&d2[i].name,&d2[i].dep,&d2[i].course,&d2[i].year);
		}


Just add fflush(stdin) inside the for loop... this will solve your problem...
To remember : Always use fflush(stdin) before scanf() cause if not done, the input which you have given will remain in the input stream and it will get read by scanf next time...

ahamed101
Junior Poster
117 posts since Jul 2008
Reputation Points: 51
Solved Threads: 14
 

Never add fflush(stdin) - fflush() works with output or update streams only. See, for example:
http://www.opengroup.org/onlinepubs/000095399/functions/fflush.html

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

Hi ArkM,
How else can we clear the input stream?... I think the issue with the above code is basically the input stream is not cleared... I also read that the usage of fflush for stdin is undefined...


Regards,
Ahamed

ahamed101
Junior Poster
117 posts since Jul 2008
Reputation Points: 51
Solved Threads: 14
 

fflush should not work with input stream!
But in some compilers it does!
That's all

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

Hi ArkM, How else can we clear the input stream?... I think the issue with the above code is basically the input stream is not cleared... I also read that the usage of fflush for stdin is undefined...

Regards, Ahamed

Use this instead

void clrInputBuf( void )
{
     int ch;
     
     while( ( ch = getchar() ) != '\n' && ch != EOF );
}


ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

No portable solution of this problem in standard C. The C character stream stdin is not keyboard stream, you may attach it to a disk file, for example, and your program does not know about that fact. Really, as mentioned by Sci@phy, some compilers clear stdin stream buffer - VC, for example:
http://support.microsoft.com/kb/43993
But in this case you must clear BIOS keyboard input buffer too (see the article). Regrettably, no garantee that fflush(stdin) will supported by MS for the future compiler versions.

As far as no portable solutions, you can use, for example, non-standard header with old console i/o functions. Better don't mix stdio and conio input in that case (write some problem-oriented console i/o functions to compact and clear your code).

I have seen "good advices" to read stdin char by char until '\n' or EOF occured. I think, it's a bad idea: there are situations when user/program dialog hangs.

Yet another solution: don't worry, make a simple and clear interface - that's enough ;)...

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
I have seen "good advices" to read stdin char by char until '\n' or EOF occured. I think, it's a bad idea: there are situations when user/program dialog hangs.

Thats the only way which i can think off, which could be portable to certain extent. But as you say if it was used in some event driven cases then probably it might hang (Need to hit enter, since getchar is used!), if '\n' or EOF not found in the input buffer.

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

Thanks all of u

J-son
Newbie Poster
20 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

Thanks very much for ur helps but sorry ... I don't get what u mean. I m just a beginner. Here I removed function and tested, it run, compiled but error with result. What should I do? Thanks anyway.

#include <stdio.h>
int main( )
{
	struct student
		{
			char name;
			char dep;
			char course;
			int roll;
			int year;
		};
	struct student d2[3];
	int i;
	printf("Enter Roll Number,Name, Department, Course, Year of Joining\n");
	for (i=0;i<=2;i++){
			scanf("%d %c %c %c %d",&d2[i].roll,&d2[i].name,&d2[i].dep,&d2[i].course,&d2[i].year);
		}
	for (i=0;i<=2;i++)
		printf("\nRoll Number:%d Name:%c Department:%c Course:%c Year of Joining:%d",d2[i].roll,d2[i].name,d2[i].dep,d2[i].course,d2[i].year);
	return 0;
}

Is it resolved?

ahamed101
Junior Poster
117 posts since Jul 2008
Reputation Points: 51
Solved Threads: 14
 

you just have to make following change in your structure.....your progam will surely run....
you can use "getch();" to make output wait till you press any key...
it is included in

hope it will work....

struct student
{
	char name[10];
	char dep[10];
	char course[10];
	int roll;
	int year;
};
abhijeetcn
Newbie Poster
3 posts since Sep 2008
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You