// Mariah Contreras CSC5 Chapter 4, P.226, #23
//
/* ************************************************************
*
* COMPUTE TIME OF SOUND
* ____________________________________________________________
* This program will display a menu of enviromental mediums and
* prompt the user for a selction. Then based upon the users
* selction it will compute the time of the sound.
*
* Computation is based on the formula :
* time = distance / speed
* ____________________________________________________________
* INPUT
* choice : Choice on menu
* distance : Distance of sound traveled
*
* OUTPUT
* time : Time of sound
*
**************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
float const speedAir = 1100, // CONSTANT - Sound speed in Air
speedWater = 4900, // CONSTANT - Sound speed in Water
speedSteel = 16400; // CONSTANT - Sound speed in Steel
const int airTime = 1, // CONSTANT - Menu choice 1
waterTime = 2, // CONSTANT - Menu choice 2
steelTime = 3, // CONSTANT - Menu choice 3
quit = 4; // CONSTANT - Menu choice 4
int choice; // INPUT - Menu Choice
float distance, // INPUT - Distance Speed Traveled
time; // OUTPUT - Time of sound
//
// Display Menu and Prompt User for Choice
cout << "The Speed of Sound\n";
cout << "1. Calculate the Time of Sound in Air\n";
cout << "2. Calculate the Time of Sound in Water\n";
cout << "3. Calculate the Time of Sound in Steel\n";
cout << "4. Quit\n";
cout << "Enter your choice (1-4):";
// Get Menu Choice and Respond
cin >> choice;
switch (choice)
{
//
// Time of Sound in Air
case airTime:
cout << "\nEnter the distance of sound";
cin >> distance;
if (distance > 0)
{
time = distance / speedAir;
cout << "\nThe time of sound is " << time;
}
else
{
cout << "\nYou Entered an Invalid Number.Please ";
cout << "\nreset the Program and enter a valid number.";
}
break;
//
// Time of Sound in Water
case waterTime:
cout << "\nEnter the distance of sound";
cin >> distance;
if (distance > 0)
{
time = distance / speedWater;
cout << "\nThe time of sound is " << time;
}
else
{
cout << "\nYou Entered an Invalid Number.Please ";
cout << "\nreset the Program and enter a valid number.";
}
break;
//
// Time of Sound in Steel
case steelTime:
cout << "\nEnter the distance of sound";
cin >> distance;
if (distance > 0)
{
time = distance / speedSteel;
cout << "\nThe time of sound is " << time;
}
else
{
cout << "\nYou Entered an Invalid Number.Please ";
cout << "\nreset the Program and enter a valid number.";
}
break;
//
// Quit Program
case quit:
cout << "\nYou've ended the program";
break;
default: cout << "\nMenu input invalid. Please enter a number from 1-4.\n";
}
return 0;
}