Help Needed: How to set precision of an input

Status
Not open for further replies.

nsw1216

Member level 3
Joined
May 7, 2010
Messages
60
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,667
Hi all,

I would like ask that how can i set the precision of the input when reading from file?

For example, data from my input file are
HTML:
-9.0000000e+000 -6.1000000e+001  3.7000000e+001  4.0000000e+000  1.1000000e+000  7.2300000e-012
 -8.0000000e+000 -6.1000000e+001  3.7000000e+001  4.0000000e+000  1.1000000e+000  7.2300000e-012

My desired output are
HTML:
-9.00 -61.00  37.00  4.00  1.10  7.23e-012
 -8.00 -61.00  37.00  4.00  1.10  7.23e-012

However, i tried the code below
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

const int fac = 8485;
const int shift = 6;
double factory[fac][shift];
int z;

int main()
{
ifstream inFile("Testing.txt",ios::in);
inFile.precision(2);
inFile.setf(ios::fixed, ios::showpoint);

while (!inFile.eof())
	{
		for(int a=0; a<fac; a++) 
		{
			for(int b=0; b<shift; b++)  
			{
				inFile>>factory[a][b];
			}
		}
		for(int a=0; a<fac; a++) 
		{
			for(int b=0; b<shift; b++)  
			{
				cout<<factory[a][b]<<" ";
			}
			cout<<endl;
		}
	}

inFile.close();
return 0;
}

I got the output as below
HTML:
-9 -61 37 4  1.1  7.23e-012
 -8 -61  37  4  1.1  7.23e-012

May I know what is to codes or ways to get my desired output from reading data from a file ?

Your reply are most welcomed and appreciated.
Thank you in advance.

Best Regards.
 

try to make inFile.precision(2); more i.e. inFile.precision(4);


Good Luck

Thanks for your reply, milind.a.kulkarni.
I tried the method you mentioned above yet I still cannot get my desired output.

May i know what is going wrong with my coding above ?

Best Regards.
 

As per my understanding the inFile function in C++ reads the contents in files as characters and then mostly triming out the zero after the decimal points that what I fell....can you do one thing just chenge -9.11 inspite of -9.00 in your file and try to see that you are getting the same output or not.... bcz -9.000 and -9 are equal....also 1.1 is getting read as 1.1

As per your code is concern at least i don't see any problem in coding....only the doubt will be realted to this inFile function only....

Good Luck
 


Thanks for the clarification. I will try it out.

---------- Post added at 07:31 ---------- Previous post was at 06:57 ----------

milind.a.kulkarni.

Do you know how to avoid the program to trim out the zeros after decimals because my data read from file and require 2 decimal points for the following process ?

I tried to changed all zeros to ones but i noticed that my output are some 2 decimal points and some are one decimal points only. May i know how to fix this problem?
The output i got are as below:
-9.11 -61.1 37.1 4.11 1.11 7.23e-012

Thanks.

Best Regards.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…