Pls help me with this Struct function

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 20
Reputation: J-son is an unknown quantity at this point 
Solved Threads: 0
J-son's Avatar
J-son J-son is offline Offline
Newbie Poster

Pls help me with this Struct function

 
0
  #1
Sep 11th, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,978
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Pls help me with this Struct function

 
0
  #2
Sep 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Pls help me with this Struct function

 
0
  #3
Sep 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 20
Reputation: J-son is an unknown quantity at this point 
Solved Threads: 0
J-son's Avatar
J-son J-son is offline Offline
Newbie Poster

Re: Pls help me with this Struct function

 
0
  #4
Sep 11th, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: Pls help me with this Struct function

 
-1
  #5
Sep 28th, 2008
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...
regards,
Ahamed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Pls help me with this Struct function

 
0
  #6
Sep 28th, 2008
Never add fflush(stdin) - fflush() works with output or update streams only. See, for example:
http://www.opengroup.org/onlinepubs/...ns/fflush.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 113
Reputation: ahamed101 is an unknown quantity at this point 
Solved Threads: 14
ahamed101's Avatar
ahamed101 ahamed101 is offline Offline
Junior Poster

Re: Pls help me with this Struct function

 
0
  #7
Sep 28th, 2008
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
regards,
Ahamed.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Pls help me with this Struct function

 
0
  #8
Sep 28th, 2008
fflush should not work with input stream!
But in some compilers it does!
That's all
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 251
Reputation: ssharish2005 is on a distinguished road 
Solved Threads: 20
ssharish2005's Avatar
ssharish2005 ssharish2005 is offline Offline
Posting Whiz in Training

Re: Pls help me with this Struct function

 
0
  #9
Sep 28th, 2008
Originally Posted by ahamed101 View Post
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
"Any fool can know, point is to understand"
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Pls help me with this Struct function

 
0
  #10
Sep 28th, 2008
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 ...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1393 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC