Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: C++ question

  1. #1
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    C++ question

    (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.
    Last edited by demoneyejin; 06-27-2010 at 11:39 PM. Reason: first question might've been harder, was lazy to type this up but then I did.

  • #2
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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)
    Last edited by demoneyejin; 06-27-2010 at 11:40 PM.

  • #3
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Sure this isn't homework?
    Call me Brick, if you like!

  • #4
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

    edit

    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
    Last edited by demoneyejin; 06-28-2010 at 12:12 AM.

  • #5
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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/c...string/strtok/

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

  • #6
    New Coder
    Join Date
    Nov 2009
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •