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);
}

Recommended Answers

All 13 Replies

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

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.

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;
}

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...

commented: Stop recommending fflush(stdin). It's just plain wrong. -4

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

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

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

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 <conio.h> 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 ;)...

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

Thanks all of u

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?

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 <conio.h>

hope it will work....

struct student
{
	char name[10];
	char dep[10];
	char course[10];
	int roll;
	int year;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.