Here is my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <time.h>
#include <assert.h>


using namespace std;

int main()
{
// = (num3&&num4) - (num1&&num2); 
// save this calc for later: hrIn + (hrOut/60.0);
// same thing = num3 + (num4/60);
// write an alogorithm to establish the steps to take to solve the problem
//(1) Declare some variables to work with
char fare;
char choice = 'y';
int hrIn, minIn, hrOut, minOut; 
float hours; 
float minutes;
float total_minutes; 
double cost;
float charge;


while (choice == 'y')
{
//(2) OUtput reqd info and user message

cout << "This program will calculate a single "
<< "\nfare amount which is then owed by the customer for the trip.\n"
<< "\nTo calculate a single fare amount, input S (or s)"
<< " \n\nInput S, s: ";
cin >> fare;

if(fare=='S'||fare=='s')
{
cout << "\nWhat hour did they depart? ";
cin >> hrOut;
cout << "\nWhat minute did they depart? ";
cin >> minOut;
cout << "\nYou entered for departure: " << hrOut << ":" << minOut << endl;
cout << "\nWhat hour did they arrive? ";
cin >> hrIn;
cout << "\nWhat minute did they arrive? ";
cin >> minIn; 
cout << "\nYou entered for arrival: " << hrIn << ":" << minIn << "\n" << endl;
cout << "A rickshaw departed at " << hrOut << ":" << minOut
<< " and arrived at " << hrIn << ":" << minIn << " with a single customer.\n" << endl;

float hours = hrIn - hrOut;
cout << "You arrived in " << hours << " hour(s)";
float minutes = minIn - minOut;
cout << " and " << minutes << " minute(s)";
float total_minutes = (hours * 60) + minutes; 
cout << ", or in " << total_minutes << " minutes.\n" << endl;


system("pause");
return 0; 
}

***Output

This program will calculate a single fare amount which is then owed by the customer for the trip

To calculate a single fare amount, input S (or s)

Input S, s: s

What hour did they depart? 11

What minute did they depart? 30

You entered for departure: 11:30

What hour did they arrive? 1

What minute did they arrive? 21

A rickshaw departed at 11:30 and arrived at 1:21 with a single customer.

You arrived in -10 hour(s) and -9 minute(s), or in -609 minutes.

How do I write it that it would be 1 hour and 51 minutes instead of minus hours. I am trying to input the time as AM or PM but I do not know how to.

Please Help!

Edited 5 Years Ago by WaltP: Added CODE Tags -- please use them

The first problem is that you do not specify AM or PM.
You should let the user input AM or PM like this:

string ampm;
cin >> hrOut >> ampm;
if (ampm == "pm") hrOut += 12;

Then you will have the time in 24 hour format (0-23).
If hours then is negative this means you should add 24 to it.
If min is negative you should subtract 1 from hours and add 60 to minutes.

Or

You can translate hours into minutes and then simply add minutes to
it.

Hope I got that right..

/rxlim

This article has been dead for over six months. Start a new discussion instead.