//Saliha Babar CS1A Chapter 5, Page 294, #6
//
/************************************************************************
*
* CALCULATE DISTANCE TRAVELED
* ______________________________________________________________________
* This program allows user to enter speed of vehicle in mph, how many
* hours the vehicle has traveled and the program displays distance
* traveled in each hour.
*
* Calculation is based on the formula
* distance = speed * time;
*________________________________________________________________________
* INPUT
* speedMPH : speed of the vehicle in meters per hour
* hoursTraveled : hours traveled by a car
*
* OUTPUT
* distance : distance traveled by a car in meters
* *********************************************************************/
#include <iostream>
using namespace std;
int main() {
int speedMPH; // INPUT - speed of the vehicle in MPH
int hoursTraveled; // INPUT - hours traveled by the vehicle
int hours; // COUNTER VARIABLE - for hours traveled
int distanceTraveled; // OUTPUT - distance traveled
// Get the user input for speed
cout << "What is the speed of the vehicle in mph ?\n";
cin >> speedMPH;
// Validate user input for speed
while (speedMPH <= 0)
{
cout << "Enter only positive number of the speed\n";
cin >> speedMPH;
}
// Get the hours of vehicle traveled
cout << "How many hours has it traveled?\n";
cin >> hoursTraveled;
// Validate user input for hours
while ( hoursTraveled < 1 )
{
cout << "Only enter number that is greater than 1 for hours\n";
}
cout << "Hour Distance traveled\n";
cout << "--------------------------\n";
for ( hours = 1 ; hours <= hoursTraveled ; hours++ )
{
distanceTraveled = hours * speedMPH;
cout << hours << "\t\t" << distanceTraveled << endl;
}
return 0;
}