|
-
October 8th, 2007, 01:44 PM
#1
Grabbing decimals.
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...
-
October 8th, 2007, 03:52 PM
#2
Re: Grabbing decimals.
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
-
October 8th, 2007, 05:42 PM
#3
Re: Grabbing decimals.
 Originally Posted by Kane Nathaniel
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.
Don't forget to rate my post if I was helpful!
Use code tags when posting code!
[code] "your code here" [/code]
-
October 8th, 2007, 05:43 PM
#4
Re: Grabbing decimals.
You are reading in from a file into type double? double only has a certain precision. the precision of float is even lower.
-
October 9th, 2007, 03:11 PM
#5
Re: Grabbing decimals.
67.34544532
34.32674776
99.54354045
Those are the values in the file I am trying to get.
Here is the code:
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:
Code:
#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):
Code:
**** 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.
Last edited by Kane Nathaniel; October 9th, 2007 at 03:42 PM.
-
October 9th, 2007, 04:28 PM
#6
Re: Grabbing decimals.
That's how setprecision 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
-
October 9th, 2007, 04:30 PM
#7
Re: Grabbing decimals.
Just to add, the link I posted shows an example of setprecision (but, it's the help page to setw).
Viggy
-
October 10th, 2007, 01:36 PM
#8
Re: Grabbing decimals.
Thanks for the help MrViggy.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|