Loss of integer

Hello, I am practicing a problem in Ch3 of Friedman c++ book, where functions are introduced. The problem is:

Write a function that calculates the elapsed time in minutes between a start time and an end time expressed as integers on a 24-hourclock (8:30 P.M = 2030). You need to deal only with end times occurring later on the same day as the start time. Also write a driver program to test your function.

Certain inputs result in a loss of a minute. For example 1144 to 1215, where 1145 or 1146 and 1143work fine. Why does this happen and how would I fix it?



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
#include <cstdlib>

#include <iostream>

#include <cmath>

using namespace std;

int calculations(int, int);

int main()
{
    int start,
    end;
    
    cout << "Please input your start time in military time(example: 8:3=2030): ";
    cin >> start;
    cout << "Please input your end time: ";
    cin >> end;
    
    int calculations(int start, int end);


    cout << "Your elapsed time is " << calculations(start, end) << " minutes.";

    return 0;
}

int calculations(int s, int e)         //calculations to determine the difference in time
{
    float startHr,                  //separation of hours and minutes
            endHr,
            startDec,               //hours and minutes in decimal form 
            endDec,
            startMin,             //minutes when separated from hours
            endMin;
    
    startHr = s/100;                //calculations to separate the hour to integer form
    floor(startHr);
    startMin = s%100;              //calculations to find remainder in minutes
    startDec = startMin/60 + startHr;   //converting minutes to decimal form and adding to hours
    
    endHr = e/100;                    //same calculations for end time
    floor(endHr);
    endMin = e%100;
    endDec = endMin/60 + endHr;
    
    return (endDec - startDec)*60;  //return of final calculations in decimal form multiplied by 60 to convert to minutes
}


Any other criticism is welcome as I would like to look as professional/perfect as possible. "Title" comments are excluded obviously, but I am here to learn! Thanks!
Just a couple of things off the bat.

You don't need line 21.

line 39 and 44 - these return values but you're not saving them anywhere. Did you mean to update the values of startHr and endHr there?
Looks like you have integer division, which will discard the fractional part.
e.g. startHr = s/100;
both s and 100 are integers.
You could use s/100.0f to specify that 100 is of type float, not int.
Last edited on
Using floating point variables when it isn't needed should be avoided.

floor returns a value. It does not change the variable fed to it, so the correct way to use it is: variable = floor(variable);

Consider the following:
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
#include <iostream>

using namespace std;

unsigned difference_in_minutes(unsigned, unsigned);

int main()
{
    unsigned start, end;

    cout << "Please input your start time in military time(example: 8:3=2030): ";
    cin >> start;

    cout << "Please input your end time: ";
    cin >> end;

    cout << "Your elapsed time is " << difference_in_minutes(start, end) << " minutes.";

    return 0;
}

unsigned minutes_since_midnight(unsigned military_time)
{
    const unsigned hours = military_time / 100;
    const unsigned minutes = military_time % 100;

    return hours * 60 + minutes;
}

unsigned difference_in_minutes(unsigned begin_time, unsigned end_time)
{
    const unsigned beg = minutes_since_midnight(begin_time);
    const unsigned end = minutes_since_midnight(end_time);

    return end - beg;
}


http://ideone.com/jJN4IS
at the end, when you return, it is being converted to an integer, so rounding is not what you would want:
(input) 1144 1215
the program would return:
30.99...
which will be converted to an integer, so it will return 30 minutes. A good way of rounding it would be:
float r = ...
int rounded = (int)(r + 0.5f);
so it will be rounded, which will return 31 minutes rather than 30:

return (int) ( ( (endDec - startDec) * 60.0f) + 0.5f);
Note that the brackets are important, because (int) converts the expression to its right to an integer, and this operation is done before any addition, etc, so it will convert + 0.5f to an int, which will be 0.
Thanks much for such quick responses! Now bear with me as I try to interpret with my nooby brain.
First, I see my obvious line 21 and floor mistake.
Next, half of what was said had no been learned yet (adding .0f or unsigned), but I did some research to figure out what those mean.
But - trying to avoid possibly using more advanced techniques as the book states to use float (unsigned, even though it is far less messy) and using what I have, I've been trying to figure out how to use this answer:

Looks like you have integer division, which will discard the fractional part.
e.g. startHr = s/100;
both s and 100 are integers.
You could use s/100.0f to specify that 100 is of type float, not int.
Here is my modification with error:

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
#include <cstdlib>

#include <iostream>

#include <cmath>

using namespace std;

int calculations(int, int);

int main()
{
    int start,
    end;
    
    cout << "Please input your start time in military time(example: 8:30=2030): ";
    cin >> start;
    cout << "Please input your end time: ";
    cin >> end;
    


    cout << "Your elapsed time is " << calculations(start, end) << " minutes.";

    return 0;
}

int calculations(int s, int e)         //calculations to determine the difference in time
{
    float startHr,                  //separation of hours and minutes
            endHr,
            startDec,               //hours and minutes in decimal form 
            endDec,
            startMin,             //minutes when separated from hours
            endMin;
    
    startHr = s/100.0f;                //calculations to separate the hour to integer form
    startHr=floor(startHr);
    startMin = s%100.0f;              //calculations to find remainder in minutes
    startDec = startMin/60.0f + startHr;   //converting minutes to decimal form and adding to hours
    
    endHr = e/100.0f;                    //same calculations for end time
    endHr=floor(endHr);
    endMin = e%100.0f;
    endDec = endMin/60.0f + endHr;
    
    return (endDec - startDec)*60.0f;  //return of final calculations in decimal form multiplied by 60 to convert to minutes
}


I receive an error:
error: invalid operands of types ‘int’ and ‘float’ to binary ‘operator%’
endMin = e%100.0f;
(Using Netbeans btw)
I tried to just add .0f to each the s/100 and e/100 but the result was the same.
Trying to add the .0f to every other constant obviously doesn't work, why is that? How could I make that work? Is the rounding solution the only other way? (as stated by Irrelevant Elephant)
I think my previous reply may not have been appropriate here, looks like i didn't read the question properly.
invalid operands of types ‘int’ and ‘float’ to binary ‘operator%’

Modulus (%) expects integral operands, not floats, and returns an integral.

Overall, you should not use floats at all. Convert the times to minutes for the computation.
For example:
1144 -> 704
1215 -> 735
Diff == 31
Ah of course, as cire pointed out >.>.
Thanks a lot guys, you have educated me! :D
the book states to use float

Where does it state so? Not in your OP.

the book states to use float

Where does it state so? Not in your OP.

I apolagize as I went back and even misread what the book meant, which was to use float rather than double or long double, so even that update was incorrect.

I have run into a new issue as I have been working on this problem all day :(

Assume your friend has solved the previous programming exercise. Write another function that uses your friend’s function to calculate the speed (km/h) one must average to reach a certain desitnation by a designated time. Function inputs include same-day departure and arrival times as integers on a 24-hour clock and the distance to the destination in kilometers. Also write a driver program to test your function.

Updated code:
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
#include <iostream>

using namespace std;

int timeDifference(int, int);
float calcSpeed(float);

int main()
{
    int start,
    end,
            distance;
    
    cout << "Please input your start time in military time(example: 8:30=2030): ";  //request for user to input start time
    cin >> start;
    cout << "Please input your end time: ";                                      //request for user to input end time
    cin >> end;
    

    cout << "Your elapsed time is " << timeDifference(start, end) << " minutes."<<endl;   //result of calculations displayed
    
    cout << "How far must you travel in km?: ";
    cin >> distance;
    
    cout << "Your velocity needed is: " << calcSpeed(distance) << " km/hr.";

    return 0;
}

int minutesSinceMidnight(int time)         //function to convert time to total minutes
{
    
    int hours = time/100;        //separation of time to hours
    int minutes = time%100;      //separation of minutes
    return hours*60 + minutes;         //calculation to return total minutes of hours and minutes

}    
int timeDifference(int s, int e)             //function to subtract start and end time
{
    int begin = minutesSinceMidnight(s);  //declaration of start and end times
    int end = minutesSinceMidnight(e);
    return end - begin;                   //calculation to return minute difference
}

float calcSpeed(float x)
{
    float time = timeDifference(start, end);
    float velocity;
    velocity = x/time/60;
    return velocity;
    
}


errors:
54:33: error: ‘start’ was not declared in this scope
float time = timeDifference(start, end);
54:40: error: ‘end’ was not declared in this scope
float time = timeDifference(start, end);

I believe there are other methods to solving the problem, but I believe the issue I have here is taking the solution from the timeDifference calculation, and inserting it into the calcSpeed function. Is there a way to do that? Thanks!

Edit: Upon reflection, I was wondering why even use int or float when it seemed unsigned could have been used as a universal data type?
Last edited on
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
#include <iostream>

using namespace std;

int timeDifference(int, int);
float calcSpeed(float, int);

int main()
{
    int start, end, distance;

    cout << "Please input your start time in military time(example: 8:30=2030): ";  //request for user to input start time
    cin >> start;
    cout << "Please input your end time: ";                                      //request for user to input end time
    cin >> end;


    cout << "Your elapsed time is " << timeDifference(start, end) << " minutes."<<endl;   //result of calculations displayed

    cout << "How far must you travel in km?: ";
    cin >> distance;

    cout << "Your velocity needed is: " << calcSpeed(distance, timeDifference(start, end)) << " km/hr.";

    return 0;
}

int minutesSinceMidnight(int time)         //function to convert time to total minutes
{

    int hours = time/100;        //separation of time to hours
    int minutes = time%100;      //separation of minutes
    return hours*60 + minutes;         //calculation to return total minutes of hours and minutes

}
int timeDifference(int s, int e)             //function to subtract start and end time
{
    int begin = minutesSinceMidnight(s);  //declaration of start and end times
    int end = minutesSinceMidnight(e);
    return end - begin;                   //calculation to return minute difference
}

float calcSpeed(float x, int diff)
{
    float time = diff;
    float velocity;
    velocity = x/time/60;
    return velocity;

}


should take care of the scope problem...I think
Topic archived. No new replies allowed.