how would i use setprecision(2) on a value that i cant reference outside of a loop?
for example: ( pun intended )
1 2 3 4 5 6 7 8 9 10 11 12 13
int a = 0;
cout << "hey, the contents of sString[ x ] are : ";
for ( int x = a < sString.length(); x++ )
{
// so lets say we have a decimal in our sString[ x ]
// like 1.782345
// i cant limit the number of digits displayed here
// but i try anyway with setprecison defined in iomanip lib
cout << setprecision(2) << sString[ x ] ;
}
cout << endl; // what we want is 1.78
// insted we get the full decimal..
Your loop looks very wrong. For loops have three "parameters": an initialization, a condition, and an ending incrementation. Yours has only two. In any case, if you are couting chars out of a string (you are, using the [] operator) then setpercision does nothing. It only has any effect if you are couting an actual numerical value. What you should do if you have the numbe in a string is either parse the number out into a variable and use setpercision, or cout up until you see the decimal point then cout two more numbers.