1. conversion

I wrote this code and i can't seem to convert the hours to minutes . here is my code
Code:
```#include <iostream.h>

int main()
{
// declare variable
int start_hour, start_min;
int stop_hour, stop_min;
float tax;
float discount;
int ampm;
float cost;
int difference,hour_difference, min_difference;
float charges;
int hold;

// get times from user
cout << "Enter Start Hour of call : ";
cin  >> start_hour;
cout << "Enter Start Minute of call : ";
cin  >> start_min;
cout << "1) AM" << endl;
cout << "2) PM" << endl;
cout << "Please choose your time by entering 1 or 2 : ";
cin  >> ampm;
cout << "Enter Stop Hour of call : ";
cin  >> stop_hour;
cout << "Enter Stop Minute of cal; : ";
cin  >> stop_min;
cout << "1) AM" << endl;
cout << "2) PM" << endl;
cout << "Please choose your time by entering 1 or 2 : ";
cin  >> ampm;

// switch statement
switch(ampm)
{
case 1:
cout << "The time of your phone call was in the morning." << endl;
break;
case 2:
cout << "The time of your phone call was in the evening." << endl;
break;
default:
cout << "Please enter a valid number (1 or 2) next time you run program." << endl;
break;
}

// give values to variable & if/else statements and converts min. to hour.
tax = (cost * 0.05);
if (ampm == 1 && start_hour >= 8 && start_min >=00)
cout << "Your phone call will be charged full price. " << endl;

if (ampm == 2 && stop_hour <= 5 && stop_hour <=00)
cout << "Your phone call will be charged full price. " << endl;

if (start_min >=60)
difference = (start_hour + 1);

if (stop_min >=60)
difference = (stop_hour + 1);

if (start_hour >12)
cout << "Invalid start hour." << endl;

if (stop_hour >12)
cout << "Invalid stop hour." << endl;

// convert time
hour_difference = (stop_hour - start_hour);
min_difference = (stop_min - start_min);
cout << "Your call is " << hour_difference << " hour(s) " << min_difference << " minute(s) " << endl;

// phone charges
if (start_hour >=8 && start_min >=00 && stop_hour <=5 && stop_min <=00)
cout << "Your charges are \$ " << (min_difference * 0.20) << endl;

cin >> hold;
return 0;
}```

3. Hours to Minutes: Multiply the hours with 60.

If you mean something else, plz be a little more specific. I see no Hour -> Minute conversion in your code.

4. hour to minute

yeah i tried to put hour to minute conversion. but i can't do the formula. if you multiply the hour by 60 that won't be right.
If the phone call started at 4:00 pm and ended at 5:30 pm, you can't multiply 4 * 60 or 5 * 60. I'm simplying trying to convert this:
i have to subtract stop hour minus start hour. then i want to convert that number to minutes.
so, if the phone call started at 5:00 pm and ended at 6:45pm. it would be 105 minutes.
So, if anyone can tell me the formula, that would be great.
Thanks

5. Convert both times to military time (ie 2:45pm is 14:45 and 12:01 am is 00:01), then the formula becomes:

(hour2*60+minutes2)-(hour1*60+minutes1)

If the result is negative like if you go from 11pm to 1am, just add (24*60) to it.

6. Re: hour to minute

Originally posted by sonict
yeah i tried to put hour to minute conversion. but i can't do the formula. if you multiply the hour by 60 that won't be right.
If the phone call started at 4:00 pm and ended at 5:30 pm, you can't multiply 4 * 60 or 5 * 60. I'm simplying trying to convert this:
i have to subtract stop hour minus start hour. then i want to convert that number to minutes.
so, if the phone call started at 5:00 pm and ended at 6:45pm. it would be 105 minutes.
So, if anyone can tell me the formula, that would be great.
Thanks
You want the difference in minutes? Then you don't add the times, you subtract one from the other to get the difference:

T1 and T2 are times containing Hours and Minutes.

Difference = ((T2.Hours * 60) + T2.Minutes) - ((T1.Hours * 60) + T1.Minutes)

If you want the difference in hours and minutes:

T3.Hours = (int)(Difference / 60);
T3.Minutes = Difference % 60;

7. Re: Re: hour to minute

Originally posted by Magos
You want the difference in minutes? Then you don't add the times, you subtract one from the other to get the difference:

T1 and T2 are times containing Hours and Minutes.

Difference = ((T2.Hours * 60) + T2.Minutes) - ((T1.Hours * 60) + T1.Minutes)

If you want the difference in hours and minutes:

T3.Hours = (int)(Difference / 60);
T3.Minutes = Difference % 60;
I still don't understand T1 and T2. how can they can contain both Hours and Minutes in the same variable.

8. for the T1 and T2 stuff, easiest way is to make a struct.

but possible error in code, you only need to input ampm once. if you want to tell them it originated in the morning and ended in the afternoon, you'll need two variables. the way you have it now, it will tell them when the call ended. if it was intentional, then ignore this.

anyways, for storing time, you could do this (this should help explain the T1 & T2 stuff):

Code:
```#include <iostream>

struct time
{
int hours;
int min;
}

int main()
{
time start, end;

//cout stuff

cin >> start.hour >> start.min; //would work if no colon when given time

// or

cout << "Enter hour: ";
cin >> start.hours;
cout << "Enter min: ";
cin >> start.min;

//do stuff like that, go through the checks, and subtract

return 0;
}```

9. i just wanted to comment that variables like T1 and T2, j1, j2, num1, num2, its a bad habit. You rlly should use neumatic(sp) variables ie meaningful.

10. Originally posted by Ride -or- Die
i just wanted to comment that variables like T1 and T2, j1, j2, num1, num2, its a bad habit. You rlly should use neumatic(sp) variables ie meaningful.
I agree ROD, makes code easier to read. less confusing, i.e. if variables were called a, b, c, d, etc.

edit: although i use shorthand like min for minutes for examples...

11. i susually like to word em out, ie

hours

minutes

start_time

end_time

etc