//Maxwell Brewer CS1A Chapter 4, P. 224, #19
//
/***************************************************************
*
* CALCULATE SOUND SPEED
* ________________________________________________
*
* This program will take user input to determine
* the distance of sound traveling through a particular gas
* by using a switch statement.
* _____________________________________________
* INPUT
*
* environment, distance, time
*
* OUTPUT
*
* distance
*
***************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
// Initialization
int environment;
double distance;
double time;
// Display input prompts
cout << "In what environment is the sound travelling in?\n";
cout << "1. Carbon Dioxide\n2. Air\n3. Helium\n4. Hydrogen\n";
cout << "Enter 1, 2, 3 or 4:\n";
cin >> environment;
cout << "How many seconds did it take? (0-30)\n";
cin >> time;
// Output
if(time >= 0 && time <=30){
switch (environment){
case 1:
distance = time * 258.0;
break;
case 2:
distance = time * 331.5;
break;
case 3:
distance = time * 972.0;
break;
case 4:
distance = time * 1270.0;
break;
default:
//display error message for invalid environment
//input
cout << "Please enter an available environment!\n";
break;
}
}
else{
//display error message for negative distances
cout << "Please enter a time within given range!\n";
}
//display on screen output message
cout << "The distance from the source is ";
cout << distance << " meters.\n";
return 0;
}