2

I writing with borland C++ compiler. I want to read numbers with 5 decimal places from text file then save it into another text file.

I wrote my program as below:

main
{
double Bid[25], Be1[25],Bn1[25];
int i,no=10,j,p;
char filename[30];

    ifstream fp1;
    fp1.open("bufile2.txt");
    cout<<"\n id  lat long";
    for (i=1;i<=no;i++)
        {
        fp1>>Bid[i]>>Be1[i]>>Bn1[i];
        cout<<"\n "<<i<<"  "<<Bid[i]<<"  "<<Be1[i]<<"   "<<Bn1[i];
        }
    fp1.close();

    getch();
    return 0;
    }

my input file is as follow:

id lat long 
1   101.28411   2.91245
2   101.28211   2.91345
3   101.28301   2.91345
4   101.28401   2.91345
5   101.28501   2.91345
6   101.28001   2.91445
7   101.28101   2.91445
8   101.28201   2.91445
9   101.28301   2.91445
10  101.28401   2.91445

my problem is, the Be1[i]... not outputting 5 decimal number in my stored file "result.txt". It only give me 3 decimal places. The result is as followed:

id lat long
1   101.284 2.91245
2   101.282 2.91345
3   101.283 2.91345
4   101.284 2.91345
5   101.285 2.91345
6   101.280 2.91445
7   101.281 2.91445
8   101.282 2.91445
9   101.283 2.91445
10  101.284 2.91445

Help me please

Abelisto
  • 14,826
  • 2
  • 33
  • 41
  • please format your code so it is better readable without scrolling as much to the left (about 70 chars per line should be fine). Sort the formatting as well, you keep switching different styles of coding (f.e. linebreak after { or not, indentation inside {} or not - this makes it hard to read. Every "good" IDE has tools to format consistently. – Patrick Artner Nov 26 '17 at 14:35
  • 1
    I suggest you start from the simplest program that does some part of your assignment (just read and write one single number), and get it to work first like you want. Then once that's done, build upon it. – M. Prokhorov Nov 26 '17 at 14:43
  • Dear M.Prokhorov, re-phase my code to. could please advise the reason of not getting result in 5 decimal. – eddy junaidy Nov 26 '17 at 15:20
  • Not sure about Borland C++, but it works with GCC. Add `cout.precision(8);` as the first line of your function. – Abelisto Nov 26 '17 at 15:50
  • 1
    Duplicated BTW: [How do I print a double value with full precision using cout?](https://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout) – Abelisto Nov 26 '17 at 15:55
  • Abelisto, it does't work with Borland C++ – eddy junaidy Nov 26 '17 at 16:10
  • https://stackoverflow.com/a/35097437/593144 – Abelisto Nov 27 '17 at 10:38
  • 1
    @eddyjunaidy: `cin.precision()` (or `std::setprecision()`) is the correct way to handle this, so *which* Borland C++ compiler are you using exactly that this does not work in? – Remy Lebeau Nov 29 '17 at 00:02
  • @Remy Lebeau it work with stackoverflow.com/a/35097437/593144. TQ Abelisto – eddy junaidy Dec 05 '17 at 17:14

0 Answers0