why is it rounding up?

krazeegook
krazeegook used Ask the Experts™
on
I'm reading in float number from text file.
here is the code:

#include <vector>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

main(int argc, char *argv[])
{
  vector<float> N;

  fload i;

  while (cin >> i)
    N.push_back(i);

  //debug -- print out elements of vector
  for(int x=0;x<N.size();x++)
    cout<< N[x] << "\n ";
  cout<< "\n";
}

In the text file, the floats are up to 6 decimal digits.(ie, 4.563424, 2.532190) But when I print out the float by cout, it rounds up to 3 decimal digit and prints it out. (ie 4.564, 2.532)  
Why is it doing this? Does this mean, it's getting stored in the vector like this? or it's just a limitation of "cout"? Is there a way of getting around this(whether it's storage limition or i/o limitation)?

THanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Let me rephrase that alittle

this is the input(two float in one line):
0.000000 840.187717
4.943829 783.099223
13.928229 911.647358
16.903744 335.222756
25.586040 277.774711
32.125740 477.397052
39.414448 364.784473
45.548458 952.229725
55.710407 635.711728
63.883377 141.602555

and these are the output:

0 840.188
4.94383 783.099
13.9282 911.647
16.9037 335.223
25.586 277.775
32.1257 477.397
39.4144 364.784
45.5485 952.23
55.7104 635.712
63.8834 141.603

Commented:
Try with setting the precision

cout.precision(int)

and use double instead of float

Author

Commented:
already tried double and doesn't effect the result.
Is it problem with storage? or limitation of "cout"?
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Commented:
have you tried with

cout.precision(10) ?
Author of the Year 2009

Commented:
krazeegook,
If that solved your problem, please come back and award the points to Kimpan.  Thanks
-- Dan
krazeegook

I know it's not easy to grade a question sometimes, but I think this one should be pretty straight forward. If Kimpan's comment was what you were looking for than he deserves an 'A' grade.

Take a look at http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp to learn more about grading.

I changed the grade to 'A'.

** Mindphaser - Community Support Moderator **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial