HELP! calculating with time

• (2 Pages)
• 1
• 2

19 Replies - 1267 Views - Last Post: 24 October 2013 - 09:41 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=332164&amp;s=8bf7181ac3304701e75cd51485f05f47&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

HELP! calculating with time

Posted 22 October 2013 - 12:30 PM

hi, Im writing a program in which users will be inputting:
start time = startTime
number of minutes = callDuration

so first off, teacher wants us to input time as a floating point number such as
09.00

how do I validate the time so that users cant input i.e. 09.61 for every hour?
in other words, so the user cant input minutes 60-99 for every hour.

then, when calculating:
endTime = startTime + callDuration

how would I make endTime display a correct time in such a situation:
endTime = 09.45 + 00.30

so that it displays 10.15 not 09.75

any help would be greatly appreciated

Is This A Good Question/Topic? 0

Replies To: HELP! calculating with time

#2 Vaypur

Reputation: 11
• Posts: 53
• Joined: 09-October 13

Re: HELP! calculating with time

Posted 22 October 2013 - 12:40 PM

You could use if statements to compare the data.

Perhaps you can split it into hours and minutes and do something like the following for the
``` if timeMins > 60
hours++;
timeMins - 60;
if timeMins <= 0
timMins = 0;
....

```

Note that I am just throwing stuff out here to get you on track and the above code is untested. Give it a shot and see what happens. Post your code here if you need further assistance.

This post has been edited by Vaypur: 22 October 2013 - 12:41 PM

#3 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

Re: HELP! calculating with time

Posted 22 October 2013 - 01:11 PM

i thought of that but it seams the Teach is set on the user input being a floating point number such as 09.00 meaning one variable.

#4 Peter O

• D.I.C Regular

Reputation: 125
• Posts: 287
• Joined: 19-October 13

Re: HELP! calculating with time

Posted 22 October 2013 - 01:44 PM

This is not a good place to use floating point numbers because they are imprecise and could lead to rounding errors. It is better to read the time as two integer variables, hours and minutes. To make it easier to do calculations with the time you can convert it to minutes. When you have converted the time to minutes you simply use the + operator to add the times, and then you convert it back to hours and minutes to be able to print it correctly.

#5 Vaypur

Reputation: 11
• Posts: 53
• Joined: 09-October 13

Re: HELP! calculating with time

Posted 22 October 2013 - 01:58 PM

Typically when hours and minutes are used in the decimal form, the decimal doesn't stop at 60. Is this a specific requirement from your professor? Like 9 hours and 45 minutes represented as a decimal would be 9.75. The way I am understanding your problem is that a user will enter the start time, like 12:41. The start time should then be converted to minutes. The call duration would then be input and converted to minutes. Hours and Minutes would be added, and converted to the float number you are requesting. Does that sound like your assignment? I suppose I don't fully understand what your professor will and will not allow for this specific problem.

#6 Skydiver

• Code herder

Reputation: 5261
• Posts: 17,714
• Joined: 05-May 12

Re: HELP! calculating with time

Posted 22 October 2013 - 07:13 PM

Or to paraphrase Vaypur's question: What does 9.61 represent? Is that 9 hours and 61 minutes? Is that 9 hours, 36 minutes and 36 seconds?

#7 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

Re: HELP! calculating with time

Posted 22 October 2013 - 08:14 PM

ok, let me clarify the problem a little bit.

the program is used to calculate the charge for a phone call using the user inputs:

float startTime // input example 09.00 as per teacher
callDuration // input example 00.30

there are 3 constants:

rate per minute 1, 2, and 3 which correspond with 3 diferent times zones throughout the day. their value doesn't realy realy concern you. im not tryn to get you to do my homework. you know...

needless to say, you multiply the callDuration by the RPM that the startTime corresponds with.

the program is to display:

startTime
callDuration
endtime
RPM // rate per minute
totalCharge

now... ive got everything pretty much figured out except for:

1. how to validate the time so that a time such as 09.61 can not be input.
2. in a situation where endTime = startTime(09.55) + callDuration(00.10) I don't want the program to display 09.65

I emailed my Teach and he gave me a little hint saying to look at the fmod function. you guys know anything about that?

again, I really appreciate you guys helping me out.

#8 Skydiver

• Code herder

Reputation: 5261
• Posts: 17,714
• Joined: 05-May 12

Re: HELP! calculating with time

Posted 22 October 2013 - 08:24 PM

Did you even look at the documentation of fmod() to try to help yourself? http://en.cppreferen...meric/math/fmod

You still didn't answer the question directly, but it sounds like 0.30 means 30 minutes, and not 18 minutes. Is that correct?

#9 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

Re: HELP! calculating with time

Posted 22 October 2013 - 08:34 PM

no, didn't look at it yet. ive been out. wrote the post on my phone but thanks for the link. yes 0.30 means 30 minutes

#10 Vaypur

Reputation: 11
• Posts: 53
• Joined: 09-October 13

Re: HELP! calculating with time

Posted 23 October 2013 - 08:57 AM

Fantastic news. fmod will make this project a piece of cake.

```int test = fmod(10,2);
```

The above would set test to 0, because there is no remainder of 10 being divided by 2. Because you are allowed to use this, you should be able to do everything with one float number. No need to split it up into 2 different variables. If you need help, you know where to find us.

fmod documentation

#11 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

Re: HELP! calculating with time

Posted 23 October 2013 - 01:11 PM

forgive my complete ignorance for im all of about 2 months into programming but Im not following. can you elaborate?

#12 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

Re: HELP! calculating with time

Posted 23 October 2013 - 01:17 PM

just19k, on 23 October 2013 - 01:11 PM, said:

forgive my complete ignorance for im all of about 2 months into programming but Im not following. can you elaborate?

I mean I get what fmod does. I just don't see how it can help me.

#13 Vaypur

Reputation: 11
• Posts: 53
• Joined: 09-October 13

Re: HELP! calculating with time

Posted 23 October 2013 - 01:49 PM

Oh sure, so fmod will give the remainder of division right?

Say you have 9.75. You have decided that at .60 you are calling it an hour. If I were to do
```fmod(9.75, 1);
```

I would have the remainder of .75. At this point, I can increment the hours by 1, subtract 60 and check again to make sure that my remainder is not >= .60. I really would like to give you an example in code, but I feel like that would essentially be doing the assignment for you.

#14 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

Re: HELP! calculating with time

Posted 23 October 2013 - 04:32 PM

I think I know what youre saying but im really not sure without that example;) im working on the code now and ill post it when im done or get stuck. probably the latter, haha.

thanks a bunch

#15 just19k

Reputation: 0
• Posts: 11
• Joined: 22-October 13

Re: HELP! calculating with time

Posted 23 October 2013 - 06:49 PM

im having trouble validating the minutes in the time.
[// long-distance call charge.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
// variables
float startTime, // time at start of call
endTime, // time at end of call
totalCharge, // cost of the call
HH, // hours
MM; // minuts

// constants
const float rpm1 = 0.07, // rate per minute 1 (00.00 - 07.59)
rpm2 = 0.12, // rate per minute 2 (08.00 - 15.59)
rpm3 = 0.28, // rate per minute 3 (16.00 - 23.59)
min_time = 00.00, // minumum time
max_time = 23.59, // maximum time
max_min = 0.59; // maximum minutes

// set the numeric output formatting.
cout << fixed << showpoint << setprecision(2);

// get user inputs
cout << "Please enter the start time of the call in HH.MM format using military time. ";
cin >> startTime;

// validate input
if (startTime >= min_time && startTime <= max_time) // validates start time is greater than 00.00 and less than 23.59
MM = fmod(startTime, 1); // extracts minutes from the start time
else
// an invalid time was entered.
cout << "that is an invalid time. Rerun the program and try again. ";
if (startTime < min_time && startTime > max_time) exit(0);
if (MM > max_min)
{
cout << "that is an invalid time. Rerun the program and try again. ";
exit(0);
}

return 0;
}

]