sxsa
sxsa

Reputation: 11

C++ decimal to time conversion issue

My assignment requires me to convert a time into minutes, but the time must be inserted like HH.MM, with a decimal point in between. So 9:25am would be input into the program as 9.25. However, the problem is that 9.25 is not equal to 9.25am, it is instead equal to 9:15am. I have a second variable that converts the 9.25 into an integer form, removing the .25 from the 9.

I have tried converting 9:25 into minutes by multiplying 9.25 * 60, but again will eventually result in the wrong answer, because 9.25 does not equal 9:25am.

    cout << "Enter the starting hour of the call 
    cin >> startTime;

    cout << "Enter the total number of minutes for this call: ";
    cin >> minutes;


    int getwin = startTime; 
    int startHH = getwin / 60; 
    int startMM = getwin % 60;

I am supposed to be able to output the start time of a phone call, add minutes to that phone call, and output the end time of that phone call. So for example: START TIME: 9.25 CALL LENGTH: 180 MINUTES END TIME: 12.25

Upvotes: 0

Views: 105

Answers (1)

Dr Phil
Dr Phil

Reputation: 880

Your problem is that you fixate on the "decimal" point. It is not a decimal point, it is a delimiter. You can either input the whole string and parse the hours and minutes separately (the intuitive way) or use cin to input a floating point number say like the code below:

   double input;
   cin >>input;
   int h = (int) input; // the integer part is hours
   double mm = input - h; //the non-integer part is minutes
   mm*=100;// the minutes are less than one ,we have to multiply by 100
   int m = (int) m;

   cout <<h<<":"<<m<<endl;

Upvotes: 1

Related Questions