?
Solved

How do I return to the default settings for cout once I have set a precision?

Posted on 2003-02-24
3
Medium Priority
?
193 Views
Last Modified: 2010-04-01
In a program I set the precision of the cout function to n where n is an integer value. However, later I would like to output values with the default cout settings, i.e. I want to undo what I did to the precision. I'm not sure how to do this and would appreciate any help. A copy of the code I'm using is below.

void Newton(Poly f)
{
  Poly g = f;
  int maxIter = 100;
  int i, precision;
  double guess, previous, guessRound, previousRound;

  // Prompt user to enter an initial guess

  cout << "Please enter your initial guess: ";
  cin >> guess;

  // Prompt user to enter the desired precision

  cout << "Enter your desired precision (up to 20 decimal places): ";
  cin >> precision;

  if(precision < 0 || precision > 20)
    cout << "\nI'm sorry, you have entered an incorrect precision.";

  else if(f.eval(guess) == 0)
    {
      cout << "\nYour guess, " << guess << ", is a root to the equation." << endl;
    }

  else
    {
      // Take derivative of original function

      f.derivative();

      // Set the output precision to user's specification

      cout.setf(ios::fixed, ios::floatfield);
      cout.setf(ios::showpoint);
      cout.precision(precision);

      previous = guess;

      // Use a for loop to perform several iterations of Newton's method.
      // Check for precision and terminate loop when it is reached.

      for(i=1; i<=maxIter; i++)
        {
          guess = previous - (g.eval(previous)/f.eval(previous));

          guessRound = round(guess, precision);
          previousRound = round(previous, precision);

          if(guessRound == previousRound)
            break;
          else
            previous = guess;
        }

      if(guessRound == previousRound)
        cout << "\nAfter " << i << " iterations, the root was found to be " << guessRound << "." << endl;
      else
        cout << "\nI'm sorry, after 100 iterations a root was not found." << endl;
}

Thanks to anyone who can help.
0
Comment
Question by:ViperX883
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Expert Comment

by:Kimpan
ID: 8015337
why don't you save the old precision before you set the new one?
0
 

Author Comment

by:ViperX883
ID: 8017295
I read somewhere that I would have to save the precision, but the problem is I have absolutely no idea how to do that. I have tried several times to develop classes and functions, but to no avail.

Thanks again for all the help.
0
 
LVL 5

Accepted Solution

by:
Kimpan earned 225 total points
ID: 8030960
you either use


int precision(int newvalue) // returns the old value

or

int precision() const // returns the current value
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question