Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,351 people online right now. Registration is fast and FREE... Join Now!
my whole program including all comments. it's not working actualy because of an error .. in smallest_largest function ! the compiler said " invalid operands to binary > " i didn't understand the error .. please, i need help !!
CODE
/**************************************************************/ /* Assigment 2 */ /* */ /* This Program prompt the user to enter an operation code, */ /* then calls a function to perform the requested action. */ /* Repeat until the user the command'Q'. */ /* */ /**************************************************************/ #include <stdio.h> #include <string.h>
typedef struct record { int ID; char fname[30]; char lname[30]; float midterm; float final; } student;
/*************************************************************/ /* This void Function allow the user to insert a new student */ /* record into array,the function checks first if the record */ /* is already in the array by comparing the new ID with all */ /* IDs in list. */ /*************************************************************/
void insert_record( student a[], int *n ) { int ID; printf("Enter a student ID to insert : "); scanf("%d",&ID); printf("check for record in the array ..\n"); if( ID>0 && *n<100 ) { if( a[*n].ID==ID ) printf("Record is already inserted"); else { a[*n].ID=ID; printf("Enter student's first name : "); scanf("%s",a[*n].fname); printf("Enter student's last name : "); scanf("%s",a[*n].lname); printf("Enter student's midterm grade : "); scanf("%f",&a[*n].midterm); printf("Enter student's final grade : "); scanf("%f",&a[*n].final); insert_record(a,n+1); } } } /*************************************************************/ /* This void Function delete a student record using ID, then */ /* shift the array to fill up the gap created by the deleted */ /* record. */ /*************************************************************/
void delete_record( student a[], int n ) { int ID, i, pos; printf("Enter a student ID to delete : "); scanf( "%d",&ID );
for ( i=0; i<n; ++i ) if( a[i].ID==ID ) pos=i;
for( i=pos; i<n-1; ++i) a[i]=a[i+1];
} /*************************************************************/ /* This void Function print the average of final exam . */ /*************************************************************/
void average( student a[], int n ) { float avg; float sum=0; int i;
for( i=0; i<n; ++i ) sum=sum+a[i].final; avg=sum/n; printf("The average of final = %f \n", avg ); } /*************************************************************/ /* This Function of student type print the smallest& largest */ /* score of the final exam . */ /*************************************************************/
float smallest_largest( student a[], int n, float *L, float *S ) { int i; for( i=0; i<n; i++ ) { if( a[i].final>L ) L=a[i].final; if( a[i].final<S ) S=a[i].final; } } } /*************************************************************/ /* This void Function sort the array by descending order */ /* according to the first name . */ /*************************************************************/
void sort_record( student a[], int n ) { student temp; int i, j, pos; for( i=0; i<n; ++i ) { temp=a[i]; pos=i;
for( j=i+1; j<n; ++j ) if( strcmp(a[j].fname,temp.fname)==1 ) { temp=a[i]; pos=j; } a[pos]=a[i]; a[i]=temp; } } /*************************************************************/ /* The MAIN function, calls all functions . */ /*************************************************************/
main () { student a[100]; char ch; int n=0; float large=0; float small=100;
printf( "Enter an operation code: \n"); printf("1) I, to insert a new record. \n"); printf("2) D, to delete a record from the array. \n"); printf("3) F, to print the average of final exam. \n"); printf("4) C, to print the smallest & largest of the final exam. \n"); printf("5) S, to sort the array by descending order according to first name .\n"); printf("6) Q, to quit the program. \n"); scanf("%c",&ch);
while( ch!='Q'&&ch!='q' ) { if( ch=='I'||ch=='i' ) insert_recerd(a,&n); else if( ch=='D'||ch=='d') delete_record(a,n); else if( ch=='F'||ch=='f') average(a,n); else if( ch=='C'||ch=='c') { smallest_largest(a,n,&L,&S); printf("The Largest score of final exam = %f\n",L); printf("The smallest score of final exam = %f\n",S); } else if( ch=='S'||ch=='s') sort_record(a,n); else printf("Undefined operation code !!! \n"); printf("please, Try another operation code \n");
The above is incorrect. You really should specify the return type of main. IE, int main ()
Issue Number 3: In main(), you have this smallest_largest(a,n,&L,&S);
The problem is that you have not declared L or S. You have the variables large and small, which I assume were meant to go there. IE, just replace L with large, and S with small.
If you correct the above issues, you should be able to compile.
Cerolobo thanx for UR help, i realy appreciate it .
but there still 1 problem, after adding some details & compiling i only can use the programe once for one operation only, if i wanna call another operation as inserting a student info. again .. a new problem apears ?! DO ANY BODY KNOW WHY ,PLZ ?!!!!!
CODE
/**************************************************************/ /* Assigment 2 */ /* Computer Programming 206 \ 01a */ /* By : Sara Abdullah Al-AQeel , ID : 206114181 */ /* */ /* This Program prompt the user to enter an operation code, */ /* then calls a function to perform the requested action. */ /* Repeat until the user the command'Q'. */ /* */ /**************************************************************/ #include <stdio.h> #include <string.h>
typedef struct record { int ID; char fname[30]; char lname[30]; float midterm; float final; } student;
/*************************************************************/ /* This void Function allow the user to insert a new student */ /* record into array,the function checks first if the record */ /* is already in the array by comparing the new ID with all */ /* IDs in list. */ /*************************************************************/
void insert_record( student a[], int *n ) { int ID; printf("Enter a student ID to insert : "); scanf("%d",&ID); printf("check for record in the array ..\n"); if( ID>0 && *n<100 ) { if( a[*n].ID==ID ) printf("Record is already inserted"); else { a[*n].ID=ID; printf("Enter student's first name : "); scanf("%s",a[*n].fname); printf("Enter student's last name : "); scanf("%s",a[*n].lname); printf("Enter student's midterm grade : "); scanf("%f",&a[*n].midterm); printf("Enter student's final grade : "); scanf("%f",&a[*n].final); } } } /*************************************************************/ /* This void Function delete a student record using ID, then */ /* shift the array to fill up the gap created by the deleted */ /* record. */ /*************************************************************/
void delete_record( student a[], int *n ) { int ID, i, pos; printf("Enter a student ID to delete : "); scanf( "%d",&ID );
for ( i=0; i<*n; ++i ) if( a[i].ID==ID ) pos=i;
for( i=pos; i<*n-1; ++i) a[i]=a[i+1]; (*n)--;
} /*************************************************************/ /* This void Function print the average of final exam . */ /*************************************************************/
void average( student a[], int n ) { float avg; float sum=0; int i;
for( i=0; i<n; ++i ) sum=sum+a[i].final; avg=sum/n; printf("The average of final = %f \n", avg ); } /*************************************************************/ /* This Function of student type print the smallest& largest */ /* score of the final exam . */ /*************************************************************/
void smallest_largest( student a[], int n, float *L, float *S ) { int flag=0; if( n==-1 ) return; else { if( flag==0) { *L=a[n].final; *S=a[n].final; flag=1; }
else { if( *L<a[n].final ) *L=a[n].final; else if( *S>a[n].final ) *S=a[n].final; smallest_largest(a,n-1,L,S); } } } /*************************************************************/ /* This void Function sort the array by descending order */ /* according to the first name . */ /*************************************************************/
void sort_record( student a[], int n ) { student temp; int i, j, pos; for( i=0; i<n; ++i ) { temp=a[i]; pos=i;
for( j=i+1; j<n; ++j ) if( strcmp(a[j].fname,temp.fname)==1 ) { temp=a[i]; pos=j; } a[pos]=a[i]; a[i]=temp; } } /*************************************************************/ /* The MAIN function, calls all functions . */ /*************************************************************/
main() { student a[100]; char ch; int n=0; float L,S;
printf( "Enter an operation code: \n"); printf("1) I, to insert a new record. \n"); printf("2) D, to delete a record from the array. \n"); printf("3) F, to print the average of final exam. \n"); printf("4) C, to print the smallest & largest of the final exam. \n"); printf("5) S, to sort the array by descending order according to first name .\n"); printf("6) Q, to quit the program. \n"); scanf("%c",&ch);
while( ch!='Q'&&ch!='q' ) { if( ch=='I'||ch=='i' ) insert_record(a,&n); else if( ch=='D'||ch=='d') delete_record(a,&n); else if( ch=='F'||ch=='f') average(a,n); else if( ch=='C'||ch=='c') { smallest_largest(a,n,&L,&S); printf("The Largest score of final exam = %f\n",&L); printf("The smallest score of final exam = %f\n",&S); } else if( ch=='S'||ch=='s') sort_record(a,n); else printf("Undefined operation code !!! \n"); printf("please, Try another operation code \n");
scanf(" %c",ch); }
return 0;
}
This post has been edited by Mi$s-Q8: 8 Jul, 2008 - 09:15 AM