So I am suppose to write a program that displays a menu for allowing the user to select air, water or steel. After the user has made a selection, s(he) should be asked to enter the distance a sound wave will travel in the selected medium, then whether this distance is in feet or miles. The program will then display the amount of time it will take for sound to travel this distance (in seconds).
Medium / Speed
Air = 1,100 feet per second
Water = 4,900 feet per second
Steel = 16,400 feet per second
So this is what I got so far:
#include <iostream> #include <iomanip> using namespace std;
int main() { int choice; int distance; double amountOfTime;
const double Air = 1100; //feet per second const double Water = 4900; //feet per second const double Steel = 16400; //feet per second cout << "\t\tThe Speed of Sound \n"; cout << "Please select your medium \n\n"; cout << "1. Air \n"; cout << "2. Water \n"; cout << "3. Steel \n"; cout << "Enter your choice: "; cin >> choice; cout << "Enter the distance the sound wave will travel: "; cin >> distance; cout << setprecision (4) << fixed; cout <<
if (distance == 0) { cout << "Distance must be greater than zero\n"; } else if (choice >= 1 && choice <=3) { switch (choice) { case 1: amountOfTime = distance / Air; break; case 2: amountOfTime = distance / Water; break; case 3: amountOfTime = distance / Steel; }
cout << "The amount of time it will take to travel is "; cout << amountOfTime; cout << " seconds" << endl; } return 0; }
The program works but I still need help in writing the code to ask the user on whether if they want the distance in feet or miles within my program and with a conversion in the code that will display the result of the feet or miles that they will want inputted because so far the distance I ask the user only displays the standard of feet, thanks!
Question
Jackerman23
So I am suppose to write a program that displays a menu for allowing the user to select air, water or steel. After the user has made a selection, s(he) should be asked to enter the distance a sound wave will travel in the selected medium, then whether this distance is in feet or miles. The program will then display the amount of time it will take for sound to travel this distance (in seconds).
Medium / Speed
Air = 1,100 feet per second
Water = 4,900 feet per second
Steel = 16,400 feet per second
So this is what I got so far:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice;
int distance;
double amountOfTime;
const double Air = 1100; //feet per second
const double Water = 4900; //feet per second
const double Steel = 16400; //feet per second
cout << "\t\tThe Speed of Sound \n";
cout << "Please select your medium \n\n";
cout << "1. Air \n";
cout << "2. Water \n";
cout << "3. Steel \n";
cout << "Enter your choice: ";
cin >> choice;
cout << "Enter the distance the sound wave will travel: ";
cin >> distance;
cout << setprecision (4) << fixed;
cout <<
if (distance == 0)
{
cout << "Distance must be greater than zero\n";
}
else if (choice >= 1 && choice <=3)
{
switch (choice)
{
case 1: amountOfTime = distance / Air;
break;
case 2: amountOfTime = distance / Water;
break;
case 3: amountOfTime = distance / Steel;
}
cout << "The amount of time it will take to travel is ";
cout << amountOfTime;
cout << " seconds" << endl;
}
return 0;
}
The program works but I still need help in writing the code to ask the user on whether if they want the distance in feet or miles within my program and with a conversion in the code that will display the result of the feet or miles that they will want inputted because so far the distance I ask the user only displays the standard of feet, thanks!
Link to comment
Share on other sites
4 answers to this question
Recommended Posts