Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 122577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:09:03+00:00 2026-05-11T04:09:03+00:00

I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have

  • 0

I am reading a text file with this format:

grrr,some text,45.4321,54.22134

I just have my double valued stored in a string variable.

Why is it only giving me the first digit of the string?

If I start over with just one while loop and a text file of this new format:

21.34564

it works as it should.

The thing is, sLine has the the same value as the one when I started over. What is different is the three nested for loops that most likely is causing the problem.

Here is the code that gets me what I want:

#include <iostream> #include <fstream> #include <string> #include <vector> #include <iomanip> #include <cstdlib> #include <sstream>  using namespace std;  int main() {     string usrFileStr,     fileStr = 'test.txt',  // declaring an obj string literal     sBuffer,     sLine,     str;      double dValue ;      int lineCount = 1;     int nStart;     istringstream issm;      fstream inFile;                  // declaring a fstream obj     // cout is the name of the output stream     cout << 'Enter a file: ';     cin >> usrFileStr;      inFile.open( usrFileStr.c_str(), ios::in );      // at this point the file is open and we may parse the contents of it       while ( getline ( inFile, sBuffer ) && inFile.eof() )     {           cout << 'Original String From File: ' << sBuffer << endl;            cout << 'Modified Str from File: ' << fixed << setprecision(2)            << dValue << endl;     }      fgetc( stdin );     return 0; }       

So there it works just like it should. But i cant get it to work inside a for loop or when i have multiple feilds in my text file…

With this code, why is it taken off the decimal?  #include <iostream> #include <fstream> #include <string> #include <vector> #include <iomanip> #include <cstdlib> #include <sstream> #include <errno.h>  using namespace std;  int main() {     string usrFileStr,     myFileStr = 'myFile.txt',  // declaring an obj string literal     sBuffer,      sLine = '';       istringstream inStream;      int lineCount = 1;     int nStart;     double dValue = 0,     dValue2 = 0;     float fvalue;      fstream inFile;                  // declaring a fstream obj     // cout is the name of the output stream     cout << 'Enter a file: ';     cin >> usrFileStr;       inFile.open( usrFileStr.c_str(), ios::in );      // at this point the file is open and we may parse the contents of it      if ( !inFile )     {          cout << 'Not Correct ' << endl;     }       while ( getline ( inFile, sBuffer ) )     {           nStart = -1 ;            for ( int x = nStart + 1; x < sBuffer.length(); x++ )           {               if ( sBuffer[ x ] == ',' )               {                    nStart = x;                    break;               }                cout << sBuffer[ x ];           }            for ( int x = nStart + 1; x < sBuffer.length(); x++ )           {                if ( sBuffer[ x ] == ',' )               {                        nStart = x;                    break;               }                cout << sBuffer[ x ];           }             for ( int x = nStart + 1; x < sBuffer.length(); x++ )           {                   if ( sBuffer[ x ] == ',' )               {                    nStart = x;                    break;               }                sLine = sBuffer[ x ];               inStream.clear();               inStream.str( sLine );                if ( inStream >> dValue )               cout << setprecision(1) << dValue;            }             for ( int x = nStart + 1; x < sBuffer.length(); x++ )           {               if ( sBuffer[ x ] == ',' )               {                    nStart = x;                    break;               }                sLine = sBuffer[ x ];                    inStream.clear();               inStream.str( sLine );                if ( inStream >> dValue2 )                   cout << setprecision(1) << dValue2;               }            cout << ') \n';           lineCount++;     }       cout << 'There are a Total of: ' << lineCount -1 << ' line(s) in the file.'     << endl;      inFile.clear();           // clear the file of any errors     inFile.close();  // at this point we are done with the file and may close it      fgetc( stdin );     return 0; } 

I don’t have any other characters to loop over in the first code because im just reading a nice little double value.

In my second code, i have many characters to get to before the one that i want. But regardless, it is still isolated from the other characters and it is still in its own varaible. im to sick to realize what the problem is :/ although i think its the for loops.

I have also tried atof but i get a ‘0’ where the decimal should be. and strtod is hard because i need im not reading data into a const char *cPtr

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:09:03+00:00Added an answer on May 11, 2026 at 4:09 am

    Using instream>>dvalue is certainly the right way to do things. But sometimes what’s right isn’t always easiest or necessarily best.

    We could do something like this:

    int main() {   string s = 'grrr,some text,45.4321,54.22134';   double a,b;    ASSERT_IS( 2, sscanf( s.c_str(), '%*[^,],%*[^,],%lf,%lf', & a, & b ) );    cout << setprecision(8);   SHOW(a);   SHOW(b); } 

    Or perhaps something like this, while less efficient, might be easier to understand…

    int main() {   string s = 'grrr,some text,45.4321,54.22134';   vector<string> v;    StringSplit( & v, s, ',' );    cout << setprecision(8);   SHOW(v);   SHOW(atof(  v[2].c_str()));   SHOW(strtod(v[3].c_str(), (char**)NULL)); } 

    Assuming:

    #define SHOW(X)  cout << # X ' = ' << (X) f << endl       /* A quick & easy way to print out vectors... */ template<class TYPE> inline ostream & operator<< ( ostream & theOstream,                               const vector<TYPE> & theVector ) {   theOstream << 'Vector [' << theVector.size() << '] {'              << (void*)(& theVector) << '}:' << endl;    for ( size_t i = 0;  i < theVector.size();  i ++ )     theOstream << '    [' << i << ']:   \'' << theVector[i] << '\'' << endl;    return theOstream; }   inline void StringSplit( vector<string> * theStringVector,  /* Altered/returned value */              const  string  & theString,              const  string  & theDelimiter ) {   UASSERT( theStringVector, !=, (vector<string> *) NULL );   UASSERT( theDelimiter.size(), >, 0 );    size_t  start = 0, end = 0;    while ( end != string::npos )   {     end = theString.find( theDelimiter, start );        // If at end, use length=maxLength.  Else use length=end-start.     theStringVector -> push_back( theString.substr( start,                    (end == string::npos) ? string::npos : end - start ) );        // If at end, use start=maxSize.  Else use start=end+delimiter.     start = (   ( end > (string::npos - theDelimiter.size()) )               ?  string::npos  :  end + theDelimiter.size()     );   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer This set of Visio stencils and templates for UML 2.0… May 11, 2026 at 11:26 am
  • added an answer With bash's extglob shopt setting enabled, you can exclude files… May 11, 2026 at 11:26 am
  • added an answer The text is a child node of the MyEle element,… May 11, 2026 at 11:25 am

Related Questions

I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I am reading a binary file into a parsing program. I will need to
I am reading a .NET book, and in one of the code examples there
I am reading a binary log file produced by a piece of equipment. I
I am reading a tutorial that uses the following example (that I'll generalize somewhat):
I am building an MVC application in which I am reading a list of
I am reading an XML file into a DataSet and need to get the
I am reading in numbers from a file and then trying to add them
I am reading over the K&R book, and am a little stuck. What is
In this tutorial I am reading , Dave Ward creates a page that shows

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.