View Full Version : C++ question

06-27-2010, 05:55 PM
(here's the actual thing, if it helps any)

A long-distance carrier charges the following rates for telephone calls:
Starting Time of Call
Rate per Minute
00:00–06:59 : 0.12

07:00–19:00 : 0.55

19:01–23:59 : 0.35

Write a program that asks for the starting time and ending time of the call in military format, and then calculates and displays the number of minutes and the charges of the call. The program should ask the user to enter the time as a floating-point number in the form HH.MM. For example, 07:00 hours will be entered as 07.00, and 16:28 hours will be entered as 16.28.

Example 1:
Starting Time: 18:50 Ending Time: 20:03 Duration: 73 mins. Charges: $40.15

Example 2:
Starting Time: 23:50 Ending Time: 00:17 Duration: 27 mins. Charges: $9.45

Input Validation

The program should check if: the starting time and the ending time are numbers. the starting time and the ending time are in military format. (i.e. 0 <= hrs <= 23 and 0 <=
mins <= 59)

If the input is not valid, then the program should display an appropriate error message and terminate immediately. For example, if the starting time is invalid, the program
should not prompt the user for the ending time of the call.

Requirements: Your program should follow the programming style guidelines discussed in class. In addition, it should
be well documented and use descriptive variable names. The name of the variable should reflect its purpose. Your program must use named constants where appropriate.

06-27-2010, 10:58 PM
and my psuedo code doesn't look like much but I'm trying to figure it out.

1. prompt the user to enter the starting time in military hours.
2.input startingtime
3.prompt the user to enter the ending time in military hours.
4.input endingtime
5. store startingtime in an int to store hours
6. store the minutes of starting time in another int
7. (stuck)

06-27-2010, 11:47 PM
Sure this isn't homework?

06-27-2010, 11:55 PM
Lets just consider it my homework given to me by my book now that I can't figure it out I wanna learn it and make sure I understand it.

I'm assuming I'm suppose to take a float and then separate it.

I know if I store a float as an integer I can grab the hours, but I don't know how to grab a float, example 23.45 and just grab up the 45 to store that into minutes.

I could also use set precision to just read 23, but still it's the minutes I don't know how to grab.

maybe I'm also going about this the wrong way lmao.

also thinking of doing the modulus of start time and the modulus of end time and adding them up to get the minutes. modulus of 60.

I dunno my heads all over the place.


and of course I forgot that modulus only words with integers not float, so that shoots me down instantly there. that's why I was considering doing the hours and minutes seperate as ints. hours % 24 and mins % 60 then to add them up.

2nd edit:

I'll just ask my teacher at school tomorrow, thanks again. super headache activated :(

06-28-2010, 07:56 PM
Your user input will start out as a String or character array, it seems to me that the floating point requirement is more a format issue than an actual data type issue.

Look here for input/output in C++: http://www.cplusplus.com/doc/tutorial/basic_io/

Once you have the String, you can use some of the standard String methods like strtok to split it using the dot as your delimiter.

Here is the reference page for strtok: http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Once you've split it into 2 strings, you can convert to an integer and do whatever you need to do from there.

06-30-2010, 03:16 AM
I finally spoke to my teacher about it, and he says my best approach is to convert time into minutes itself starting from 00.00.

for example:

Starting 00.00

then someone inputs the starting time of 02.50

from 00.00 to 02.50

a number of minutes are accumulated.

then they input the ending time at 05:00

and from 00.00 to 05.00

another number of minutes are accumulated, then you subtract those two accumulated minutes together to find the duration.

have any explanation towards this?

I'd rather figure out the logic behind it instead of the code. Syntax code isn't hard to do, it's the logical part (problem solving?) that I want to get better at.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum