fork download
  1. //Isabel Tejeda CSC5 Chapter 4, page 220, #6
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * CALCULATE DISTANCE OF THE SOUND SOURCE
  6.  * _____________________________________________________________________________
  7.  * This program will display a menu that allows the user to select one of four
  8.  * gases. The program will then take the input of seconds it took for the sound
  9.  * to travel from its source to the location it was dectected. It will then
  10.  * calculate the distance from the sound source to its detected location and
  11.  * display the result in meters.
  12.  *
  13.  * Computation is based on the formula:
  14.  * distance = speed * time
  15.  * _____________________________________________________________________________
  16.  * INPUT
  17.  * mediumChoice : The medium the user selects from the menu
  18.  * seconds : The second the sounds traveled through the medium
  19.  *
  20.  * OUTPUT
  21.  * distance : The distance the medium traveled in meters from the source of
  22.  * the sound to the location that the sound was dectected
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. int main()
  29. {
  30. int mediumChoice; // INPUT-The medium choice the user selects from the menu
  31. float time; // INPUT-The speed in meters per second that the medium travels
  32. float distance; /* OUTPUT-The distance the medium traveled in meters from the
  33. source of the sound to the location that the sound was
  34. detected*/
  35. //
  36. // Constants for Medium Speed's
  37. const float carbonDioxideSpeed = 258.0;
  38. const float airSpeed = 331.5;
  39. const float heliumSpeed = 972.0;
  40. const float hydrogenSpeed = 1270.0;
  41. //
  42. // Constants for Menu Choices
  43. const int carbonDioxideMediumChoice = 1;
  44. const int airMediumChoice =2;
  45. const int heliumMediumChoice = 3;
  46. const int hydrogenMediumChoice = 4;
  47. //
  48. // Display Menu
  49. cout << "\t\tMedium Menu" << endl;
  50. cout << "1. Carbon Dioxide" << endl;
  51. cout << "2. Air" << endl;
  52. cout << "3. Helium" << endl;
  53. cout << "4. Hydrogen" << endl;
  54. cout << "Select a Medium (Enter a number 1 - 4): " << endl;
  55. cin >> mediumChoice;
  56. //
  57. // Respond to the menu selection
  58. switch(mediumChoice)
  59. {
  60. //
  61. // Carbon Dioxide
  62. case carbonDioxideMediumChoice:
  63. cout << "Please enter the number of seconds it took for the sound "
  64. << "to travel in this medium from its source \nto the location "
  65. << "it was dectected.(Seconds must be no less than 0 seconds "
  66. << "but no more than 30 seconds): " << endl;
  67. cin >> time;
  68. if (time >= 0 && time <=30)
  69. {
  70. distance = carbonDioxideSpeed * time;
  71. cout << "The sound traveled " << distance << " meters from the "
  72. << "source of the sound to the detection location through "
  73. << "Carbon Dioxide.";
  74. }
  75. else
  76. {
  77. cout << "Seconds must be no less than 0 seconds but no more than "
  78. << "but no more than 30 seconds. Please try again.";
  79. cin >> time;
  80. }
  81. break;
  82. //
  83. // Air
  84. case airMediumChoice:
  85. cout << "Please enter the number of seconds it took for the sound "
  86. << "to travel in this medium from its source \nto the location "
  87. << "it was dectected.(Seconds must be no less than 0 seconds "
  88. << "but no more than 30 seconds): " << endl;
  89. cin >> time;
  90. if (time >= 0 && time <=30)
  91. {
  92. distance = airSpeed * time;
  93. cout << "The sound traveled " << distance << " meters from the "
  94. << "source of the sound to the detection location through "
  95. << "air.";
  96. }
  97. else
  98. {
  99. cout << "Seconds must be no less than 0 seconds but no more than "
  100. << "but no more than 30 seconds. Please try again.";
  101. cin >> time;
  102. }
  103. break;
  104. //
  105. // Helium
  106. case heliumMediumChoice:
  107. cout << "Please enter the number of seconds it took for the sound "
  108. << "to travel in this medium from its source \nto the location "
  109. << "it was dectected.(Seconds must be no less than 0 seconds "
  110. << "but no more than 30 seconds): " << endl;
  111. cin >> time;
  112. if (time >= 0 && time <=30)
  113. {
  114. distance = heliumSpeed * time;
  115. cout << "The sound traveled " << distance << " meters from the "
  116. << "source of the sound to the detection location through "
  117. << "helium.";
  118. }
  119. else
  120. {
  121. cout << "Seconds must be no less than 0 seconds but no more than "
  122. << "but no more than 30 seconds. Please try again.";
  123. cin >> time;
  124. }
  125. break;
  126. //
  127. // Hydrogen
  128. case hydrogenMediumChoice:
  129. cout << "Please enter the number of seconds it took for the sound "
  130. << "to travel in this medium from its source \nto the location "
  131. << "it was dectected.(Seconds must be no less than 0 seconds "
  132. << "but no more than 30 seconds): " << endl;
  133. cin >> time;
  134. if (time >= 0 && time <=30)
  135. {
  136. distance = hydrogenSpeed * time;
  137. cout << "The sound traveled " << distance << " meters from the "
  138. << "source of the sound to the detection location through "
  139. << "air.";
  140. }
  141. else
  142. {
  143. cout << "Seconds must be no less than 0 seconds but no more than "
  144. << "but no more than 30 seconds. Please try again.";
  145. cin >> time;
  146. }
  147. break;
  148. default :
  149. cout : "Please try again and enter a number 1 - 4..";
  150. cin >> mediumChoice;
  151. }
  152. return 0;
  153. }
  154.  
Success #stdin #stdout 0s 5384KB
stdin
1
35
1
stdout
		Medium Menu
1. Carbon Dioxide
2. Air
3. Helium
4. Hydrogen
Select a Medium (Enter a number 1 - 4): 
Please enter the number of seconds it took for the sound to travel in this medium from its source 
to the location it was dectected.(Seconds must be no less than 0 seconds but no more than 30 seconds): 
Seconds must be no less than 0 seconds but no more than but no more than 30 seconds. Please try again.