//Isabel Tejeda CSC5 Chapter 4, page 220, #6
//
/*******************************************************************************
*
* CALCULATE DISTANCE OF THE SOUND SOURCE
* _____________________________________________________________________________
* This program will display a menu that allows the user to select one of four
* gases. The program will then take the input of seconds it took for the sound
* to travel from its source to the location it was dectected. It will then
* calculate the distance from the sound source to its detected location and
* display the result in meters.
*
* Computation is based on the formula:
* distance = speed * time
* _____________________________________________________________________________
* INPUT
* mediumChoice : The medium the user selects from the menu
* seconds : The second the sounds traveled through the medium
*
* OUTPUT
* distance : The distance the medium traveled in meters from the source of
* the sound to the location that the sound was dectected
*
******************************************************************************/
#include <iostream>
using namespace std;
int main()
{
int mediumChoice; // INPUT-The medium choice the user selects from the menu
float time; // INPUT-The speed in meters per second that the medium travels
float distance; /* OUTPUT-The distance the medium traveled in meters from the
source of the sound to the location that the sound was
detected*/
//
// Constants for Medium Speed's
const float carbonDioxideSpeed = 258.0;
const float airSpeed = 331.5;
const float heliumSpeed = 972.0;
const float hydrogenSpeed = 1270.0;
//
// Constants for Menu Choices
const int carbonDioxideMediumChoice = 1;
const int airMediumChoice =2;
const int heliumMediumChoice = 3;
const int hydrogenMediumChoice = 4;
//
// Display Menu
cout << "\t\tMedium Menu" << endl;
cout << "1. Carbon Dioxide" << endl;
cout << "2. Air" << endl;
cout << "3. Helium" << endl;
cout << "4. Hydrogen" << endl;
cout << "Select a Medium (Enter a number 1 - 4): " << endl;
cin >> mediumChoice;
//
// Respond to the menu selection
switch(mediumChoice)
{
//
// Carbon Dioxide
case carbonDioxideMediumChoice:
cout << "Please enter the number of seconds it took for the sound "
<< "to travel in this medium from its source \nto the location "
<< "it was dectected.(Seconds must be no less than 0 seconds "
<< "but no more than 30 seconds): " << endl;
cin >> time;
if (time >= 0 && time <=30)
{
distance = carbonDioxideSpeed * time;
cout << "The sound traveled " << distance << " meters from the "
<< "source of the sound to the detection location through "
<< "Carbon Dioxide.";
}
else
{
cout << "Seconds must be no less than 0 seconds but no more than "
<< "but no more than 30 seconds. Please try again.";
cin >> time;
}
break;
//
// Air
case airMediumChoice:
cout << "Please enter the number of seconds it took for the sound "
<< "to travel in this medium from its source \nto the location "
<< "it was dectected.(Seconds must be no less than 0 seconds "
<< "but no more than 30 seconds): " << endl;
cin >> time;
if (time >= 0 && time <=30)
{
distance = airSpeed * time;
cout << "The sound traveled " << distance << " meters from the "
<< "source of the sound to the detection location through "
<< "air.";
}
else
{
cout << "Seconds must be no less than 0 seconds but no more than "
<< "but no more than 30 seconds. Please try again.";
cin >> time;
}
break;
//
// Helium
case heliumMediumChoice:
cout << "Please enter the number of seconds it took for the sound "
<< "to travel in this medium from its source \nto the location "
<< "it was dectected.(Seconds must be no less than 0 seconds "
<< "but no more than 30 seconds): " << endl;
cin >> time;
if (time >= 0 && time <=30)
{
distance = heliumSpeed * time;
cout << "The sound traveled " << distance << " meters from the "
<< "source of the sound to the detection location through "
<< "helium.";
}
else
{
cout << "Seconds must be no less than 0 seconds but no more than "
<< "but no more than 30 seconds. Please try again.";
cin >> time;
}
break;
//
// Hydrogen
case hydrogenMediumChoice:
cout << "Please enter the number of seconds it took for the sound "
<< "to travel in this medium from its source \nto the location "
<< "it was dectected.(Seconds must be no less than 0 seconds "
<< "but no more than 30 seconds): " << endl;
cin >> time;
if (time >= 0 && time <=30)
{
distance = hydrogenSpeed * time;
cout << "The sound traveled " << distance << " meters from the "
<< "source of the sound to the detection location through "
<< "air.";
}
else
{
cout << "Seconds must be no less than 0 seconds but no more than "
<< "but no more than 30 seconds. Please try again.";
cin >> time;
}
break;
default :
cout : "Please try again and enter a number 1 - 4..";
cin >> mediumChoice;
}
return 0;
}