C++ program that displays a menu allowing the user to select air, water, steel. After the user has made a selection, 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 display the amount of time it will take for sound to travel this distance in seconds.
constants are
Air = 1100 feet per second
Water = 4900 feet per second
Steel = 16400 feet per second
the distance may be a whole number or may not be. If the user enter a negative number use its absolute value.
The feet or miles choice will be a single char. Valid values are f or F for feet and m or M for miles. However the program should consider m or M to mean miles and any other character to mean feet.
Ouput should include the entered medium,, distance in Feet and time. the time should be rounded 4 decimal places, but distance should be rounded to one decimal place.
If you could help with this I would appreciate it! I started but i got stuck but this is what I know is for sure good.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
const int Air = 1100;
const int Water = 4900;
const int Steel = 16400;
int choice;
double distance, time;
int feet, miles;
double speedOfSoundAir, speedOfSoundWater, speedOfSoundSteel;
cout << "How long will it take?" <<endl;
cout << "Enter the medium through wich sound is traveling: " <<endl;
cout << "\t1. Air " <<endl;
cout << "\t2. Water " <<endl;
cout << "\t3. Steel " <<endl;
cout << "Enter choice (1 - 3) : ";
cin >> choice;
cout << fixed << showpoint << setprecision(4);
switch (choice)
{
case 1:
cout << "Enter the distance a sound wave will travel in the selected medium: ";
cin >> distance;
cout << "Is the distance in feet or miles? Enter f for feet and m for miles: ";
cin >> feet >> miles;
return 0;
}
Help!!!
Copyright © 2024 EBIN.TIPS - All rights reserved.
Answers & Comments
Verified answer
See this link for correct code + sample output: http://ideone.com/5p6HEu
For queries, email or IM me.
i think i covered most of your requirements, other than outputting in certain precision, but you seem to have that covered already. you just need to plug in your formulas for the CalculateTimeTaken function and you should be good to go, i think. i haven't tested this, obviously.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
double CalculateTimeTaken(char medium, double distance, char unit)
{
double timeTaken = 0;
if ((unit != 'f') || (unit != 'F'))
{
distance *= 5280; // converts miles to feet.
}
if (medium == '1')
{
timeTaken = ;// insert formula for calculating sound to travel through air
}
if (medium == '2')
{
timeTaken = ;// insert forumla for calculating sound to travel through water
}
if (medium == '3')
{
timeTaken = ;// insert formula for calculating sound to travel through steel
}
return timeTaken;
}
int main()
{
const int Air = 1100;
const int Water = 4900;
const int Steel = 16400;
char medium, char unitofmeasurement;
double distance, time;
cout << "How long will it take?\nEnter the medium through which* the sound is travelling*: \n";
cout << "\t1. Air\n\t2. Water\n\t3. Steel\nEnter choice (1-3): ";
cin >> medium;
cout << "\nEnter the distance the sound wave will travel: \n";
cin >> distance;
cout << "\nIs this in feet or miles? (f or m): \n";
cin >> unitofmeasurement;
time = CalculateTimeTaken(medium, distance, unitofmeasurement);
switch (medium)
{
// really wanted to avoid doing this...
case '1':
cout << "\nIt would take " << time << " seconds for sound to travel " << distance << " through Air\n";
break;
case '2':
cout << "\nIt would take " << time << " seconds for sound to travel " << distance << " through Water\n";
break;
case '3':
cout << "\nIt would take " << time << " seconds for sound to travel " << distance << " through Steel\n";
break;
}
cout << "Press ENTER to quit";
getch();
return 0;
}
not anymore, at one time C++ became into merely an extension of C. for the reason that then, C has replaced, and so has C++. even however that being pronounced it is common for the same application to collect the two C and C++. the tactic is the same, yet there are purely some modifications int he libraries which you like the compiler to link to.