• C

Abnormal program termination

extern"C" {
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
}

struct account
{
 char *name;
 float cur_exp,prev_debit, prev_credit;
 struct account *next;
 };
typedef struct account acc;
display(acc * );
acc add(acc **);
int main()
{
int i;
clrscr();
acc *list_acc;
list_acc = NULL;
clrscr();
//Print the various options available to the user
printf("1. View the records\n");
printf("2. Calcuate current per head expenditure\n");
printf("3. View total expenditure till date\n");
printf("4. Who spends the most\n");
printf("5. Graphical representation\n");
printf("6. Add/Delete a member\n");
scanf("%d", &i);
switch(i)
 {
   case 1: printf ("Viewing records");
         add(&list_acc);
         display(list_acc);
         break;

   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   default:
   printf("Input data unknown\n");
  };
getch();
}
display(acc *list_ptr)
 {
    while(list_ptr != NULL)

    {
      printf("\n %s" ,list_ptr->name);
      list_ptr  = list_ptr->next;
     }
  }
acc add(acc **list_ptr)
 {
   FILE *fp;
   acc *temp, *new_blk;
   temp = *list_ptr;
   int i,number;
   printf(" Enter the number of people in your house");
   scanf("%d", &number);
   fp = fopen("c:\\tcplus\\tc\\data.txt","r+");
   if(fp==NULL)
   printf("Error opening file\n");  
   for(i=1;i<=number;i++)
   {
        new_blk = (acc *)malloc(sizeof(acc));
        printf("\nEnter the name of person %d", i);
        scanf("%s",new_blk->name);                     //This part gets written correctly when i used only the name variable
        printf("\n Enter the current expenditure of %s", new_blk->name);
        scanf("%f",&new_blk->cur_exp);      // This section gives error
                                                                      // says scanf : floating point formats not linked
                                                                    //Abnormal program termination  
        fprintf(fp,"%s     %2f\n ",new_blk->name, new_blk->cur_exp);
        new_blk->next = NULL;
        if(*list_ptr == NULL)
         {
         temp = new_blk;
         *list_ptr = temp;
        }
        else
         {
         temp->next = new_blk;
         temp = temp->next;
       }

   }
   }

It would be highly appreciable if someone could explain the meaning of the error message.

Thanks.
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.

imladrisCommented:
I don't get such an error when running this code. However, I did note (when I ran it) that you allocate memory for newblk in:

       new_blk = (acc *)malloc(sizeof(acc));
       printf("\nEnter the name of person %d", i);
       scanf("%s",new_blk->name);                     //This part gets written

but the name element of new_blk is merely: char *name.

Thus the scanf will be, at best, putting the name it reads into random memory, which could lead to all kinds of strange results past that statement.
You should allocate memory for name as well; something like:

       new_blk = (acc *)malloc(sizeof(acc));
       new_blk->name=(char *)malloc(30);  // allocate memory to name
       printf("\nEnter the name of person %d", i);
       scanf("%s",new_blk->name);                     //This part gets written

See if that helps the error you're getting as well, or not.
0
ksanand_beAuthor Commented:
I tried modifying the code as told by you. Still the same result. :-(.

0
van_dyCommented:
http://www.jeffc.org/msdos/dos-faq.html#Q0304

take a look here, i hope it will solve ur problem
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

van_dyCommented:
static void forcefloat(float *p)
   {
     float f = *p;
     forcefloat(&f);
   }

it says there, that just place this piece of code
in some source file, and compile it along with
your main.cpp file.
0
ksanand_beAuthor Commented:
Works to the perfection! Hats-off to the person who created this experts-exchange website and a million thanks to you.

0
van_dyCommented:
haha right,
     this is as much of a learning
process for me as it is for you.
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.