Output help!!

How do i write a code to write the result of the below code to a .txt file ?

#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main()
{

double ht;// height in feet
double wt; // weight in lbs
double ag; // age in years
double BMR; // basic metabolic rate
double calories; // calories burned

cout << "Please enter the height in feet!";
cin >> ht;
cout << "Please enter the weight in pounds!";
cin >> wt;
cout << "Please enter the age in years!";
cin >> ag;

BMR = 66 + 6.2*wt + 152.4*ht - 6.8*ag;

cout << "The BMR is calculated as:::" << BMR << endl;

cout << "Now that you know your basic metabolic rate!" << endl;

cout << "Let's take a look at your calories burn base on activity level" << endl;

cout << "Please select your activity level; " << endl;
cout << "*********************************" << endl;
cout << "1. Sedentary, little or no exercise, desk job " << endl;
cout << "2. Lightly active (light exercise 1 to 3 times a week) " << endl;
cout << "3. Moderately active (Exercise 3 to 5 times a week) " << endl;
cout << "4. Very active (Exercise 6 to 7 times a week) " << endl;
cout << "5. Extra active (Exercises two times a day. Includes running marathon etc.) " << endl;
cout << "Please enter the number to the left of your activity" << endl;

int selection;
cin >> selection;

if (selection == 1)
{
calories = BMR * 1.2;
cout << "\n\n";
cout << " your BMR is " << BMR << endl;
cout << "your calories burned is " << calories << endl;
}
if (selection == 2)
{
calories = BMR * 1.4;
cout << "\n\n";
cout << "your BMR is " << BMR << endl;
cout << " your calories burned is " << calories << endl;
}
if (selection == 3)
{
calories = BMR *1.6;
cout << "\n\n";
cout << " your BMR is" << BMR << endl;
cout << " your calories burned is" << calories << endl;
}
if (selection == 4)
{
calories = BMR *1.7;
cout << "\n\n";
cout << "your BMR is " << BMR << endl;
cout << "your burned calories is " << calories << endl;
}
if (selection == 5)
{
calories = BMR *1.9;
cout << "\n\n";
cout << " your BMR is " << BMR << endl;
cout << " your calories burned is" << calories << endl;
}
system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
#include <fstream>

    ...
    std::ofstream ofstr { "my_example_file.txt" };
    
    ofstr
        << "height: " << ht << '\n'
        << "weight: " << wt << '\n'
    ....
Last edited on


#include <iostream>
#include <string>
#include <fstream>
using namespace std;


int main()
{
cout << "what is the filename to store to?" << endl;
string filename = "BMR.txt";
cin >> filename;
ofstream fin;
fin.open(filename);
if (!fin.is_open())
{
cout << " could not open " << filename << endl;
return 0;
}




this is what I have, but the result won't write onto the BMR.txt file, it created the file but there is nothing record on it.
Topic archived. No new replies allowed.