| | |
Pls help me with this Struct function
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi there ... I need help with the following program.It is giving me error. Thanks very much.
c Syntax (Toggle Plain Text)
/*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); }
A few things:
it's
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
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.
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.
What should I do? Thanks anyway.
c Syntax (Toggle Plain Text)
#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,
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...
C Syntax (Toggle Plain Text)
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...
regards,
Ahamed.
Ahamed.
Never add fflush(stdin) - fflush() works with output or update streams only. See, for example:
http://www.opengroup.org/onlinepubs/...ns/fflush.html
http://www.opengroup.org/onlinepubs/...ns/fflush.html
•
•
•
•
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
C Syntax (Toggle Plain Text)
void clrInputBuf( void ) { int ch; while( ( ch = getchar() ) != '\n' && ch != EOF ); }
ssharish
"Any fool can know, point is to understand"
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
...
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
... ![]() |
Similar Threads
- need help in C (C)
- Self documentation (C++)
- NEED help pls. about linked list (C++)
- help me pls!!!some of my functions doesn't work! (C)
- Pls help me ;'=' : 'unsigned char ' differs in levels of indirection from 'void *' (C++)
- opcode and instructions in bits (C++)
Other Threads in the C Forum
- Previous Thread: comparing a function with library power
- Next Thread: Problem related to c in looping
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






