fork download
  1. //Saliha Babar CS1A Chapter 4, Page 223, #18
  2. //
  3. /************************************************************************
  4.  *
  5.  * CALCULATE TIME FOR A SUBSTANCE TO TRAVEL
  6.  * ______________________________________________________________________
  7.  * This program allows user to choose from 3 substances and calculates the
  8.  * time taken of that substance to travel.
  9.  *
  10.  * Calculation is based on the formula
  11.  * time taken = distance / speed
  12.  *________________________________________________________________________
  13.  * INPUT
  14.  * choice : user choice from 3 substances
  15.  *
  16.  * OUTPUT
  17.  * time taken : time taken for the substance to travel
  18.  * *********************************************************************/
  19. #include <iostream>
  20. #include <iomanip>
  21. using namespace std;
  22. int main() {
  23.  
  24. int choice; // INPUT - user choice of substance
  25. double distance; // INPUT - distance entered
  26. int const airSpeed = 1100; // INPUT - constant speed of air
  27. int const waterSpeed = 4900; // INPUT - constant speed of water
  28. int const steelSpeed = 16400; // INPUT - constant speed of steel
  29. double time; // OUTPUT - time taken to travel
  30.  
  31. cout << "Choose a selection of substance\n";
  32. cout << "1 - air\n";
  33. cout << "2 - water\n";
  34. cout << "3 - steel\n";
  35. cin >> choice;
  36.  
  37. if ( choice >= 1 && choice <=3)
  38. {
  39. cout << "Now, enter a distance that the substance travel in meters .\n";
  40. cin >> distance;
  41.  
  42. if (distance > 0)
  43. {
  44. cout << fixed << showpoint << setprecision(4);
  45. switch (choice)
  46. {
  47. case 1 :
  48. time = distance / airSpeed ;
  49. cout << "Time taken is " << time << " seconds\n";
  50. break;
  51.  
  52. case 2 :
  53. time = distance / waterSpeed ;
  54. cout << "Time taken is " << time << " seconds\n";
  55. break;
  56.  
  57. case 3 :
  58. time = distance / steelSpeed ;
  59. cout << "Time taken is " << time << " seconds\n";
  60. break;
  61. }
  62. }
  63.  
  64. else
  65. {
  66. cout << "Only enter positive distance. Run the program again\n";
  67. }
  68. }
  69.  
  70. else
  71. {
  72. cout << "Invalid Choice, run the program again with numbers 1-3\n" ;
  73. }
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0s 5280KB
stdin
45
stdout
Choose a selection of substance
1 - air
2 - water
3 - steel
Invalid Choice, run the program again with numbers 1-3