Hi. The following codes should be in C++. Modify your code from the bmi program
ID: 3779006 • Letter: H
Question
Hi. The following codes should be in C++.
Modify your code from the bmi program you wrote earlier:
-put the bmi calculation in a function – the function returns bmi and has parameters for weight and height
-put the user input in a function – the function prints all prompts, gets the data and returns weight and height to main()
-put a loop in the main function so that the user can calculate bmi as many times as they would like
Here is the previous code I had written:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int height, weight, x = 703;
double bmi;
string name;
// Welcome and read user name
cout << "Welcome to the BMI Calculator Program. ";
cout << "What is your name? ";
cin >> name;
//read user height
cout << "Please enter your height in inches: ";
cin >> height;
//Calculate user BMI
bmi = (weight * x) / pow(height,2.0);
//Display results
cout << name << ", your BMI is: " << bmi << endl;
return 0;
}
Thanks in advanced!!!
Explanation / Answer
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//function declarations
double bmiCalculation(int weight, int height);//method to calculate the bmi
string promptUserName();//method to prompt user for name
int promptUserWeight();//method to prompt user for weigth
int promptUserHeight();//method to prompt user for height
int main()
{
int height, weight, x = 703;
double bmi;
string name;
// Welcome and read user name
cout << "Welcome to the BMI Calculator Program. ";
name=promptUserName();//prompt user for user name
//read user height
height=promptUserHeight
//read user weight
weight=promptUserWeight();
//Calculate user BMI
bmi = bmiCalculation(weight,height);
//Display results
cout << name << ", your BMI is: " << bmi << endl;
return 0;
}
//method to calculate the bmi
double bmiCalculation(int weight, int height){
double bmi;
int x= 703;
bmi = (weight * x) / pow(height,2.0);
return bmi;
}
//method to prompt user for name
string promptUserName(){
string name;
ccout << "What is your name? ";
cin >> name;
return name;
}
//method to prompt user for weigth
int promptUserWeight(){
int weight;
cout << "Please enter your weight in kg: ";
cin >> weight;
return weight;
}
//method to prompt user for height
int promptUserHeight(){
int height;
cout << "Please enter your height in inches: ";
cin >> height;
return height;
}
--------output----------
Welcome to the BMI Calculator Program.
What is your name? John
Please enter your height in inches: 68
Please enter your weight in kg: 75
John, your BMI is: 11.4025
--------output ends---------
Note: feel free to ask any doubts. God bless you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.