LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2007, 04:25 PM   #1
fs11
Member
 
Registered: Aug 2006
Posts: 79

Rep: Reputation: 15
problem in string to float conversion..C++


Hello All,

I am using the following code to read a string from a file and converting it to float for later comparision and stuff..

Code:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int main () {
  string line;
  float x;
  const char* s;



  ifstream myfile ("valseq");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);

      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file";

/*converting string to floating point value*/
  x=atof(line.c_str());
  cout << x;




  return 0;


}


The output that i get is

Code:
0.125933

0

i.e. 0.125933 for the first cout(which mst be the same for the second cout as well because the file contains this value.)and 0 for the second cout...

what am i doing wrong..Please HELP....

thanks
 
Old 01-15-2007, 05:46 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
I haven't tested this, but I believe the reason is because the last pass through the loop picks up a newline at the end of the file and stores that into variable line. Then you try to parse a float out of that newline and get zero as a result. That's why there's an extra newline in your output, as well.
 
Old 01-15-2007, 06:06 PM   #3
fs11
Member
 
Registered: Aug 2006
Posts: 79

Original Poster
Rep: Reputation: 15
Thanks but this doesnt seem to be the case...
Please Help!!!
 
Old 01-15-2007, 06:28 PM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Hmm, strange. On my system that's what's wrong with it. Just add "cout << atof(line.c_str()) << endl" after where you output the line. When I do that I see that the translation from string to float is correct (the float is output twice), then I again get the newline and the zero.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
transform a string to a float spinal2 Programming 10 10-30-2006 12:09 PM
IBM Real format conversion to IEEE float ppanyam Programming 0 12-15-2005 11:10 PM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
c++: float to double conversion ashirazi Programming 6 11-30-2004 04:14 PM
Float/Double to String? Mega Man X Programming 16 01-03-2004 07:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration