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)