You are here:

C/Array of Structure Problem

Advertisement


Question
Dear Sir,
I have problem in array of structure. When data member of structure taken in float datatype. It cant initialize the float variable by scanf(). As it shows a runtime error that,
"scanf : floating point not linked. Abnormal program termination."
The program is,
#include<stdio.h>
#include<conio.h>
void main()
{
  struct book
  {
     int id;
     char name[25];
     float price;
  }a[3];
  int i;
  clrscr();
  for(i=0;i<3;i++)
  {
     printf("\n\n===================");
     printf("\n DATA %d",i+1);
     printf("\n===================");
     printf("\nEnter Book ID:");
     scanf("%d",&a[i].id);
     printf("Enter Book Name:");
     scanf("%s",a[i].name);
     printf("Enter the Price:");
     scanf("%f",&a[i].price);
  }
  for(i=0;i<3;i++)
  {
     printf("\n\n===================");
     printf("\n DATA %d",i+1);
     printf("\n===================");
     printf("\nBook ID: %d",a[i].id);
     printf("\nBook Name: %s",a[i].name);
     printf("\nPrice: %f",a[i].price);
  }
  getch();
}
Plz help me for this problem.

With Regards,
Praful S

Answer
It sounds like you're using an old compiler that tries to leave out floating point operations if they aren't used (and doesn't properly detect when they're used sometimes) such as Borland TurboC.  The work-around for this is to include some function that clues the compiler in to the fact that you are using floating points.  You can call a built-in function (such as sqrtf) or you can add a dummy function (which you don't even have to call) such as:

void dummy(float*f){float v=*f; dummy(&v);}

Hope that helps!

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.