ksanand_be
asked on
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\\dat a.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_e xp); // 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.
#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\\dat
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);
printf("\n Enter the current expenditure of %s", new_blk->name);
scanf("%f",&new_blk->cur_e
// 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.
ASKER
I tried modifying the code as told by you. Still the same result. :-(.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
{
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.
ASKER
Works to the perfection! Hats-off to the person who created this experts-exchange website and a million thanks to you.
haha right,
this is as much of a learning
process for me as it is for you.
this is as much of a learning
process for me as it is for you.
new_blk = (acc *)malloc(sizeof(acc));
printf("\nEnter the name of person %d", i);
scanf("%s",new_blk->name);
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);
See if that helps the error you're getting as well, or not.