Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Logical Error: I included a picture below. The question is: 4.18:The Speed o

ID: 668848 • Letter: C

Question

C++ Logical Error: I included a picture below.

The question is: 4.18:The Speed of Sound
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.

The Code I used is:

#include
#include
using namespace std;

int main()
{
    const int AIRSPEED=1100;
    const int WATERSPEED=4900;
    const int STEELSPEED=16400;

    int soundChoice;
    double distance;

    cout << "Select a medium: ";
    cout << "1. Air ";
    cout << "2. Water ";
    cout << "3. Steel ";
    cout << " ";
    cout << "Enter your choice: ";
    cin >> soundChoice;
    
    if (soundChoice>=1 && soundChoice <=3)
    {
        cout << "Enter the distance: ";
        cin >> distance;
        if (distance > 0.0)
        {
            cout << "A sound wave takes ";
            cout << fixed << setprecision(4);
            switch (soundChoice)
            {
                case 1:
                    cout << distance / AIRSPEED << " seconds to travel " << distance << " feet through air.";
                    break;
                case 2:
                    cout << distance / WATERSPEED << " seconds to travel " << distance << " feet through water.";
                    break;
                case 3:
                    cout << distance / STEELSPEED << " seconds to travel " << distance << " feet through steel.";
                    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;
}

Explanation / Answer

Your answers is working perfectly fine

i didn't got what error you are getting

see the code i compiled , and output below

comment if you still got any error

#include <iostream>
#include<iomanip>
using namespace std;

int main()
{
const int AIRSPEED=1100;
const int WATERSPEED=4900;
const int STEELSPEED=16400;

int soundChoice;
double distance;

cout << "Select a medium: ";
cout << "1. Air ";
cout << "2. Water ";
cout << "3. Steel ";
cout << " ";
cout << "Enter your choice: ";
cin >> soundChoice;
  
if (soundChoice>=1 && soundChoice <=3)
{
cout << "Enter the distance: ";
cin >> distance;
if (distance > 0.0)
{
cout << "A sound wave takes ";
cout << fixed << setprecision(4);
switch (soundChoice)
{
case 1:
cout << distance / AIRSPEED << " seconds to travel " << distance << " feet through air.";
break;
case 2:
cout << distance / WATERSPEED << " seconds to travel " << distance << " feet through water.";
break;
case 3:
cout << distance / STEELSPEED << " seconds to travel " << distance << " feet through steel.";
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;
}

OUTPUT:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote