float time = int minutes / 60 equals 0?

Why does my float time keep returning 0 when it is initialized to be int minutes/ 60? It is boded and underlined below.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 #include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

// Metabolic equivalent of tasks (mets) are listed, and are global constants..
const float RUNNING = 10.0f, BIKING = 8.0f, LIFTING = 3.0f, YOGA = 2.5f;


int main()
{
    // variables are listed.
    float weight = 0, weightKG = 0, calories = 0; // weight is initially in pounds (lbs)
    float MET = 0, activityTime = 0;
    int activityChoice = 0, minutes = 0;
    bool flag = 1; //Used later for data validation.

    cout << "Welcome to Song's Fitness Center." << endl;

    do // do while loop for weight data validation.
    {
        cout << "Please input your weight in pounds: ";
        cin >> weight;
        weightKG = weight / 2.2;
        flag = 0;
            if (cin.fail() || (weight < 1 || weight > 999)) //Checks if data entered if correct.
            {
                cin.clear();
                cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                cout << "You weight must be positive number between 1 and 999." << endl;
                flag = 1;
            }
    } while (flag); //Checks if the loop needs to be repeated.

    do // do while loop for activity data validation.
    {
        cout << "____________________________________________" << endl
             << "|Please input activity type, you may choose|" << endl
             << "|1 for Running,                            |" << endl
             << "|2 for Biking,                             |" << endl
             << "|3 for Lifting,                            |" << endl
             << "|4 for Yoga,                               |" << endl
             << "|5 for End.                                |" << endl
             << "|------------------------------------------|" << endl
             << " " << endl
             << "Please input the activity you wish to track: " << endl;

        cin >> activityChoice;
        flag = 0;

        switch (activityChoice) //Switch statement for picking which exercise the user did.
        {
            case 1:
                MET = RUNNING;
                do // do while loop for minute input data validation.
                {
                    cout << "Please enter the amount of time spent doing activity in minutes between 30 and 60: ";
                    cin >> minutes;
                    activityTime = minutes / 60;
                    flag = 0;

                    if (cin.fail() || (minutes < 30 && minutes > 60))
                    {
                        cin.clear();
                        cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                        cout << "Please enter a number between 30 and 60." << endl;
                        flag = 1;
                    }
                } while (flag); //Checks if the loop needs to be repeated.
                // The equation for calories burned is declared.
                calories = activityTime * MET * weightKG;
                //Outputs the answer with a fixed position at 1 decimal point.
                cout << fixed << setprecision(1) << calories << " calories 
                burned." << endl;
                if (calories >= 0)
                {
                    if (calories >= 0 && calories <= 200)
                    {
                        cout << "Light-Intensity aerobic activity.\n";
                    }
                    else if (calories >= 201 && calories <= 500)
                    {
                        cout << "Moderate-intensity activity.\n";
                    }
                    else
                    {
                        cout << "Vigorous-intensity aerobic activity.\n";
                    }

                }
                flag = 1;
                break;
Last edited on
Do you realize that there are no fractions when doing integer math?

Since both minutes and 60 are integers, the following is doing integer math.
activityTime = minutes / 60;
Therefore if minutes is less than 60 you will get zero.

How would I go about fixing this without using casting? As we have not learned this in class yet we are not allowed to use it.
I fixed it by adding a .0 after the 60. So now it looks like float activityTime = int minutes / 60.0 and it got 204.5 which was the correct answer.
Topic archived. No new replies allowed.