Joker Joker - 6 months ago 51
C++ Question

Using setprecision in C++

I am trying to use setprecision in c++. Here is what I am doing-

I have an output file, where I am writing the results-

void function(some parameters)
{
outfile << "Annual Interest Rate: " << interest_rate << endl;
while(some condition)
{

// some code
//
//
outfile << left << setw(10) << count << fixed << setprecision(2) << setw(10) << monthly
<< fixed << setprecision(2) << setw(10) << interest
<< fixed << setprecision(2) << setw(10) << principal
<< fixed << setprecision(2) << setw(10) << balance << endl;
}
}


My first call to function prints the interest_rate correctly.
Say, I call the function with
interest_rate=0.035
, so I get the following output -

Annual Interest Rate: 0.035


But next time if I call with the function with same
interest_rate i.e. 0.035
, I get the following output-

Annual Interest Rate: 0.04


I want the same output in both the cases, why is this happening?

Edit:-
here is the code-

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

#include <math.h>
using namespace std;

// Function Prototypes for our functions
void perform_calculations(double, double, double);


ofstream outfile;
int main()
{

string infile_name, outfile_name;
int count_of_items_in_file;

ifstream infile;


// Prompt the user for the input file.
cout << "Please enter the input file name: "; // prompt user
cin >> infile_name; // grab input
cout << "The user entered file name: " << infile_name << endl; // debug to make sure working

// Prompt the user for the output file.
cout << "Please enter the output file name: "; // prompt user
cin >> outfile_name; // grab input
cout << "User entered output file name as: " << outfile_name << endl; // debug


infile.open(infile_name.c_str());



outfile.open(outfile_name.c_str());

infile >> count_of_items_in_file; // Grab the first value from file


// Loop through and read values and calucate via a function.
for (int i=0; i<count_of_items_in_file; i++)
{
double input1, input3;
double input2;

infile >> input1; // production code should check for success
infile >> input2; // production code should check for success
infile >> input3; // production code should check for success

perform_calculations(input1, input2, input3);
}


return 0;
}




void perform_calculations(double val1, double interest_rate, double val3)
{



double balance=val1;
int count=1;


outfile << "Annual Interest Rate: " << interest_rate << endl;



while(count!=5)
{


outfile << left << setw(10) << count << fixed << setprecision(2) << setw(10) << balance << endl;

count++;

}


return;
}


Here is the input file:-
input.txt

3
1000.00
0.035
120.00
5000.00
0.04
165.00
200000.00
0.055
2250.00


Here is the output file I get:

Annual Interest Rate: 0.035
1 1000.00
2 1000.00
3 1000.00
4 1000.00
Annual Interest Rate: 0.04
1 5000.00
2 5000.00
3 5000.00
4 5000.00
Annual Interest Rate: 0.06
1 200000.00
2 200000.00
3 200000.00
4 200000.00


Note how the 0.055 in the input file gets printed as 0.06 in the output file

Answer

Some stream manipulators/flags apply only once (e.g.: setw) and other remain valid until they are changed (e.g.: fixed and setprecision).

I assume, in the first call of the function you are using the default format (not fixed and not scientific) and default precision (6) of the stream.

In the second call of the function (I assume you do not change it meanwhile in another function) you have the format fixed and a precision of two.