C/C

Advertisement


Question
QUESTION: Dear Sir,

I am finding a problem while i am declareing an array of structure. At the time of scaning the elements, the program treminates and giving the "floating point" related problem. The program using structure is given below:

#include<stdio.h>
#include<conio.h>
struct employee
{
char name[25];
int age;
float salary;
};
void main()
{
struct employee emp[10];
int i=0;
clrscr();
for(i=0;i<2;i++)
{
printf("E n t e r %d\n",i);
scanf("%s %d ",emp[i].name,&emp[i].age);
scanf("%f",emp[i].salary);          //error at this point
}
for(i=1;i<2;i++)
{
printf("D i s p l a y %d\n",i);
printf("%s %d %f",emp[i].name,emp[i].age,emp[i].salary);
}
getch();
}

Regards

Kavitha

ANSWER: Hi, Kavitha.

I cannot know for sure what you're seeing without knowing what you're using as input.  As a general rule of thumb, though, you need to be very, very careful using scanf and similar functions, especially when reading strings.  If you overrun the boundaries of the character array, that will cause problems.  Also, because of house scanf works, if you try to enter a first and a last name, it will read each as separate input.  Scanf also does not clear the buffer, so you're left with some data inside the buffer after the read (such as the newline character).  Chances are, these are the cause of any problems you are seeing.

---------- FOLLOW-UP ----------

QUESTION: Sir,

  i have solved my problem by using linkfloat() function.After using this function i can input floating point values into structure array.I want to know the reason for using the linkfloat() function.Please tell me the reason so that i can explain it to other students.

Regards
Kavitha

Answer
Prior to this, I'd never heard of linkfloat.  Best I can tell, this is a bad hack to fix what is, in my opinion, a bug in your compiler.  I take it you are using Borland TurboC.  I've seen a lot of people using TurboC on AllExperts.  Borland TurboC is rather old and has several quirks like this.

The problem is (as I understand it), by default, TurboC does not include a floating point implementation when compiling (because, back when TurboC was written/being used, including a floating point implementation in your application could be cost prohibitive).  So, you have to actively use floating points in some manner to get it to link in a floating point implementation, and that's what the linkfloat function does.  It is a hack function that forces the compiler to include a floating point implementation.

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.