Target heart rate

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>

using namespace 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)
{
   const double target_percent1 = .60; // Percent to be within to have target heart rate.
   const double target_percent2 = .70;
   const int 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)
   {
      return true;
   }
   else
   {
      return false;
   }
   
}
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]
Last edited on
so I need to move that line further down in the code?
If you want thing B to happen after thing A, then you have to put the code that does thing B after the code that does thing A. So, yes.
Last edited on
It works now but when it prints out the target_rate_high and target_rate_low twice.

for example,
What is your age? 30
How many heartbeats did you count in a minute? 130
133
114
133
114
You are within your target heart range.
Enter another age: 
Are you still calling your heartRate function twice?

I fixed it. Thanks a lot!
You're welcome. Glad it worked out.
Topic archived. No new replies allowed.