943,723 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1863
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 11th, 2008
0

Pls help me with this Struct function

Expand Post »
Hi there ... I need help with the following program.It is giving me error. Thanks very much.
  1. /*Write a structure function to enter Roll number, Name, Department, Course, Year of joining and print them out */
  2.  
  3. #include <stdio.h>
  4. struct student
  5. {
  6. char name;
  7. char dep;
  8. char course;
  9. int roll;
  10. int year;
  11. };
  12. main( )
  13. {
  14. struct student d1[5];
  15. name(d1) ;
  16. }
  17.  
  18. name(struct student d2[5])
  19. {
  20. int i;
  21. printf("Enter Roll Number,Name, Department, Course, Year of Joining\n");
  22.  
  23. for(i=0;i<=2;i++){
  24. fflush(stdin);
  25. scanf("%d %s %s %s %d",&d2[i].roll,&d2[i].name,&d2[i].dep,&d2[i].course,&d2[i].year);
  26. }
  27. for(i=0;i<=2;i++)
  28. 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);
  29. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
J-son is offline Offline
20 posts
since May 2008
Sep 11th, 2008
0

Re: Pls help me with this Struct function

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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 11th, 2008
0

Re: Pls help me with this Struct function

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Sep 11th, 2008
0

Re: Pls help me with this Struct function

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.
  1. #include <stdio.h>
  2. int main( )
  3. {
  4. struct student
  5. {
  6. char name;
  7. char dep;
  8. char course;
  9. int roll;
  10. int year;
  11. };
  12. struct student d2[3];
  13. int i;
  14. printf("Enter Roll Number,Name, Department, Course, Year of Joining\n");
  15. for (i=0;i<=2;i++){
  16. scanf("%d %c %c %c %d",&d2[i].roll,&d2[i].name,&d2[i].dep,&d2[i].course,&d2[i].year);
  17. }
  18. for (i=0;i<=2;i++)
  19. 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);
  20. return 0;
  21. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
J-son is offline Offline
20 posts
since May 2008
Sep 28th, 2008
-1

Re: Pls help me with this Struct function

Hi there,

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

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...
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Sep 28th, 2008
0

Re: Pls help me with this Struct function

Never add fflush(stdin) - fflush() works with output or update streams only. See, for example:
http://www.opengroup.org/onlinepubs/...ns/fflush.html
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Sep 28th, 2008
0

Re: Pls help me with this Struct function

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
Reputation Points: 51
Solved Threads: 14
Junior Poster
ahamed101 is offline Offline
114 posts
since Jul 2008
Sep 28th, 2008
0

Re: Pls help me with this Struct function

fflush should not work with input stream!
But in some compilers it does!
That's all
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Sep 28th, 2008
0

Re: Pls help me with this Struct function

Click to Expand / Collapse  Quote originally posted by ahamed101 ...
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

  1. void clrInputBuf( void )
  2. {
  3. int ch;
  4.  
  5. while( ( ch = getchar() ) != '\n' && ch != EOF );
  6. }

ssharish
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006
Sep 28th, 2008
0

Re: Pls help me with this Struct function

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 ...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: comparing a function with library power
Next Thread in C Forum Timeline: Problem related to c in looping





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC