Is this a good pseudocode and can you help me write this, I am kind of new to programming.

if (choice > 0 && choice < 4)  //choice is between 0 and 4
{
cout << "Enter the distance: \n"; //displays the distance 
cin >> distance;                  //prints the distance

if (distance > 0)
{
cout << "A sound wave takes ";     //displays the amount of time it takes a sound wave to travel 
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.";  //displays that distance must be greater than zero
}
}
else 
{
cout << "The valid choices are 1 through 3. Run the program again and select one of those.";
}    //choice must be either 1,2,3 and if not, run the program again

system("pause"); //pause
return 0;
}

Dani AI

Generated

— this thread mixes actual C++ and a textbook-style exercise. is right that what you posted is C++, while gave a useful high-level pseudocode. Below are practical, concrete improvements you can apply to turn that sketch into robust, readable C++ that handles real input and edge cases.

Suggested plan (small, testable steps)

  1. Use named constants for speeds (makes the math readable and easy to change).
  2. Read raw lines (std::getline) and parse them; this avoids leftover input errors when users type letters.
  3. Validate both the choice (must be 1–3) and distance (must be numeric and > 0). Loop until valid input.
  4. Put the travel-time calculation in a small function and format the output with std::fixed/std::setprecision.
  5. Avoid nonportable calls like system("pause"); let the environment/IDE control program termination.

Example of a compact, robust approach (illustrative)

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

constexpr double SPEED_AIR_FPS   = 1100.0;   // approximate, ft/s
constexpr double SPEED_WATER_FPS = 4900.0;
constexpr double SPEED_STEEL_FPS = 16400.0;

double travel_time(double distance, int medium) {
    switch (medium) {
        case 1: return distance / SPEED_AIR_FPS;
        case 2: return distance / SPEED_WATER_FPS;
        default: return distance / SPEED_STEEL_FPS;
    }
}

/* main reads/validates input with getline and stringstreams, then prints time */

Testing and notes

  • Try non-numeric input, zero, and negative distances to confirm validation.
  • The numeric speed constants are approximations; real speed depends on temperature and medium composition.
  • Keep functions small and name things clearly (e.g., SPEED_AIR_FPS) — it helps both readability and grading.

Recommended Answers

All 4 Replies

What, and where is the problem in the code? Mention the problem you facing so that we can help you to solve the issue.

that's not pseudocode at all, that's poorly written C++.

The variables like distance, choice should be declared when you write the actual program.

As pointed out, that is just bad code with no indentation, not pseudo code, which is the process of describing what the program will do, not doing it. Here is your code in pseudocode form:

    if the user inputs a valid medium
    {
        ask user to enter the distance
        get user input for distance
        if (distance > 0)
        {
            calculate the time the sound takes for the distance specified
                for the medium specified
            output the time the sound will take to traverse the medium
        }
        else
        {
            tell user they specified an invalid distance
        }
    }
    else
    {
        tell the user they chose an unsupported medium
    }

Again, the purpose of pseudocode is to tell WHAT the program should do, NOT how it should do it, though hints, suggestions, issues to address and such are ok. The idea is that the pseudocode can be commented out and the appropriate code inserted, resulting in a well-commented application.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.