Skip to content

Instantly share code, notes, and snippets.

@nerdyworm
Created February 2, 2012 22:33
Show Gist options
  • Save nerdyworm/1726216 to your computer and use it in GitHub Desktop.
Save nerdyworm/1726216 to your computer and use it in GitHub Desktop.
//Mike Schmitt
//calculates the amount of time it will take to travel a given distance at a given speed.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
//Input Data
string first;
string last;
float miles = 0; //Distance in Miles
float miles_per_hour = 0; //Speed in MPH
int hours = 0; //Time in Hours
int minutes = 0; //Time in Minutes
int seconds = 0;
int total_seconds = 0; //Total time in Minutes
//Collect Data
cout << "Please use the following program to figure out how long it will take you travel any distance at any average speed. Input should be rounded to the nearest whole number." << endl << endl;
cout << "Please enter your first and last name: ";
cin >> first >> last;
cout << "Please enter your average speed in Miles Per Hour: ";
cin >> miles_per_hour;
cout << "Please enter the distance in miles you are going to travel: ";
cin >> miles;
//Calculations
total_seconds = (miles / miles_per_hour ) * 3600;
hours = total_seconds / 3600;
minutes = ((total_seconds) - (hours * 3600)) / 60;
seconds = ((total_seconds) - ((hours * 3600) + (minutes *60)));
//Display Data
cout << endl << "Hello " << first << " " << last << ". It will take you " << hours << " hour(s), " << minutes << " minute(s) and " << seconds << " second(s) to travel " << miles << " mile(s) at " << miles_per_hour << " mph." << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment