fstream printing new line

This program is designed to calculate your bodyfat percentage using a method used by the navy. I want to enter the info, have it print the results to a file. When I reopen the program and re enter new info, for it to append on a NEW LINE, not the SAME LINE.
It does this "25.8 25.8. 25.8" (one line)
I want it like this:
25.8
25.8
25.8
1
2
3
4
5
6
7
8
9
10
11
12
13
    
    // multiply measurements by 2.54 to convert inches to centimeters
    bodyfat = 495 / ( 1.0324 - .19077 * ( log10( ( waist * 2.54 ) - ( neck * 2.54 ) ) ) + .15456
                   * ( log10( height * 2.54 ) ) ) - 450;
   
    ofstream fout;
    fout.open ("bodyfat.txt", ios::out | ios::app | ios::binary);
    
    cout << setprecision(2) << fixed << bodyfat << endl;
    fout << endl; // this doesn't work for a new line
    fout << setprecision(2) << fixed << bodyfat << endl;
    fout.close();
    cin.ignore();
Last edited on
Since endl is failing for you, I am thinking you are using Windows and your compiler has a version of endl that throws only a LF character into the file, but in Windows you need CRLF.

If I am right, change your endl to:

fout << char(13) << endl;
Thanks for the reply, but that didn't work.
Okay you where partially right. I fixed it, and this is the working code.

1
2
3
4
5
6
    ofstream fout;

    fout.open ("bodyfat.txt", ios::out | ios::app | ios::binary);
  
    cout << setprecision(2) << fixed << bodyfat << endl;
    fout << setprecision(2) << fixed << bodyfat << char(13) << char(10);
closed account (DSLq5Di1)
Alternatively.. << "\r\n";
that would be better. its more clear
If you're writing a text file, you shouldn't be using the binary flag.

fout.open ("bodyfat.txt", ios::out | ios::app | ios::binary);

Remove the ios::binary. In text mode iostream will corectly map endl to \r\n for you.
That works too. I will paste the updated code incase someone in the future runs in to this issue as well.

1
2
3
4
5
   fout.open ( "bodyfat.txt", ios::out | ios::app );
  
    cout << setprecision(2) << fixed << bodyfat << endl;
    fout << setprecision(2) << fixed << bodyfat << endl;
Topic archived. No new replies allowed.