Click to See Complete Forum and Search --> : Grabbing decimals.
Kane Nathaniel
October 8th, 2007, 01:44 PM
Sorry to bother you guys.
I'm a newbie to C++, only been programming for a month, but I need help grabbing up to 8 decimal places from a file.
I've gotten it to grab 4 decimal places.
Do I have to use a certain constant?
Any help would be appreciated.
I'm sure it is easier than I think...
MrViggy
October 8th, 2007, 03:52 PM
I don't understand what you mean by "grab 4 decimal places." The float and double data types have specific value ranges, there is no "grabbing" less or more decimal places.
Viggy
bovinedragon
October 8th, 2007, 05:42 PM
Sorry to bother you guys.
I'm a newbie to C++, only been programming for a month, but I need help grabbing up to 8 decimal places from a file.
I've gotten it to grab 4 decimal places.
Do I have to use a certain constant?
Any help would be appreciated.
I'm sure it is easier than I think...
This sounds more like a file io problem. Please post your code so we can show you where this problem is coming from.
NMTop40
October 8th, 2007, 05:43 PM
You are reading in from a file into type double? double only has a certain precision. the precision of float is even lower.
Kane Nathaniel
October 9th, 2007, 03:11 PM
67.34544532
34.32674776
99.54354045
Those are the values in the file I am trying to get.
Here is the code:
double value1, value2, value3;
fstream myFile; // Entitling use of file.
myFile.open("input.dat"); // Opening file.
myFile >> value1;
myFile >> value2;
myFile >> value3;
When I display the results it shows me :
67.3454
24.3267
99.5435
That is what I mean by 4 decimals.
I need to grab the whole value.
Thanks for any help.
Sorry for the confusion.
------------------------------------------------------------------------------------------------
Another Question:
Okay, so just going with the 4 decimal places, this is my program:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
double value1, value2, value3;
fstream myFile; // Entitling use of file.
myFile.open("input.dat"); // Opening file.
myFile >> value1;
myFile >> value2;
myFile >> value3;
if(!(value1 < 100))
{
cout << "**** Invalid numerical values. Must be lower than 100. ****";
return 1;
}
else if(!(value2 < 100))
{
cout << "**** Invalid numerical values. Must be lower than 100. ****";
return 1;
}
else if(!(value3 < 100))
{
cout << "**** Invalid numerical values. Must be lower than 100. ****";
return 1;
}
else if((value1 < 100)&&(value2 < 100)&&(value3 < 100))
cout << "**** Values entered are valid. ****";
cout << endl;
cout << "-----------------------------------" << endl;
cout << setw(35) << "****** [Order Value Entered] ******" << endl;
cout << value1 << " , " << value2 << " , " << value3 << endl;
cout << setw(35) << "******* [Least to Greatest] *******" << endl;
if((value1 > value2)&&(value2 > value3))
cout << value3 << " , " << value2 << " , " << value1;
else if((value1 > value3)&&(value3 > value2))
cout << value2 << " , " << value3 << " , " << value1;
else if((value2 > value3)&&(value3 > value1))
cout << value1 << " , " << value3 << " , " << value2;
else if((value2 > value1)&&(value1 > value3))
cout << value3 << " , " << value1 << " , " << value2;
else if((value3 > value1)&&(value1 > value2))
cout << value2 << " , " << value1 << " , " << value3;
else if((value3 > value2)&&(value2 > value1))
cout << value1 << " , " << value2 << " , " << value3;
else if((value1==value2)&&(value2==value3))
cout << "**** All values are the same... ****";
else if((value1==value2)||(value1==value3)||(value2==value3))
cout << "**** Two of the values are the same. Please change them. ****";
cout << endl;
cout << endl;
cout << showpoint;
cout << "--------------------------------------------------" << endl;
cout << "Value Read" << setw(20) << "Rounded Amount" << setw(20) << "Banker's Amount" << endl;
cout << value1 << setprecision(4) << setw(20) << endl;
cout << value2 << setprecision(4) << setw(20) << endl;
cout << value3 << setprecision(4) << setw(20) << endl;
return 0;
}
Okay, when I run it, it outputs this (using the values above):
**** Values entered are valid. ****
-----------------------------------
****** [Order Value Entered] ******
67.3454 , 34.3267 , 99.5435
******* [Least to Greatest] *******
34.3267 , 67.3454 , 99.5435
--------------------------------------------------
Value Read Rounded Amount Banker's Amount
67.3454
34.33
99.54
Notice at the bottom:
34.3267 was rounded ----> 34.33
99.5435 was rounded ----> 99.54
Why were they rounded?
I didn't put round(value2) in my code anywhere.
Sorry about the super long post...
~Kane.
MrViggy
October 9th, 2007, 04:28 PM
That's how setprecision (http://msdn2.microsoft.com/en-us/library/8bbhbaew(vs.80).aspx) works. You only get that number of "precise" digits (you set it to 4). Also, the modifier works for everything after it in the stream, which is why your first value is the whole value.
Offhand, I'm not sure what the default precision of the output stream is.
Viggy
MrViggy
October 9th, 2007, 04:30 PM
Just to add, the link I posted shows an example of setprecision (but, it's the help page to setw).
Viggy
Kane Nathaniel
October 10th, 2007, 01:36 PM
Thanks for the help MrViggy.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.