CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2007
    Posts
    9

    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...

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Join Date
    Jun 2007
    Location
    United States
    Posts
    275

    Re: Grabbing decimals.

    Quote 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]

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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.

  5. #5
    Join Date
    Oct 2007
    Posts
    9

    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.

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    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

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Grabbing decimals.

    Just to add, the link I posted shows an example of setprecision (but, it's the help page to setw).

    Viggy

  8. #8
    Join Date
    Oct 2007
    Posts
    9

    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
  •  





Click Here to Expand Forum to Full Width

Featured