# Changing the number of decimal places displayed by the cout function

 acevan s · December 19, 2014 Hi guys. Just needed a little help understanding something. I wrote a little program that should calculate a root of a number iteratively and should stop when the xnew and xold values differ by less than the tolerance value. However irrespective of the tolerance value the output always has a fixed resolution of 5 decimal places. I dont understand why. Maybe someone can point out my mistake. Thanks.``#include#includeusing namespace std;int main (){float t,x,xnew,xold,temp,diff;int count=0;cout<<"Enter the number"<> x;cout<< "Enter the guess value"<>xold;cout<<"Enter the tolerance"<>t;do{    temp=xold;    xnew=0.5*(xold + x/xold);    xold=xnew;    count=count+1;    diff=abs(xnew-temp);}while(diff>t);cout<< xnew<

## Replies

 acevan s · December 25, 2014 Thanks a lot Stan. That did the trick Stan Botha · December 22, 2014 The default precision in C++ is set at 6 (total number of digits excluding the sign (+ or -) and is used to decide whether to display the output in scientific notation or not. Try the following lines of code before your last cout statements:cout.setf(ios::fixed);cout.precision(8); (or any other integer inside the parantheses) Not Shifty · December 21, 2014 If you look at the limits, it will tell you that floats can take up to 38 zeros(?) and doubles can go up to 308 zeros (?).  10 zeros is barely stretching the boundaries.... acevan s · December 21, 2014 Also i tried the fixed output thing in a integer factorial program. It gave a really big space for the digits before the decimal but still showed 6 decimal places with zeroes. eg 89856216184984651654986523.000000 acevan s · December 21, 2014 Checked that option already. No difference at all. acevan s · December 19, 2014 Thanks for your reply. I tried both of your methods(copy and paste (: because i am just a beginner) but they give answers only upto 6 decimal places. I want an answer like you see it on a calcuators display.. you know. I am attaching a pic of the output with your solution. Unfortunately there is some bug which is preventing me from uploading a pic. so i will just write it down :/Enter the number15Enter the guess value 5Enter the tolerance0.000000000013.8729835Process returned 0
• 1

## C++

122,846 followers