Problem with structure with a float variable

This is a discussion on Problem with structure with a float variable within the C Programming forums, part of the General Programming Boards category; Hi, I created a structure which has 2 char arrays and 1 float variable. When trying to input data into ...

  1. #1
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10

    Problem with structure with a float variable

    Hi,

    I created a structure which has 2 char arrays and 1 float variable. When trying to input data into the float variable the program crashes. On the output screen I can see the following error.

    Code:
    scanf : floating point formats not linked
    Abnormal program termination.
    Following is the code:

    Code:
    #include<stdio.h>
    
    #define NAME_LIMIT 100
    #define TEAM_LIMIT 20
    #define NO_OF_PLAYERS 10
    
    struct cricket
    {
    	char name[NAME_LIMIT];
    	char team[TEAM_LIMIT];
    	float bat_avg;
    };
    typedef struct cricket cricket;
    
    void getS(char [], int);
    
    void main()
    {
    	cricket player[NO_OF_PLAYERS];
    	int i;
    	clrscr();
    	printf("Please enter details of %d players:-",NO_OF_PLAYERS);
    	for(i=0;i<NO_OF_PLAYERS;i++)
    	{
    		printf("\n\nPlayer %d:-",i+1);
    
    		printf("\nPlayer Name: ");
    		getS(player[i].name,NAME_LIMIT);
    
    		printf("\nTeam: ");
    		getS(player[i].team,TEAM_LIMIT);
    
    		printf("\nBatting Average:");
    		scanf("%f",&player[i].bat_avg);
    	}
    }
    
    void getS(char str[], int max)
    {
    	int i=-1;
    	do
    	{
    		str[++i] = getche();
    	}
    	while(i<max-1 && str[i] != '\n' && str[i] != '\r');
    	str[++i] = '\0';
    }
    I am using Turbo C compiler. I cannot understand what I am doing wrong. Please help me.

    Thank you,
    -Sreejit

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Aside from void main being wrong (see the FAQ, and never mind Borland's autogenerated template) and the fact that your compiler is probably 13 years old (if it's TC 3.0, as I suspect), I think that you are missing some compiler options that make using scanf with floats possible. The error message hints at that.

    Best solution would be to use a proper compiler.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    void getS(char str[], int max)
    {
    	int i=-1;
    	do
    	{
    		str[++i] = getche();
    	}
    	while(i<max-1 && str[i] != '\n' && str[i] != '\r');
    	str[++i] = '\0';
    }
    As CornedBee said that u are using a very old compiler where u to seriously think about change your compiler. well, u could use DEV-C++ with the good IDE for your use and which is not that difficult to use that. Dev-c++ connects to one of the port of gcc compiler which basically mean u are compiling as per the gcc standards.

    And in the above code you ar using a non standard function which is getche change to getchar which is standard function.

    ssharish2005

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I remember that I read somewhere that the borland compilers don't recognize that the floatingpoint support has to be linked to your program if the only place a float is used is in a scanf or printf statement.
    A workaround is to initialize a dummy float somewhere in your program.
    e.g.
    Code:
    int main() {
        float dummy = 1.0f;
        ....
    }
    Kurt

  5. #5
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10

    Thumbs up Thank you!

    Thank you so much for the replies. I am using the compiler because I have to appear for a lab test next month and we will be asked to code on Turbo C.

    Kurt, your solution of declaring and initializing a dummy variable did not help. However, I used the dummy variable to get the input. The following code works fine now:
    Code:
    scanf("%f",&dummy);
    player[i].bat_avg = dummy;
    But I still do not understand why does scanf have problems with a float variable inside a structure but not with a normal float variable.

    Thanks anyway for the suggestions. I will certainly try and get a better compiler once I am through with my lab test.

    -Sreejit

  6. #6
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10
    Aside from void main being wrong (see the FAQ, and never mind Borland's autogenerated template) and the fact that your compiler is probably 13 years old (if it's TC 3.0, as I suspect), I think that you are missing some compiler options that make using scanf with floats possible. The error message hints at that.

    Best solution would be to use a proper compiler.
    CornedBee, Thank you for information on void main. I am a beginner and did not know that we are not supposed to use void with main. http://faq.cprogramming.com/cgi-bin/...&id=1043284376 helped.

    Thanks a lot.

    -Sreejit

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    In addition to what Zuk said, I thought you had to do some dummy calculation on the float to actually get the float support linked in (not just simply declaring a float variable), by calling a floating-point function, such as sin:

    Code:
    #include <math.h>
    
    ...
    
    sin(dummy);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10
    Thanks a lot Salem and hk_mp5kpdw. Salem, I tried google but my search text was ... well... stupid I guess, after looking at your search results. Thank you for the help.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Searching for pointed results is an acquired skill.
    run time error
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  4. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 02:41 PM
  5. Replies: 1
    Last Post: 02-03-2005, 02:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21