Thread: conversion

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    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;
    }

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Unhappy please help me!

    Please help me

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    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. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    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;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    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. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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. #11
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i susually like to word em out, ie

    hours

    minutes

    start_time

    end_time

    etc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM