tags:

views:

257

answers:

5

I have this Code:

cout << "Your Value is: ";

for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {

              if ( sBuffer[ x ] == ',' )
              {    
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
// setprecision doesnt work right when used with [] but i try anyway..

       cout << setprecision(2) << sBuffer[ x ];

And my sBuffer[ x ] contains a double value, say, 4.1415679

I want to use setprecision( 2 ) to display only 4.14

Im stuck, what can i do?

A: 

You probably want std::fixed:

cout << fixed << setprecision(2) << sBuffer[ x ];
//      ^^^^^
strager
A: 

If sBuffer is an array of chars, then it doesn't contain a double.

Eclipse
+2  A: 

It looks like sBuffer is a string or an array of chars, so when you print sBuffer[x], it's treating it as a character instead of a floating point number.

You need to convert the string representation of the number into a double. You can use the atof or strtod functions from C, or boost::lexical_cast<double>.

Matthew Crumley
+2  A: 

It doesn't look like your sBuffer[x] would contain a double, you compare it against ',' after all. So sBuffer is a buffer of characters? Then sBuffer[x] is just one character of your "4.1415679" and setprecision won't do what you want if you just output this character.

You can use stringstreams to read a double from a string:

#include <sstream>

istringstream strm("4.1415679");
double d;

if (strm >> d) {
  cout << "Your number: " << setprecision(2) << d << endl;
}
else {
  cout << "Not a number." << endl;
}

If you have the boost libraries installed (always a good idea), you can also use boost::lexical_cast<double>("..."), like Matthew Crumley said.

sth
A: 

it doesn't contain a double because i'm reading a text file, which i forgot to mention sorry.

my text file has this format:

string, some string, 49.59494, 29.4094944

storing each Field before a comma into the sBuffer[ x ].

so i in fact do have a double. The reason that i think its not working is that the complier is interpreting it as a string, or char, and not a double value.

right?

Heres my full code feel free to compile in dev or something but be sure to use the text file with this format:

String,Some String,54.2345,34.6654 ...

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>
#include <cstdlib>



using namespace std;


int main()
{
    string usrFileStr,
    fileStr = "test.txt",  // declaring an obj string literal
    sLine,                        // declaring a string obj
    sBuffer;

    int lineCount = 1;

    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() )
    {
          int nStart = -1 ;
          cout << "First String " << lineCount << " (";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {
              if ( sBuffer[ x ] == ',' )
              {
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
          cout << ") ";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {

              if ( sBuffer[ x ] == ',' )
              {    
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
          cout << " (First dValue";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {

              if ( sBuffer[ x ] == ',' )
              {
                   nStart = x;
                   break;
                   }
              cout << setprecision(2) << sBuffer[ x ];
          }
          cout << ", Second dValue: ";
          for ( int x = nStart + 1; x < sBuffer.length(); x++ )
          {
              if ( sBuffer[ x ] == ',' )
              {
                   nStart = x;
                   break;
                   }
              cout << sBuffer[ x ];
          }
          cout << ") \n";
          lineCount++;
    }


    cout << "There are a Total of: " <<  lineCount << " 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 we close it

    fgetc( stdin );
    return 0;
}

// use a funnction

Yeah i know i could use a function prototype for the excessive looping(4) But i'd like to get this value issue taken care of first.

sth's answer (http://stackoverflow.com/questions/467649/parse-contents-of-array-c#467759) is probably what you want, then.
strager