Parse contents of array C++
<p>I have this Code:</p>

<pre><code>cout &lt;&lt; "Your Value is: ";

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

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

       cout &lt;&lt; setprecision(2) &lt;&lt; sBuffer[ x ];
</code></pre>

<p>And my sBuffer[ x ] contains a double value, say, 4.1415679</p>

<p>I want to use setprecision( 2 ) to display only 4.14</p>

<p>Im stuck, what can i do?</p>

<hr>

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

<p>my text file has this format:</p>

<p>string, some string, 49.59494, 29.4094944</p>

<p>storing each Field before a comma into the sBuffer[ x ].</p>

<p>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.</p>

<p>right?</p>

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

<p>String,Some String,54.2345,34.6654
...</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;iomanip&gt;
#include &lt;cstdlib&gt;



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 &lt;&lt; "Enter a file: ";
    cin &gt;&gt; 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 ) &amp;&amp; !inFile.eof() )
    {
          int nStart = -1 ;
          cout &lt;&lt; "First String " &lt;&lt; lineCount &lt;&lt; " (";
          for ( int x = nStart + 1; x &lt; sBuffer.length(); x++ )
          {
              if ( sBuffer[ x ] == ',' )
              {
                   nStart = x;
                   break;
                   }
              cout &lt;&lt; sBuffer[ x ];
          }
          cout &lt;&lt; ") ";
          for ( int x = nStart + 1; x &lt; sBuffer.length(); x++ )
          {

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

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


    cout &lt;&lt; "There are a Total of: " &lt;&lt;  lineCount &lt;&lt; " line(s) in the file."
    &lt;&lt; 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
</code></pre>

<p>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.</p>
