Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main()
- {
- //This program calculates the distance traveled in a vehicle from inputs by the user
- //Get miles per hour from user
- //Setup variable for speed calculation
- int milesPerHour;
- int distanceTraveled;
- cout << "Welcome to the distance calculator.";
- cout << endl;
- cout << "What is the speed of the vehicle in mph (miles per hour)?";
- cout << endl;
- cin >> milesPerHour;
- //Get hours traveled in vehicle from user
- int hours;
- cout << "For how many hours was the vehicle traveled?";
- cout << endl;
- cin >> hours;
- //Setup input validation loop statement for hours and speed
- while(hours <= 0)
- {
- //Get the input again
- cout << "You must enter at least one hour.\n";
- cout << "For how many hours has the vehicle traveled?";
- cout << endl;
- cin >> hours;
- }
- while(milesPerHour <= 0)
- {
- //Get the input again
- cout << "A negative speed is not allowed.\n";
- cout << "What is the speed of the vehicle in mph (miles per hour)?\n";
- cin >> milesPerHour;
- }
- //Calcute distance traveled with new variables and loops
- for(int i = 1; i <= hours; i++)
- {
- cout << "Hours\tDistance Traveled\n";
- cout << "------------------------\n";
- distanceTraveled = hours * milesPerHour;
- cout << setw(3) << hours << setw(6) << distanceTraveled << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement