Here is my prompt for my homework:
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. Create 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. Keep prompting and reading in new ages and heart rates until the user enters a negative value for the age.
I feel like I have everything right but it is not compiling, its giving me the error:
"error: expected ';' before 'true'" and "error: expected ';' before 'false'"
I keep having trouble returning the bool value and making the if statement in function main the correct value.
Here is my code:
#include <iostream>
usingnamespace std;
int heartRate (int, int, bool);//Function prototype.
int main()
{
int age;//To store the age.
double heartbeats_minute;// To store amount of heartbeats
bool target_heart_rate;//For the target heart rate to return true or false.
//Ask their age
cout << "What is your age: ";
cin >> age;
// 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, target_heart_rate);
//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.\n";
cout << "Enter another age.";
cin >> age;
}
else
{
cout <<"You are not within your target heart range.\n";
cout <<"Enter another age";
cin >> age;
}
}
return 0;
}
// heartRate function
int heartRate(int age, int heartbeats_minute)
{
constdouble target_percent = .65;//Percent to be within to have target heart rate
constint num = 220;// Number to subtract by the age
double target_rate;// Target rate
bool target_heart_rate;
target_rate = ((num - age)*target_percent);// Calculate the target rate
cout << target_rate;
if (heartbeats_minute >= target_rate)// If greater than target rate return true
{
return target_heart_rate true;
}
else
{
return target_heart_rate false;
}
}
You can return only one value from a function, and you seem to be trying to return two values. One is target_heart_rate and another is tru or false. Just say return true; or return false; and try.
Also your function prototype and definition are taking different parameter types.
return statements just take a value; as on line 40, you have return 0;, not returnint 0; or anything like that.
Also, heartRate seems to be trying to return a bool, but is declared as returning an integer. Finally, target_heart_rate is being used without being initialized on lines 24 and 27. Were you trying to pass the parameter to the function and have the function modify it, perhaps? If so, you should look up how to use references.
You may also want to note that your prototype and declaration of heartRate() don't match; they need to in order to avoid a linker error you'll get after you fix your current error.
Thanks for the quick replies, I think i have fixed everything ya'll have said and i have gotten rid of the errors, but my program is not running correctly. It is returning false no matter what and i don't know how to fix it. Here is my fixed code. Any ideas?
#include <iostream>
usingnamespace std;
int heartRate (int, int);//Function prototype.
int main()
{
int age;//To store the age.
double heartbeats_minute;// To store amount of heartbeats
bool target_heart_rate;//For the target heart rate to return true or false.
//Ask their age
cout << "What is your age: ";
cin >> age;
// 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.\n";
cout << "Enter another age.";
cin >> age;
}
else
{
cout <<"You are not within your target heart range.\n";
cout <<"Enter another age";
cin >> age;
}
}
return 0;
}
// heartRate function
int heartRate(int age, int heartbeats_minute)
{
constdouble target_percent = .65;//Percent to be within to have target heart rate
constint num = 220;// Number to subtract by the age
double target_rate;// Target rate
target_rate = ((num - age)*target_percent);// Calculate the target rate
cout << target_rate;
if (heartbeats_minute >= target_rate)// If greater than target rate return true
{
returntrue;
}
else
{
returnfalse;
}
}
Change int heartRate(int age, int heartbeats_minute) to bool heartRate(int age, int heartbeats_minute) and change it on your prototype to match. The int there declares a return type of int while you are wanting to return a bool.
Do the changes that ryancb06 said, then in your main function, use target_heart_rate = heartRate(age, heartbeats_minute);.
You should also change the heartRate function, because you are not solving the problem correctly.
Your function returns true when heart rate is 65% (or higher) of the maximal heart rate. The problem says that it should return true if it's between 60% and 70% of maximal heart rate.
Wow thanks fg109 I don't know how i missed that. I fixed everything ya'll said to but its still just giving me false. I'm sorry i'm very new at this, i've only been programming since September. I know once i find the answer it will be a duh moment, just not seeing anything right now.
#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.\n";
cout << "Enter another age.";
cin >> age;
}
else
{
cout <<"You are not within your target heart range.\n";
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
target_rate_low;
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 (heartbeats_minute >= target_rate_low && heartbeats_minute <= target_rate_high)
// If greater than target rate return true
{
returntrue;
}
else
{
returnfalse;
}
}