C++ Convert

Discussion in 'Programming' started by scottlpool2003, Oct 12, 2008.

  1. #1
    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
     
    scottlpool2003, Oct 12, 2008 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    Here's what I have so far...

    //A Scott Brade incorporated program
    //10/10/08
    
    #include <iostream.h>
    #include<stdio.h>
    
    double mins,S_TIME,E_TIME,totmins,fullcost,actualcost,discount,day1,time;
    char day;
    
    
    
    void parking()
    
    
    //days menu
    
    
    {
    {
    cout<<"+----------------------------+\n";
    cout<<"|          M = Monday        |\n";
    cout<<"|          U = Tuesday       |\n";
    cout<<"|          W = Wednesday     |\n";
    cout<<"|          T = Thursday      |\n";
    cout<<"|          F = Friday        |\n";
    cout<<"|          A = Saturday      |\n";
    cout<<"|          S = Sunday        |\n";
    cout<<"+----------------------------+\n\n";
    }
    
    
    
    //user input
    
    
    
    
    cout<<"            +---------------------------------------+\n";
    cout<<"            |            Input Section              |\n";
    cout<<"            +---------------------------------------+\n\n\n";
    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;
    }
    
    cout<<"\n\n";
    cout<<"What day is it?\n\n";
    
    cin>>day;
    cout<<"\n\n";
    cout<<"You entered ";
    cout<<day;
    cout<<" for ";
    
    
    //call day
    
    
    if (day =='M') { cout<<"Monday";}
    else if (day =='U') { cout<<"Tuesday";}
    else if (day =='W') { cout<<"Wednesday";}
    else if (day =='T') { cout<<"Thursday";}
    else if (day =='F') { cout<<"Friday";}
    else if (day =='A') { cout<<"Saturday";}
    else if (day =='S') { cout<<"Sunday";}
    	
    
    
    //calculations section
    
    
    
    cout<<"\n\n";
    cout<<"            +---------------------------------------+\n";
    cout<<"            |            Calculations               |\n";
    cout<<"            +---------------------------------------+\n\n\n";
    
    cout<<"+----------------------------+\n";
    cout<<"|   Hourly rate:             |\n";
    cout<<"|   --------------------     |\n";
    cout<<"|   Start time:              |\n";
    cout<<"|   --------------------     |\n";
    cout<<"|   End time:                |\n";
    cout<<"|   --------------------     |\n";
    cout<<"|   Total duration:          |\n";
    cout<<"|   --------------------     |\n";
    cout<<"|   Full call cost:          |\n";
    cout<<"|   --------------------     |\n";
    cout<<"|   Discount:                |\n";
    cout<<"|   --------------------     |\n";
    cout<<"|   Actual call cost:        |\n";
    cout<<"+----------------------------+\n\n";
    
    }
    
    void main()
    
    {
    
         
    
    	parking();
    
    
    
    }
    PHP:
    I understand all of it apart from the function I've just found:

    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;
    }
    
    PHP:
    If someone can help me understand it, I'm sure I'll manage. :)
     
    scottlpool2003, Oct 12, 2008 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    Thanks anyway guys, I managed to solve it.

    See code below:

    //A program to get call costs,durations,times
    
    
    # include <iostream.h>
    # include <stdio.h>
    # include <ctype.h>
    
    //Variables Declaration Section.
    
    
    char Day,M,U,W,T,F,A,S,Weekday,START_TIME[5],END_TIME[5];
    
    
    int StartTime,EndTime,HoursStartTime,MinutesStartTime,HoursEndTime,MinutesEndTime,TotalCallTime;
    
    
    
    int convert (char time [5])
    
    
    
    //Convert time
    
    
    {
    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;
    }
    
    
    
    
    //Start/End Time
    
    void GetTimes()
    {
    
    cout<< "Please enter call start time:\n\n";
    cin>>START_TIME;
    cout<<"\n\n";
    cout<< "Please enter call end time:\n\n";
    cin>>END_TIME;
    cout<<"\n\n";
    StartTime=convert(START_TIME);
    EndTime=convert(END_TIME); 
    }
    
    //This Section Calculates The Start Time Of The Call. Works
    void CalculateStartTime()
    {
    HoursStartTime=StartTime/100;
    MinutesStartTime=StartTime%100;
    MinutesStartTime =((HoursStartTime*60)+MinutesStartTime);
    }
    
    //This Section Calculates The End Time Of The Call. Works
    
    void CalculateEndTime()
    {
    
    HoursEndTime=EndTime/100;
    MinutesEndTime=EndTime%100;
    MinutesEndTime =((HoursEndTime*60)+MinutesEndTime);
    TotalCallTime=(MinutesEndTime-MinutesStartTime);
    	{
    cout<<"            +---------------------------------------+\n";
    cout<<"            |            Results Section            |\n";
    cout<<"            +---------------------------------------+\n\n\n\n\n";
    }
    cout<<"+--------------------------+\n";
    cout<<"| Start Time: ";cout<<START_TIME;cout<<"         |\n";
    cout<<"| End Time: ";cout<<END_TIME;cout<<"           |\n";
    cout<<"| Duration: ";cout<<TotalCallTime;cout<<" mins        |\n";
    cout<<"+--------------------------+\n\n\n";
    
    
    //Error loop
    if (TotalCallTime <600)
    {
    StartTime=convert(START_TIME);
    EndTime=convert(END_TIME);
    }
    else
    if(TotalCallTime !=600)
    {
    cout<<"Error, call time can't be more than 10 hours.\nPlease try again.\n\n";
    cout<< "Enter call start time\n\n";
    cin>>START_TIME;
    cout<< " Enter call end time\n\n";
    cin>>END_TIME;
    StartTime=convert(START_TIME);
    EndTime=convert(END_TIME);
    }
    
    }
    
    //This Section Displays How To Input The Days Of The Week Correctly. Works
    
    void GetDay()
    { 
    Weekday = 'M','U','W','T','F','A','S';
    
    
    
    {
    cout<<"+----------------------------+\n";
    cout<<"|          M = Monday        |\n";
    cout<<"|          U = Tuesday       |\n";
    cout<<"|          W = Wednesday     |\n";
    cout<<"|          T = Thursday      |\n";
    cout<<"|          F = Friday        |\n";
    cout<<"|          A = Saturday      |\n";
    cout<<"|          S = Sunday        |\n";
    cout<<"+----------------------------+\n\n";
    }
    	{
    cout<<"            +---------------------------------------+\n";
    cout<<"            |            Input Section              |\n";
    cout<<"            +---------------------------------------+\n\n\n";
    }
    
    cout<<"What day is it?\n\n";
    
    cin>>Day;
    cout<<"\n\n";
    Day=toupper(Day); //Converts Lowercase To Uppercase.
    
    
    
    cout<<"You entered ";
    cout<<Day;
    cout<<" for ";
    
    if (Day =='M') { cout<<"Monday\n\n";}
    else if (Day =='U') { cout<<"Tuesday\n\n";}
    else if (Day =='W') { cout<<"Wednesday\n\n";}
    else if (Day =='T') { cout<<"Thursday\n\n";}
    else if (Day =='F') { cout<<"Friday\n\n";}
    else if (Day =='A') { cout<<"Saturday\n\n";}
    else if (Day =='S') { cout<<"Sunday\n\n";}
    else {cout<<"Error day is incorrect\n\n";}
    
    
    }
    
    
    
    
    
    
    
    
    //This Section Calls All The Functions.
    
    void main()
    
    {
    GetDay();
    GetTimes();
    CalculateStartTime();
    CalculateEndTime();
    }
    PHP:
     
    scottlpool2003, Oct 12, 2008 IP