reading float from file

This is a discussion on reading float from file within the C++ Programming forums, part of the General Programming Boards category; Trouble reading float variables. I tried reading some float variables from a file. This is what happened. Input.(input file). Code: ...

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Question reading float from file

    Trouble reading float variables.
    I tried reading some float variables from a file. This is what happened.
    Input.(input file).
    Code:
    Hydrogen	H	G	1	1.008
    Helium		He	G	2	4.0
    Lithium		Li	S	3	6.94
    Beryllium	Be	S	4	9.01
    Boron		B	S	5	10.8
    Carbon		C	S	6	12.0
    Nitrogen	N	G	7	14.0
    Oxygen		O	G	8	16.0
    Fluorine	F	G	9	19.0
    Neon		Ne	G	10	20.2
    Sodium		Na	S	11	23.0
    Magnesium	Mg	S	12	24.30
    Aluminium	Al	S	13	27.0
    But output.(both screen and output file).
    Code:
           Hydrogen		H	1	1.008	G
             Helium		He	2	4	G
            Lithium		Li	3	6.94	S
          Beryllium		Be	4	9.01	S
              Boron		B	5	10.8	S	//works ok
             Carbon		C	6	12	S
           Nitrogen		N	7	14	G
             Oxygen		O	8	16	G
           Fluorine		F	9	19	G
               Neon		Ne	10	20.200001	G //decimals.
             Sodium		Na	11	23	S
          Magnesium		Mg	12	24.299999	S //decimals
          Aluminium		Al	13	27	S
    My doubt...
    Is there any way to avoid this.?
    I even tried editing the file as:
    Code:
    Neon		Ne	G	10	20.20
    and
    Neon		Ne	G	10	20.20000000000
    here's the part reading the file...
    Code:
    Element table[20]; class with details of an element...
    int n=0;
    char na[25], sy[3], st='\0';	//name symbol and state of element...
    unsigned int z=0;
    float m=0.0;
    while(!file.eof()){
    	file>>na>>sy>>st>>z>>m;
    		if(file.good()){
    		table[n].set(na, sy, st, z, m);	//tested - OK
    		n++;
    		}
    	}
    cout<<"There are currently "<<n<<" entries in the input file...\n\n";

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try using setprecision() from <iomanip>

    or you could mulitply by 100, truncate to an int, and divide by 100 (but that still leaves the chance of that happening)

    or you could try using doubles.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >float m=0.0;
    Doubles will give you more precise values:
    double m=0.0;

    But floating point representation can't store every number exactly, since there's an infinite number of values to represent.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    a side note:

    my guess is your getting some werid output
    at the end of the file to, or dealing with it in your
    table, its comming from using .eof()

  5. #5
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    >float m=0.0;
    Doubles will give you more precise values:
    double m=0.0;
    Thanks 'swoopy'...
    Double worked...
    I didn't know that 'float' variables had such a problem.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Read my link in this thread - exact conversion of floating point to string
    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.

  7. #7
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Thanks for the document.
    but It'll take years for me to read through it.(literally!).

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Yeah, but you should soon learn to not trust almost everything about floating point as being absolute truth.
    Always understand the approximate nature of them and program accordingly.
    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
    Join Date
    Nov 2002
    Posts
    491
    while(!eof) is incorrrect since eof is signaled after the final read. The correct idiom is while(read_successful).

  10. #10
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    nothinggg

    while(read_successful).
    I Never read that anywhere before?
    do you mean 'read_successful'is,
    while(cin.get(character))?
    Last edited by arjunajay; 07-30-2005 at 09:42 AM. Reason: nothinggg

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yes.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Replies: 8
    Last Post: 03-26-2007, 06:48 PM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 06:13 PM
  4. Display list not displaying?
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 09-19-2004, 07:47 PM
  5. File input question
    By Beast() in forum C Programming
    Replies: 16
    Last Post: 07-09-2004, 04:23 PM

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