Feb 27, 2014 at 5:00am UTC
Hello, I'm new to C++ and need help finishing up my program. I am writing a program that calculates BMI and I would like to be able to view my results both on the screen and in a separate file.
I know that cout << " "<< enables me to print on the screen.
I also know that ofstream outfile; followed with results.open(filename); enables me to print my results to a file.
How can I do both at the same time?(printed on the screen, and to a separate file?) Thanks in advance.
This is my code so far with just the results being printed to the screen:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <cmath>
using namespace std;
const int number=703;
void PromptUser(float &feet, float &inches);
float ConvertToInches(float &feet, float &inches);
float BMICalculator(float &weight, float &height);
void heightWeight(float feet, float inches, float weight);
void WeightStatus(float &bmi);
/******************************************************************************/
//PromptUser Function
void PromptUser(float &feet, float &inches)
{
cout << "BMI Calculator" << endl;
cout << "Enter the patient's height(in ft and inches). Enter 0 0 to stop." << endl;
cin >> feet>> inches;
return;
}
/******************************************************************************/
// ConvertToInches Function
float ConvertToInches(float &feet, float &inches)
{
float height;
height= (feet*12)+inches;
return height;
}
/******************************************************************************/
// BMI Calculator Function
float BMICalculator(float &weight, float &totalHeight)
{
float bmi;
bmi= (weight)/(totalHeight*totalHeight)*number;
return bmi;
}
/******************************************************************************/
// WeightStatus Function
void WeightStatus(float &bmi)
{
cout.precision(1);
cout << "Your BMI is: " << bmi <<", indicating that your weight is in the" << endl;
if (18.5 <= bmi && bmi <= 24.9)
cout << "normal category for adults of your height." << endl;
else if (bmi < 18.5)
cout << "underweight category for adults of your height." << endl;
else if (24.9 < bmi && bmi < 29.9)
cout << "overweight category for adults of your height." << endl;
else
cout << "obese category for adults of your height." << endl;
cout << endl;
cout << endl;
return;
}
/******************************************************************************/
void heightWeight(float feet, float inches, float weight)
{
cout.precision(0);
cout << "Height:"<< " " << feet << " " << "feet," << " " << inches << " "<< "inches"<< endl;
cout << "Weight:"<< " "<< weight << " " << "pounds" << endl;
cout << endl;
return;
}
// Main Function
int main()
{
float weight; //sets variable for weight (in pounds)
float feet; // sets variable for initial height in feet
float inches; // sets variable for initial height in inches
float totalHeight; // sets variable for calculated total height
float bmi; // sets variable for calculated Body Mass Index(BMI)
cout.setf(ios::fixed, ios::floatfield);
PromptUser(feet,inches);
while(feet&&inches>0) {
totalHeight= ConvertToInches(feet,inches);
cout << "Enter the patient's weight." << endl;
cin >> weight;
bmi= BMICalculator(weight,totalHeight);
heightWeight(feet,inches,weight);
WeightStatus(bmi);
PromptUser(feet,inches);
}
return 0;
system("PAUSE");
}
Last edited on Feb 27, 2014 at 5:01am UTC