Hi guys...

I'm a student having a bit of trouble with my assignment.

The assignment is to make a program for a phone company to calculate call durations, costs, discounts etc...

I need the user to input call start time in 24 hour style... and end time.

In order to calculate the call I need to seperate hours from minutes... I'm sure this can be done by converting the numbers from/to string or something.

Input Call Start Time: 0800
Input Call End Time: 0815

Calculating it with these numbers would give 55 mins when the duration is actually 15 minutes.

Can anybody help?

Much appreciated

Here's a bit of what I have so far, the actual program is a lot larger but this is the function I'm struggling with:

#include <iostream.h>
#include<stdio.h>

double S_TIME,E_TIME,time_mins;




void parking()

{

cout<<"Please enter call start time\n\n";
cin>>S_TIME;
cout<<"\n\n";
cout<<"Please enter call end time\n\n";
cin>>E_TIME;


}
int convert(char time[5])

{
	int time_mins;
	int int_time[4];

		int_time[0]=time[0] - '0';
		int_time[1]=time[1] - '0';
		int_time[2]=time[2] - '0';
		int_time[3]=time[3] - '0';


time_mins = int_time[0] * 1000 + int_time[1] * 100 + int_time[2] * 10 + int_time[3];
return time_mins;
}

void main()

{



	parking();



}

I'm having trouble understanding it, if someone could help I would much appreciate it.

Cheers

Well does this really convert the char to string?

int convert(char time[5])

{
	int time_mins;
	int int_time[4];

		int_time[0]=time[0] - '0';
		int_time[1]=time[1] - '0';
		int_time[2]=time[2] - '0';
		int_time[3]=time[3] - '0';


time_mins = int_time[0] * 1000 + int_time[1] * 100 + int_time[2] * 10 + int_time[3];
return time_mins;
}

You should use string streams to do that.

And secondly. When you are taking doubles as input why do you need to convert from char to int again?

No need to convert into strings.
Try this

#include <iostream.h>
#include<stdio.h>
#include

void parking()
{
  int S_TIME,E_TIME,time_mins;
  cout<<"Please enter call start time\n\n";
  cin>>S_TIME;
  cout<<"\n\n";
  cout<<"Please enter call end time\n\n";
  cin>>E_TIME;
  if( (E_TIME-S_TIME) > 0 )
    time_mins = (( (E_TIME - S_TIME)/100) - 1 ) * 60 + ( 60 - ( S_TIME - (S_TIME/100) ) ) + E_TIME - ( (E_TIME/100)*100 );
  else
    time_mins = ( 12 + ((E_TIME - S_TIME)/100) - 1 ) * 60 + ( 60 - ( S_TIME - (S_TIME/100) ) ) + E_TIME - ( (E_TIME/100)*100 );
}

void main()
{
  parking();
}

Here's my reasoning:
There will be 2 cases:
*Starting time is numerically lower (0840 or 8:40pm) than end time (1050 or 10:50pm)

To calculate the time in minutes, you must remember we can only have 8:59 and not 8:60.
This can also be restricted for input validity.
----For the first case:----
Let's say you have 0840 as your starting time.
Let's say you have 1050 as your end time.
if you do 1050 - 840 (when reading an int, that first zero in 0850 is neglected and only 850 is stored) you get 210; but we know that's incorrect.
But that 2, from 210 does tell us something, there difference between the hours, just the hours without considering the minutes. In this case is 2, and 2 is the difference in hours between 8 and 10. If you were to just multiply that 2 by 60, the number of minutes in an hour, you'd be assuming the call started at 8:00 and ended at 10:00pm, but we know that's not true. So what we do is subtract 1 from that difference (10 - 8 ) - 1 and multiply that by 60.
So far we got: time_mins = ( (E_TIME - S_TIME)/100) - 1 ) * 60
Note: (E_TIME - S_TIME)/100) will return a integer because E_TIME and S_TIME are int variables.
Now, we need to get more accurate. For this example the call started at 8:40 so before it was 9:00pm, the person had already spoken for 20 minutes, and that's 60 - 40, and 40 is 840 - 800. We'll get that from 60 - ( S_TIME - (S_TIME/100) ). Again, (S_TIME/100) returns an integer, in this case 800. That should give us 60 - (840 - 800) = 20.
So far we got: time_mins = (( (E_TIME - S_TIME)/100) - 1 ) * 60 + ( 60 - ( S_TIME - (S_TIME/100) ) )
Something similar happens for the minutes of the call after 10:00pm. For this example the call ended at 10:50 pm, 50 minutes after 10:00pm. So we just need 1050 - 1000 = 50. We'll get that with: E_TIME - ((E_TIME/100)*100)
That brings us to:
time_mins = (( (E_TIME - S_TIME)/100) - 1 ) * 60 + ( 60 - ( S_TIME - (S_TIME/100) ) ) + E_TIME - ( (E_TIME/100)*100 )

*Starting time is numerically higher (1100 or 11:00pm) than end time (0230 or 2:30am)
----For the second case----
With these times, we know the call lasted 3 hours and 30 minutes, or 210 minutes.
If you do 230 - 1100 you will get -870. If we divide that by 100, we get -8, if we add that to 12, we get 12 + (-8) = 4. For the same reason as in the first case, we subtract 1 from that result and we have 3, then we multiply that by 60. But by trial end error and deeper thinking (when we divide -870 by 100 and get -8, we're missing out the -0.7) you get to the conclusion that we actually need to subtract 2, not just 1 for this case. So we'd have 12 + (-8) = 4 - 2. This way the rest of the formula works properly and end up with:
time_mins = ( 12 + ((E_TIME - S_TIME)/100) - 2 ) * 60 + ( 60 - ( S_TIME - (S_TIME/100) ) ) + E_TIME - ( (E_TIME/100)*100 )

Let me know if this works for you, I haven't tried the code yet, I'm doing other stuff on the computer and this took quite a while to type down :)

In school we don't use iostream but we do use stdio. I still can guess what the iostream functions do though ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.