Welcome to Dream.In.Code
Become a C++ Expert!

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!




Error: invalid operands to binary

 
Reply to this topicStart new topic

Error: invalid operands to binary, student information system

Mi$s-Q8
7 Jul, 2008 - 09:09 AM
Post #1

New D.I.C Head
*

Joined: 30 Apr, 2008
Posts: 22


My Contributions
hi,

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 .. blink.gif
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");
                
            scanf("%c",ch);
            }
            
     return 0;
    
}
                
            
    
            
    
                        
                        
        
                        
                        

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Error: Invalid Operands To Binary
7 Jul, 2008 - 09:29 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,982



Thanked: 43 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Modified title to be more descriptive.
User is online!Profile CardPM
+Quote Post

Mi$s-Q8
RE: Error: Invalid Operands To Binary
7 Jul, 2008 - 09:35 AM
Post #3

New D.I.C Head
*

Joined: 30 Apr, 2008
Posts: 22


My Contributions
i'm sorry for not being specific, but i dont think that is the only problem in my program !

any way, thanx.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Error: Invalid Operands To Binary
7 Jul, 2008 - 01:47 PM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
Ok, there are three issues you should correct

Issue Number 1:
In smallest_largest(), you declared the variables L and S and pointers to float, yet you are comparing them as so.

CODE
           if( a[i].final>L )
               L=a[i].final;
           if( a[i].final<S )
               S=a[i].final;

So, in the first if, you are comparing the student's final grade (which is a float), with a pointer to a float.

To compare the final grade to what ever L is pointing at, you simply need to deference L. The same goes for S.

CODE
           if( a[i].final>*L )
               *L=a[i].final;
           if( a[i].final<*S )
               *S=a[i].final;



Issue Number 2:
main ()

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.
User is offlineProfile CardPM
+Quote Post

Mi$s-Q8
RE: Error: Invalid Operands To Binary
8 Jul, 2008 - 09:14 AM
Post #5

New D.I.C Head
*

Joined: 30 Apr, 2008
Posts: 22


My Contributions
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
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Error: Invalid Operands To Binary
8 Jul, 2008 - 09:44 AM
Post #6

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
From what I can tell, you never increase the value of n when you insert a value.

IE, every time you call insert_record(), you will be inserting at index 0.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:45PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month