daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Mar 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {  
  9.   //This program calculates the distance traveled in a vehicle from inputs by the user
  10.  
  11.   //Get miles per hour from user
  12.   //Setup variable for speed calculation
  13.   int milesPerHour;
  14.   int distanceTraveled;
  15.  
  16.  
  17.   cout << "Welcome to the distance calculator.";
  18.   cout << endl;
  19.   cout << "What is the speed of the vehicle in mph (miles per hour)?";
  20.   cout << endl;
  21.   cin >> milesPerHour;
  22.  
  23.  
  24.  
  25.   //Get hours traveled in vehicle from user
  26.   int hours;
  27.  
  28.   cout << "For how many hours was the vehicle traveled?";
  29.   cout << endl;
  30.   cin >> hours;
  31.  
  32.   //Setup input validation loop statement for hours and speed
  33.  
  34.   while(hours <= 0)
  35.   {
  36.   //Get the input again
  37.   cout << "You must enter at least one hour.\n";
  38.   cout << "For how many hours has the vehicle traveled?";
  39.   cout << endl;
  40.   cin >> hours;
  41.  
  42.   }
  43.  
  44.   while(milesPerHour <= 0)
  45.   {
  46.   //Get the input again
  47.   cout << "A negative speed is not allowed.\n";
  48.   cout << "What is the speed of the vehicle in mph (miles per hour)?\n";
  49.   cin >> milesPerHour;
  50.  
  51.  
  52.   }
  53.   //Calcute distance traveled with new variables and loops
  54.   for(int i = 1; i <= hours; i++)
  55.   {
  56.  
  57.     cout << "Hours\tDistance Traveled\n";
  58.   cout << "------------------------\n";
  59.  
  60.    distanceTraveled = hours * milesPerHour;
  61.    cout << setw(3) << hours << setw(6) << distanceTraveled << endl;
  62.  
  63.  
  64.  
  65.  
  66.   }
  67.  
  68.  
  69.  
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top