• 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

    • Par for the course. I asked a question a few days ago and was threatened to be reported. 😂
    • Ummmm Are you kidding? Do think these books being sold are disclosing they are AI written?
    • i'm just commenting because of this madness. you simply asked "what crowd" which i too am genuinely curious about, only to receive a response in the form of a link that directs to an analysis of the audited financial statements with the accompanying notes. then you say that the guy who wrote it is stupid, which whatever, that's not an argument being discussed atm, only to receive a response from Arceles saying "i don't deal with people whose first response is an ad homenim". jesus. this is like making a claim, and then saying "i don't deal with people who speak in a certain way or swear so i'm not gonna answer you hah!" (said in a nasaly voice, not trying to depict you Arceles). then focus on the argument instead of the explanation begins... "so what don't you like about the guy (lunduke)" followed by "he just likes to insult people" and the explanation for the "crowd" being referred to was never even established. so a request for an explanation about the crowd turned into an argument about "why do you think lunduke is an idiot". wowza.
    • Sony lays off 30% of staff from Days Gone developer Bend Studio by Pulasthi Ariyasinghe Another wave of layoffs has hit the game developer space, and this time, it's a first-party Sony studio that's been affected. Following a report by Bloomberg's Jason Schreier, Bend Studio has confirmed that it is letting go of "incredibly talented teammates" as it begins work on a new game project. "Today, we said goodbye to some incredibly talented teammates as we transition to our next project," said Bend Studio in a social media post today. "We're deeply thankful for their contributions as they've shaped who we are, and their impact will always be part of our story." Bend Studio is most well-known for its 2019-released open-world zombie adventure Days Gone, which even received a remaster just a few months ago. Prior to that, the studio had been responsible for the classic Syphon Filter series while also developing several PlayStation Portable and Vita games like Resistance: Retribution and Uncharted: Golden Abyss, respectively. "This is a difficult moment for our team, but we hold immense respect for everyone who got us here," the company added. "As we move forward, we remain committed to building the future of Bend Studio with creativity, passion, and innovation in the titles we craft." While Sony did not detail just how many staff have been affected by this latest decision, Jason Schreier revealed that 30% of the studio is being laid off. This amounts to around 40 people, according to the reporter. Earlier this year, Sony canceled two live service games that were in development at Bend Studio and Bluepoint Games. It was never revealed what this mystery game was supposed to be. "Bend and Bluepoint are highly accomplished teams who are valued members of the PlayStation Studios family," Sony said at the time. It's unclear if Bluepoint, which had been developing a God of War live service experience, will soon be hit by its own wave of layoffs too.
    • KataLib 4.5.3.0 by Razvan Serea KataLib is more than just a music player — it's a complete audio suite designed for music lovers and creators alike. It combines a powerful audio player, a flexible metadata editor, a capable audio converter, and a music library manager into one streamlined application. Core Features: Audio Player Enjoy seamless playback of virtually any audio format or even streaming video files. DJ Mode lets you mix tracks with manual or automatic crossfades. You can also load and save WinAmp-style playlists for quick access to your favorite sets. Audio Converter Convert between a wide range of audio formats effortlessly. Trim or normalize your output automatically, and even extract audio from streaming video sources. Ideal for preparing files for different devices or platforms. Metadata Editor View and edit ID3v2 tags and other metadata. Batch edit multiple files at once, and fetch missing information directly from the MusicBrainz database. You can also apply or update album art with ease. Music Library Manager Organize your entire audio collection, search across tracks instantly, and download cover images from the internet — or use your own custom artwork. KataLib makes it easy to keep your library tidy and enriched with useful info. Supported Formats: KataLib supports a wide range of both lossy and lossless audio formats: Input: OPUS, AAC, FLAC, M4A, MP3, MP4, MPC, APE, AIF, MKV, AVI, MOV, FLV, WEBM, Ogg Vorbis, WAV, WAVPack, WMA Output: OPUS, FLAC, M4A, MP3, Ogg Vorbis, WAV Under the hood, KataLib uses the trusted FFmpeg engine for audio conversion and media playback, ensuring compatibility with virtually all mainstream media formats. Download: KataLib 4.5.3.0 | 64.5 MB (Open Source) Links: KataLib Home Page | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • First Post
      K Dorman earned a badge
      First Post
    • Reacting Well
      rshit earned a badge
      Reacting Well
    • Reacting Well
      Alan- earned a badge
      Reacting Well
    • Week One Done
      IAMFLUXX earned a badge
      Week One Done
    • One Month Later
      Æhund earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      545
    2. 2
      ATLien_0
      268
    3. 3
      +FloatingFatMan
      207
    4. 4
      +Edouard
      203
    5. 5
      snowy owl
      140
  • Tell a friend

    Love Neowin? Tell a friend!