1

What I have so far:

cout << "Please enter your destination travel start time (HH:MM): " << endl;
cin >>dest_startHOUR>>dest_startMIN;

cout << "Please enter your destination travel end time (HH:MM): " << endl;
cin >>dest_endHOUR>>dest_endMIN;

cout << "Please enter your home travel start time (HH:MM): " << endl;
cin >>home_startHOUR>>home_startMIN;

cout << "Please enter your home travel end time (HH:MM): " << endl << endl;
cin >>home_endHOUR>>home_endMIN;
cout << "Departed from " << home_city << " at " << dest_startHOUR << ":" << dest_startMIN << "." << endl;
cout << "Traveled " << dest_miles << " miles to " << dest_city << ", arrived at " <<     dest_endHOUR << ":" << dest_endMIN << ". " << "Travel time, " << dest_endHOUR - home_startHOUR << "." << (dest_endMIN - home_startMIN) / 60 << " hours."<< endl << endl;

And it is giving me the following output:

Please enter your destination travel start time (HH:MM): 
04:30
Please enter your destination travel end time (HH:MM): 
Please enter your home travel start time (HH:MM): 
Please enter your home travel end time (HH:MM): 
Departed from CityA at 4:0.
Traveled 200 miles to CityB, arrived at 0:0. Travel time, 0.0 hours.

But I need the output to look like:

Please enter your destination travel start time (HH:MM): 05:30
Please enter your destination travel end time (HH:MM): 07:45
Please enter your home travel start time (HH:MM): 06:15
Please enter your home travel end time (HH:MM): 08:30

Departed from CityA at 05:30.
Traveled 100 miles to CityB, arrived at 
07:45. Travel time, 2.25 hours.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user3281580
  • 53
  • 1
  • 2
  • 5

2 Answers2

2

Since you want user input on the same line as the associated prompt, get rid of endl on the ens of the prompts. You also have to ignore the line breaks entered by the user, as well as take the : characters into account.

Try this instead:

#include <iostream>
#include <string>
#include <iomanip>
#include <string>

using namespace std;

...

int dest_startHOUR, dest_startMIN;
int dest_endHOUR, dest_endMIN;
int home_startHOUR, home_startMIN;
int home_endHOUR, home_endMIN;
int dest_miles;
string home_city, dest_city;
char c;

...

cout << "Please enter your destination travel start time (HH:MM): ";
cin >> dest_startHOUR >> c >> dest_startMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Please enter your destination travel end time (HH:MM): ";
cin >> dest_endHOUR >> c >> dest_endMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Please enter your home travel start time (HH:MM): ";
cin >> home_startHOUR >> c >> home_startMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Please enter your home travel end time (HH:MM): ";
cin >> home_endHOUR >> c >> home_endMIN;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << endl;
cout << "Departed from " << home_city << " at " << setw(2) << setfill('0') << dest_startHOUR << ":" << dest_startMIN << "." << endl;
cout << "Traveled " << dest_miles << " miles to " << dest_city << ", arrived at " << setw(2) << setfill('0') << dest_endHOUR << ":" << dest_endMIN << ". " << endl;
cout << "Travel time, " << dest_endHOUR - home_startHOUR << "." << (dest_endMIN - home_startMIN) / 60 << " hours." << endl;
cout << endl;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Could you explain the reason of "cin.clear()" every time after cin>> ? Thanks. – Peng Zhang Feb 19 '14 at 04:19
  • @PengZhang: read this: http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – Remy Lebeau Feb 19 '14 at 04:40
  • So, it is just for safefy? considering that this snippet of code is interacting with user input heavily. – Peng Zhang Feb 19 '14 at 04:48
  • The `clear()` is for error handling, but the `ignore()` is necessary since you are not swallowing the user's ENTER keystroke when you are reading the hour/minute values. – Remy Lebeau Feb 19 '14 at 19:26
2

You get wrong results for this reasons:

  • You put endl after each prompt message which makes the cursor moves to the next line before you get the response from a user.
  • When cin >> dest_startHOUR >> dest_startMIN; is executed, your program expects two integer variables (i.e., hours and minutes) from the user. However, after the user inputs the colon :, the program considers this as a value for the minutes. Thus, your program's execution precedes and gives you incorrect results.
  • You should have (dest_endHOUR - dest_startHOUR) to compute the departure travel time to your destination, not (dest_endHOUR - home_startHOUR).
  • To get the fraction of an hour for the departure travel time to your destination, you should change your expression to ((dest_endMIN - dest_startMIN) / 60.0 * 100). You need to put .0 after 60 to avoid integer division, and you need to multiply the result by 100 to convert it to a fraction of an hour. However, it makes sense to print out the travel time in the format HH:MM.
  • Your program works correctly if you consider only 24 hours format and the trip is started in the morning and finished in the evening the same day. Otherwise, you need to enhance your code to consider this issue.

Bellow is the revised version of your program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int dest_startHOUR, dest_startMIN;
    int dest_endHOUR, dest_endMIN;
    int home_startHOUR, home_startMIN;
    int home_endHOUR, home_endMIN;
    int dest_miles = 200;
    string home_city = "CityA", dest_city = "CityB";
    char ch;

    cout << "Please enter your destination travel start time (HH:MM): ";
    cin >> dest_startHOUR >> ch >> dest_startMIN;

    cout << "Please enter your destination travel end time (HH:MM): ";
    cin >> dest_endHOUR >> ch >> dest_endMIN;

    cout << "Please enter your home travel start time (HH:MM): ";
    cin >> home_startHOUR >> ch >> home_startMIN;

    cout << "Please enter your home travel end time (HH:MM): ";
    cin >> home_endHOUR >> ch >> home_endMIN;

    cout << "Departed from " << home_city << " at " << dest_startHOUR << ":" << dest_startMIN << "." << endl;
    cout << "Traveled " << dest_miles << " miles to " << dest_city 
        << ", arrived at " << dest_endHOUR << ":" << dest_endMIN << ". " 
        << "Travel time, " << dest_endHOUR - dest_startHOUR << "." << (dest_endMIN - dest_startMIN) / 60.0 * 100 
        << " hours." << endl << endl;

    return 0;
}

Here you find a running sample for the revised program:

Please enter your destination travel start time (HH:MM): 05:30
Please enter your destination travel end time (HH:MM): 07:45
Please enter your home travel start time (HH:MM): 06:15
Please enter your home travel end time (HH:MM): 08:30
Departed from CityA at 5:30.
Traveled 200 miles to CityB, arrived at 7:45. Travel time, 2.25 hours.
fayez
  • 71
  • 2