Hi guys, this is a question from my homework:
The speed of sound depends on the material the sound is passing through. Below is the approximate speed of sound (in feet per second) for air, water and steel:
air: 1,100 feet per second
water: 4,900 feet per second
steel: 16,400 feet per second
Write a program that displays a menu allowing the user to select air, water, or steel. After the user has made a selection, he or she should be asked to enter the distance a sound wave will travel in the selected medium. The program will then display the amount of time it will take.
Menu. The menu should look exactly like this:
Select a medium: 1. Air 2. Water 3. Steel
Enter your choice:
Prompts And Output Labels. After the user chooses the medium, prompt for distance simply with the string "Enter the distance: ". After each calculation, your output should be of the form "A sound wave takes T seconds to travel D feet in M." where T is the time you calculated to 4 digits of precision, D is the distance entered and M is the medium (air/water/steel) selected.
Input Validation. The program should validate both the menu choice and the distance entered. If the menu choice is not 1 or 2 or 3 then the program prints "The valid choices are 1 through 3. Run the program again and select one of those." and terminates. The distance must not be negative-- otherwise the program prints out the message "Distance must be greater than zero." and terminates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <iomanip>
using namespace std ;
int main ()
{
const int A = 1100;
const int W = 4900;
const int S = 16400;
double distance;
int choice;
cout << "Select a medium:\n";
cout << "1. Air\n";
cout << "2. Water\n";
cout << "3. Steel\n";
cout << "\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice > 0 && choice < 4)
{
cout << "Enter the distance: \n";
cin >> distance;
if (distance > 0)
{
cout << "A sound wave takes ";
cout << fixed << setprecision(4);
switch (choice)
{
case 1: cout << distance / A << " seconds to travel " << distance << " feet through air.\n";
break;
case 2: cout << distance / W << " seconds to travel " << distance << " feet through water.\n";
break;
case 3: cout << distance / S << " seconds to travel " << distance << " feet through steel.\n";
break;
}
}
else
{
cout << "Distance must be greater than zero.";
}
}
else
{
cout << "The valid choices are 1 through 3. Run the program again and select one of those.";
}
return 0;
}
|
This is the code that I entered, sorry if it's messy. It says "Standard output was not expected". What does this mean? I tested this in Visual Studio and there wasn't anything wrong. :O