Your Target Heart Rate is the rate at which your heart should beat to get the maximum benefit from aerobic exercise. This rate is generally agreed to be 60 to 70 percent of your maximal heart rate. Your maximal heart rate equals 220 minus your age.
reate a program which includes the following:
A main function that asks the user to enter an age and the number of heartbeats counted in one minute.
These values should be passed to a function that will calculate the Target Heart Rate.
The function should return true if the user is within his/her Target Heart Rate range and false if not.
The main function or driver should then display a message indicating if the user is in his/her Target Heart Rate range.
The main should continue prompting and reading in new ages and heart rates until the user enters a negative value for the age.
It keeps returning false for me even though the heartbeat I enter is in the target heart range.
#include <iostream>
usingnamespace std;
bool heartRate (int, int); // Function prototype.
int main()
{
int age; // To store the age.
double heartbeats_minute; // To store amount of heartbeats.
// Ask their age.
cout << "What is your age? ";
cin >> age;
bool target_heart_rate = heartRate(age, heartbeats_minute); // For the target heart rate to return true or false.
// Start a loop while age is not a negative number.
while (age > 0)
{
// Ask for heartbeats counted.
cout << "How many heartbeats did you count in a minute? ";
cin >> heartbeats_minute;
// Call heartRate function.
heartRate(age, heartbeats_minute);
// If target heart rate is true display message they are within range.
if (target_heart_rate == true)
{
cout << "You are within your target heart range." << endl;
cout << "Enter another age: ";
cin >> age;
}
else
{
cout << "You are not within your target heart range." << endl;
cout << "Enter another age: ";
cin >> age;
}
}
return 0;
}
// heartRate function
bool heartRate(int age, int heartbeats_minute)
{
constdouble target_percent1 = .60; // Percent to be within to have target heart rate.
constdouble target_percent2 = .70;
constint num = 220; // Number to subtract by the age.
double target_rate_high, target_rate_low; // Target rate.
target_rate_high = ((num - age) * target_percent2); // Calculate the target rate high.
target_rate_low = ((num - age) * target_percent1); // Calculate the target rate low.
cout << target_rate_high << endl;
cout << target_rate_low << endl;
// If greater than target rate return true.
if (heartbeats_minute >= target_rate_low && heartbeats_minute <= target_rate_high)
{
returntrue;
}
else
{
returnfalse;
}
}
At line 16, you're calling heartRate() before you've asked the user to input their age. In fact, you're calling it with age and heartbeats_minute uninitialised, so they could contain anything.
Edit: Misread your code.
Edit 2: OK, at line 16, you're calling heartRate() to set the value of target_heart_rate before you've asked the user to input their heartbeats per minute. In fact, you're calling it with heartbeats_minute uninitialised, so it could contain anything.[/s]