fork download
  1. // Mariah Contreras CSC5 Chapter 4, P.226, #23
  2. //
  3. /* ************************************************************
  4.  *
  5.  * COMPUTE TIME OF SOUND
  6.  * ____________________________________________________________
  7.  * This program will display a menu of enviromental mediums and
  8.  * prompt the user for a selction. Then based upon the users
  9.  * selction it will compute the time of the sound.
  10.  *
  11.  * Computation is based on the formula :
  12.  * time = distance / speed
  13.  * ____________________________________________________________
  14.  * INPUT
  15.  * choice : Choice on menu
  16.  * distance : Distance of sound traveled
  17.  *
  18.  * OUTPUT
  19.  * time : Time of sound
  20.  *
  21.  **************************************************************/
  22. #include <iostream>
  23. #include <iomanip>
  24. using namespace std;
  25. int main ()
  26. {
  27. float const speedAir = 1100, // CONSTANT - Sound speed in Air
  28. speedWater = 4900, // CONSTANT - Sound speed in Water
  29. speedSteel = 16400; // CONSTANT - Sound speed in Steel
  30. const int airTime = 1, // CONSTANT - Menu choice 1
  31. waterTime = 2, // CONSTANT - Menu choice 2
  32. steelTime = 3, // CONSTANT - Menu choice 3
  33. quit = 4; // CONSTANT - Menu choice 4
  34.  
  35. int choice; // INPUT - Menu Choice
  36. float distance, // INPUT - Distance Speed Traveled
  37. time; // OUTPUT - Time of sound
  38. //
  39. // Display Menu and Prompt User for Choice
  40. cout << "The Speed of Sound\n";
  41. cout << "1. Calculate the Time of Sound in Air\n";
  42. cout << "2. Calculate the Time of Sound in Water\n";
  43. cout << "3. Calculate the Time of Sound in Steel\n";
  44. cout << "4. Quit\n";
  45. cout << "Enter your choice (1-4):";
  46. // Get Menu Choice and Respond
  47. cin >> choice;
  48. switch (choice)
  49. {
  50. //
  51. // Time of Sound in Air
  52. case airTime:
  53. cout << "\nEnter the distance of sound";
  54. cin >> distance;
  55. if (distance > 0)
  56. {
  57. time = distance / speedAir;
  58. cout << "\nThe time of sound is " << time;
  59. }
  60.  
  61. else
  62. {
  63. cout << "\nYou Entered an Invalid Number.Please ";
  64. cout << "\nreset the Program and enter a valid number.";
  65. }
  66. break;
  67. //
  68. // Time of Sound in Water
  69. case waterTime:
  70. cout << "\nEnter the distance of sound";
  71. cin >> distance;
  72. if (distance > 0)
  73. {
  74. time = distance / speedWater;
  75. cout << "\nThe time of sound is " << time;
  76. }
  77.  
  78. else
  79. {
  80. cout << "\nYou Entered an Invalid Number.Please ";
  81. cout << "\nreset the Program and enter a valid number.";
  82. }
  83. break;
  84. //
  85. // Time of Sound in Steel
  86. case steelTime:
  87. cout << "\nEnter the distance of sound";
  88. cin >> distance;
  89. if (distance > 0)
  90. {
  91. time = distance / speedSteel;
  92. cout << "\nThe time of sound is " << time;
  93. }
  94.  
  95. else
  96. {
  97. cout << "\nYou Entered an Invalid Number.Please ";
  98. cout << "\nreset the Program and enter a valid number.";
  99. }
  100. break;
  101. //
  102. // Quit Program
  103. case quit:
  104. cout << "\nYou've ended the program";
  105. break;
  106. default: cout << "\nMenu input invalid. Please enter a number from 1-4.\n";
  107. }
  108. return 0;
  109. }
Success #stdin #stdout 0s 5476KB
stdin
1
1100
stdout
The Speed of Sound
1. Calculate the Time of Sound in Air
2. Calculate the Time of Sound in Water
3. Calculate the Time of Sound in Steel
4. Quit
Enter your choice (1-4):
Enter the distance of sound
The time of sound is 1