[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 889
  • Last Modified:

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.
0
ksanand_be
Asked:
ksanand_be
  • 3
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now