Help with C programming: here is my code, and I can't show the final result!
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> typedef struct{char names[30]; int age; char position[30]; char sex;} data; void age(data *emp; int n;); void sex(data *emp; int n;); void name(data *emp; int n;); void pos(data *emp; int n;); void show(data *emp; int n;); data *emp=0; int n=0,i; char ch; void input(data *emp); int main(){ printf("Input employees' information here:\n"); again: printf("\nEmployee[%d]:\n",n+1); emp=(data*)realloc(emp,(n+1)*sizeof(data)); if(emp==0) return 0; printf("%10s:","Name"); scanf("%30[^\n]",(emp+n)->names); fflush(stdin); printf("%10s:","age"); scanf("%d",&((emp+n)->age)); fflush(stdin); printf("%10s:","Position"); scanf("%30[^\n]",(emp+n)->position); fflush(stdin); printf("%10s:","Sex"); scanf("%c",&((emp+n)->sex)); fflush(stdin); n++; printf("\nMore employee? (ESC to stop): "); if(getch()!=27) goto again; printf("Here are all of your employees' information:"); for(i=0;i<n;i++){ printf("\n[%d]",i+1); printf("\t%s",emp[i].names); printf("\t%d",emp[i].age); printf("\t%s",emp[i].position); printf("\t%c",emp[i].sex);} printf("\nWhat do you want data to be sorted by?\n"); printf("1/ names\n2/ age\n3/ position\n4/ sex\n"); printf("\nYour choice: "); ch=getche(); switch(ch){ case 1 : name(emp, n); show(emp, n); break; case 2 : age(emp, n); show(emp, n); break; case 3 : pos(emp, n); show(emp, n); break; case 4 : sex(emp, n); show(emp, n); break;} return 0; } void name(data *emp; int n;){ int i,j; char temp[30]; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(emp[i].names,emp[j].names)>0) {strcpy(temp,emp[i].names); strcpy(emp[i].names,emp[j].names); strcpy(emp[j].names,temp);} } void pos(data *emp; int n;){ int i,j; char temp[30]; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(emp[i].position,emp[j].position)>0) {strcpy(temp,emp[i].position); strcpy(emp[i].position,emp[j].position); strcpy(emp[j].position,temp);} } void age(data *emp; int n;){ int i,j, temp; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++){ if(emp[i].age>emp[j].age) {temp=emp[i].age; emp[i].age=emp[j].age; emp[j].age=temp;} } } void sex(data *emp; int n;){ int i,j; char temp; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++){ if(emp[i].sex!=emp[j].sex) {temp=emp[i].sex; emp[i].sex=emp[j].sex; emp[j].sex=temp;} } } void show(data *emp; int n;){ int i; printf("\nHere are sorted lists:\n\n"); for(i=0;i<n;i++){ printf("\n[%d]",i+1); printf("\t%s",emp[i].names); printf("\t%d",emp[i].age); printf("\t%s",emp[i].position); printf("\t%c",emp[i].sex);} }
Can you use a code paste site, like pastebin, github's gist, or one of the others? If people copy and paste from OpenStudy, it removes all newline markers and makes a mess of code.
One problem that I see here, unless it is a formatting anomaly, is that your name, pos, age, and sex functions are missing braces.
You also separate your arguments being passed to your functions with a ';'(semicolon) instead of a ','(comma)
Did this program compile for you?
I have changed the ; into commas, but it still didn't show the final result
And I compiled this in CodeBlock, and it worked also.
thanks. I have found the solution. :)
Glad to hear it.
Join our real-time social learning platform and learn together with your friends!