• C

Error: Illegal use of floating point

Hi,

Please find my code pasted below. The code gives an error saying "Illegal use of floating point" at lines 224, 231, 238. The specified line numbers are commented using /*..*/ and are left aligned in the page. Basically what I'm trying to do is, to write the values from a single-linked list to a file after evaluating certain comparisons and expressions. Here is the code.

extern"C" {
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
}
void setnum(void);
int getnum(void);
//Two different structures have been used bcos data in data_cur.txt needs
//only two data - name and cur_exp.
//whereas data.txt needs all the data which is again used for final calculation
struct account_cur
{
 char *name;
 float cur_exp;
 };
struct account_data
{
  struct account_cur  *base_class;
  float cur_exp,prev_debit , prev_credit, perhead_exp ;
 struct account_data *next;
 };

typedef struct account_data acc_data;
//typedef struct account_cur acc_cur;
display(acc_data **);
void add(acc_data **);
calculate(acc_data *);
int main()
{
int i;
clrscr();
acc_data *list_acc_data;

clrscr();
//Print the various options available to the user
printf("1. Add records\n");
printf("2. View records\n");
printf("3. Calculate\n");
printf("4. Who spends the most\n");
printf("5. Graphical representation\n");
printf("6. Add/Delete a member\n");
scanf("%d", &i);
clrscr();
switch(i)
 {
   case 1: add(&list_acc_data);
         break;
   case 2: clrscr();
   display(&list_acc_data);
         break;
   case 3: calculate(list_acc_data);
         break;
   case 4:
   case 5:
   case 6: setnum();
         break;
   default:
   printf("Input data unknown\n");
  };
getch();
return(0);
}
display(acc_data **list_ptr)
 {
  FILE *fp_cur,*fp_data;
  int i,num;
  acc_data *temp,*new_blk;
  temp = *list_ptr;
  fp_data = fopen("c:\\tcplus\\tc\\data.txt", "r");
  //if (fp_cur==NULL)
   //printf("Unable to open file data_current.txt");
  if (fp_data == NULL)
   printf("Unable to open file data.txt");
  num = getnum();
  for(i=0;i<=num;i++)
   {
   new_blk = (acc_data *)malloc(sizeof(acc_data));
   fscanf(fp_data,"%s %f %f\n", new_blk->base_class->name, new_blk->prev_debit, new_blk->prev_credit);
   new_blk->next == NULL;
   if (*list_ptr == NULL)
     {
       temp = new_blk;
       *list_ptr = temp;
     }
    else
     {
      temp->next = new_blk;
      temp = temp->next;
     }
   }
  fclose(fp_data);
   getch();

/*    while(list_ptr != NULL)

    {
      printf("\n %s" ,list_ptr->base_class->name);
      list_ptr  = list_ptr->next;
     }*/

  }
 void add(acc_data **list_ptr)
 {
   FILE *fp_cur;
   acc_data *temp, *new_blk;
   temp = *list_ptr;
   int i,number;
   number = getnum();
   //Point to ponder: Everytime when cur_exp is input the previous data should be overwritten
   //Hence the file is opened in write mode.
   fp_cur = fopen("c:\\tcplus\\tc\\data_current.txt","w");
   if(fp_cur==NULL)
   printf("Error opening file\n");
   for(i=1;i<=number;i++)
   {
        new_blk = (acc_data *)malloc(sizeof(acc_data));
        printf("\nEnter the name of person %d", i);
        new_blk->base_class->name = (char *)malloc(30); //allocate memory to name
        scanf("%s",new_blk->base_class->name);  //This part gets written correctly
        printf("\n Enter the current expenditure of %s", new_blk->base_class->name);
        scanf("%f",&new_blk->base_class->cur_exp);
        fprintf(fp_cur,"%s     %2f\n ",new_blk->base_class->name, new_blk->base_class->cur_exp);
        new_blk->next = NULL;
        if(*list_ptr == NULL)
         {
         temp = new_blk;
         *list_ptr = temp;
        }
        else
         {
         temp->next = new_blk;
         temp = temp->next;
       }
  fclose(fp_cur);
   }
   }
   // the following lines were added to compensate for the disinclusion of
   //the floating point library by the borland or tc complier.
   //Refer documentation for more details
   static void forcefloat(float *p)
    {
     float f = *p;
     forcefloat(&f);
     }

   void setnum()
     {
       FILE *fp_stat;
       int num;
       fp_stat = fopen("c:\\tcplus\\tc\\statistics.txt","w");
       if(fp_stat ==NULL)
       printf("Unable to open file statistics.txt");
       else
      {
       printf("Enter the number of people in the house");
       scanf("%d",&num);
       fprintf(fp_stat,"Number of people in the house = %d",num);
       fprintf(stdout,"The number of people in the house is now set to %d", num);
       printf("\n press any key to continue...");
       getch();
      }
    }

    int getnum()
     {
       FILE *fp_stat;
       int num;
       fp_stat = fopen("c:\\tcplus\\tc\\statistics.txt","r");
       if(fp_stat ==NULL)
       printf("Unable to open file statistics.txt");
       else
      {
        fscanf(fp_stat,"%d",&num);
        fprintf(stdout,"Number of people in the house = %d",num);
      }
      return(num);
       }
     calculate(acc_data *list_ptr)
      {
       acc_data *temp,*new_blk;
       FILE *fp_cur, *fp_data;
       int number,i;
       number = getnum();
       float total = 0, average,debit, credit,curexp;
       char *name;
       temp = list_ptr;
       fp_cur  = fopen("c:\\tcplus\\tc\\data_current.txt","r+");
       fp_data = fopen("c:\\tcplus\\tc\\data.txt","r+");
       if(fp_cur ==NULL || fp_data ==NULL)
         printf("\nUnable to open the required files for calucation");
       else
        {
         for(i = 1; i<=number; i++)
         {
          new_blk = (acc_data *) malloc(sizeof(acc_data));
          fscanf(fp_cur,"%s %f %f", new_blk->base_class->name, &new_blk->base_class->cur_exp);
          fscanf(fp_data,"%f %f",new_blk->prev_debit,new_blk->prev_credit);
          total = total + new_blk->base_class->cur_exp;
           if(list_ptr == NULL)
            {
            temp = new_blk;
            new_blk->next = NULL;
            }
           else
            {
            temp->next = new_blk;
            temp = temp->next;
             }

           }
           fclose(fp_data);

        average = total/5;
        temp = list_ptr;
        fp_data = fopen("c:\\tcplus\\tc\\data.txt","w");
        for(i=1;i<=number;i++)
         {
           while(temp->next != NULL)
             {
             if(temp->base_class->cur_exp == average)
                {
                 temp->prev_debit = 0.0;
                 temp->prev_credit = 0.0;
 /*line no. 224 */                 fprintf(fp_data,("%s %2f %2f",temp->base_class->name, temp->prev_debit, temp->prev_credit));//error: Illegal use of floating point
                 }
               else if(temp->base_class->cur_exp > average)
                 {
                  debit = 0;
                  credit = credit + (temp->base_class->cur_exp - average);
                  temp->prev_credit = credit;
/*line no. 231*/                  fprintf(fp_data, ("%s %f %f",temp->base_class->name,temp->prev_debit, temp->prev_credit));
                   }
                else
                 {
                  credit = 0;
                  debit = debit + (average-temp->base_class->cur_exp);
                  temp->prev_debit = debit;
/*line no. 238*/                  fprintf(fp_data, ("%d %f %f",temp->base_class->name, temp->prev_credit, temp->prev_credit));
                  }
                }
              }
             }
            }



-------------
Thankyou.
-------------


ksanand_beAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grg99Commented:
You dont want parentheses around the ("format string", v1,v2,v3)    things.   The C compiler thinks you're specifying an expression with multiple sub-expressions, thanks to the very overloaded comma operator.  

That should help.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ksanand_beAuthor Commented:
How silly could I be?
Thankyou anyway.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.