• 0

Help with The Speed of Sound C++ Program


Question

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!

4 answers to this question

Recommended Posts

  • 0

You seem to know how to let the user enter things, so entering whether they want the distance in feet or miles should be no problem. The conversion is just a multiplication by the correct factor. What exactly is unclear to you? Please be more specific than "I need help".

  • 0

well I got the asking the user down but right after "cout << setprecision (4) << fixed;", should I go onto the next line and ask "cout << "Do you want the distance in feet or miles: ";" and what should I do after that when I create that question? sorry I am kind of new, this is my two months of C++

  • 0

#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 << "Do you want the distance in feet or miles: ";
    
    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;
}

 

 

So I put the question in asking the user if they want the distance in feet or miles but where would I put the conversion of feet to miles in the program to make the program work?

  • 0

Your units are in feet by default, so if the user enters feet, you do nothing special. If the user enters meters, you convert all your quantities to meters before your switch(choice) structure.

 

The conversion might look like this:

double metersPerFeet = 0.3something;
Air *= metersPerFeet;
Water *= metersPerFeet;

Now is the distance entered by the user in feet or meters? I guess you should ask about the units of measure before the distance, so it's clear what unit the user is expecting.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I'd concur too, as an informed enthusiast I'd find it a good deal, but your average Joe just wants cheap as possible and it's hard to beat Amazon or Ebay builds for that.
    • Now you know how many would-be Windows 11 users felt. I’m not justifying any of it, but MS and Apple both do it.
    • An Air India plane with 242 people on board crashed Thursday near a major international airport in the western Indian city of Ahmedabad, the airline and the country's government said. https://www.nbcnews.com/world/asia/air-india-plane-crashes-indias-ahmedabad-airport-rcna212545
    • NTLite 2025.06.10473 by Razvan Serea NTLite is a Windows configuration tool that allows you to modify your existing Windows install or an image yet to be deployed, remove Windows components, configure and integrate, speed up the Windows deployment process. Reduce Windows footprint on your RAM and storage drive memory. Remove components of your choice, guarded by compatibility safety mechanisms, which speed up finding that sweet spot. Windows Unattended feature support, providing many commonly used options on a single page for easy setup. Easily integrate a single or multiple drivers, update or language packages. Package integration features smart sorting, enabling you to seamlessly add packages for integration and the tool will apply them in the appropriate order, keeping hotfix compatibility in check. One of the important new features of NTLite (compared to its predecessors) is the ability to modify an already installed the operating system, by removing unnecessary components. Supports Windows 11, 10, 8.1 and 7, x86 and x64, live and image. Server editions of the same versions, excluding support for component removals and feature configuration. ARM64 image support in the alpha stage. Does not support Checked/Debug, Embedded, IoT editions, nor Vista or XP. NTLite 2025.06.10473 changelog: Upgrade UI: Text size now set as host, more readable on some systems Post-setup: Loading progress improvement for the 99% delay Components: ‘Snipping tool’ compatibility option UI-Translation: Thanks for Romanian (Coman) Fix Updates: Parallel WinRE integration getting stuck Updates: Download overwrite previous Defender updates Settings: Win10 News widget disabling Download: NTLite 64-bit | 21.5 MB (Free, paid upgrade available) Download: NTLite 32-bit | 19.3 MB Link: NTLite Home Page | NTLite Features | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      elsafaacompany earned a badge
      Week One Done
    • Week One Done
      Yianis earned a badge
      Week One Done
    • Veteran
      Travesty went up a rank
      Veteran
    • One Month Later
      somar86 earned a badge
      One Month Later
    • Week One Done
      somar86 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      506
    2. 2
      ATLien_0
      258
    3. 3
      +FloatingFatMan
      183
    4. 4
      +Edouard
      180
    5. 5
      snowy owl
      131
  • Tell a friend

    Love Neowin? Tell a friend!