fork download
  1. // Marvyn De La Torre CS1A Chapter 5, P.294, #6
  2. /**************************************************************
  3.  * Determine Distance Traveled
  4.  *
  5.  * This program calculates and displays the distance a vehicle
  6.  * travels for each hour based on its speed and travel time.
  7.  *
  8.  *INPUT:
  9.  * The speed of the vehicle in miles per hour and the number
  10.  * of hours the vehicle has traveled.
  11.  *
  12.  * OUTPUT:
  13.  * The distance the vehicle has traveled for each hour.
  14.  **************************************************************/
  15.  
  16. #include <iostream>
  17. using namespace std;
  18.  
  19. int main()
  20. {
  21. int speed;
  22. int hours;
  23.  
  24. cout << "What is the speed of the vehicle in mph? ";
  25. cin >> speed;
  26.  
  27. while (speed < 0)
  28. {
  29. cout << "Speed cannot be negative. Enter the speed again: ";
  30. cin >> speed;
  31. }
  32.  
  33. cout << "How many hours has it traveled? ";
  34. cin >> hours;
  35.  
  36. while (hours < 1)
  37. {
  38. cout << "Hours must be at least 1. Enter the hours again: ";
  39. cin >> hours;
  40. }
  41.  
  42. cout << "\nHour Distance Traveled" << endl;
  43.  
  44. for (int hour = 1; hour <= hours; hour++)
  45. {
  46. cout << hour << " " << speed * hour << endl;
  47. }
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0.01s 5308KB
stdin
40 3
stdout
What is the speed of the vehicle in mph? How many hours has it traveled? 
Hour Distance Traveled
1    40
2    80
3    120