the data below lists a medium and the speed that sound travels though this mediu
ID: 3620774 • Letter: T
Question
the data below lists a medium and the speed that sound travels though this medium in meters per second. Use this data as an input file to your program:CarbonDioxide 258.0
Air 331.5
Helium 972.0
Hydrogen 1270.0
Write a program that displays a menu allowing the user to select one of these four gases. After a selection has been made, the user should enter the number of seconds it took for sound to travel in this medium from its source to the location at which it was detected. The program should then report how far away (in meters) the source of the sound was from the detection location.
Input validation: check that the user has selected one of the available menu choices. Do not accept times less that 0 seconds or more than 30 seconds.
Please use the attached input file below: note CarbonDioxide is spelled as one word to make things simpler.
Example:
--------------------------------------------
Enter the number of the medium of your choice. Choices are:
1. CarbonDioxide
2. Air
3. Helium
-> 2
you chose air
how many seconds did the sound travel?
-> 100
The speed of sound through air is 331500m/s
Explanation / Answer
please rate - thanks your problem says secs must be between 0 and 30, and the use 100 in the sample. I'm not sure of the formula speed*sec*10 #include <iostream>#include <fstream>
using namespace std;
int main()
{int choice=0,gascount=0,i,sec;
string gas[10],type;
double speed[10],speedd;
ifstream input;
input.open("input.txt");
if(input.fail())
cout<<"file did not open please check it ";
input>>gas[gascount++];
while(input)
{input>>speed[gascount];
input>>gas[++gascount];
}
while(choice!=5)
{cout<<"Choose gas would you like to use ";
cout<<"1 CarbonDioxide ";
cout<<"2 Air ";
cout<<"3 Helium ";
cout<<"4 Hydrogen ";
cout<<"5 Exit ";
cin>>choice;
// I don't know how to clear screen system("clrscr");
switch(choice)
{case 1: type="CarbonDioxide";
break;
case 2: type="Air";
break;
case 3: type="Helium";
break;
case 4: type="Hydrogen";
break;
case 5: system("exit");
default: printf("Illegal input: Try Again ");
}
i=0;
for(i=0;i<gascount;i++)
if(type.compare(gas[i])==0)
{speedd=speed[i];
i=gascount+1;
}
cout<<"You chose "<<type<<endl;
cout<<"how many seconds did the sound travel? ";
cin>>sec;
while(sec<0||sec>30)
{cout<<"must me between 0 and 30 ";
cout<<"how many seconds did the sound travel? ";
cin>>sec;
}
cout<<"The speed of sound through "<<type<<" is "<<speedd*sec*10.<<"m/s ";
}
input.close();
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.