int medium; // int now
int distance;
double amountOfTime;
constint Air = 1100;
constint Water = 4900;
constint Steel = 16400;
cout << "Select your medium: 1. Air, 2. Water, 3. Steel:\n";
cin >> medium;
switch (medium) /
{
case 1:
cin.getline (medium, 6);
break;
case 2:
cin.getline (medium, 6);
break;
case 3:
cin.getline (medium, 6);
break;
}
Hope that helps. In the future, please try to state your problem a little bit more clearly and specifically. For example, if you're getting errors (and which ones) or which behaviour you expect but what the program actually does. That will help us to help you better.
May I have a short explanation of what you are trying to do as well comments within the code on what you are trying to do and the error(s) you are getting?
int main()
{
int medium ;
int distance;
double amountOfTime;
cout << "Select your medium: 1.Air, 2.Water, 3.Steel (Enter the number):"<<" ";
cin >> medium;
switch (medium)
{
case 1:
medium = 1100;
break;
case 2:
medium = 4900;
break;
case 3:
medium = 16400;
break;
}
cout<<"Enter the distance:"<< " ";
cin>>distance;
if (distance < 0)
{
cout<<"Distance must be greater than zero.";
exit (0);
}
else
cout<<fixed<<setprecision(4);
amountOfTime=distance/medium;
cout<<"A sound wave takes " << amountOfTime << " " << "seconds to travel " << distance << " feet in " << medium << " " << "feet per sec" << endl;
system("pause");
return (0);
}
When I run it, the variable amountOfTime always be .0000 seconds. How can I fix this thing?
E.g:
Select your medium: 1.Air 2.Water 3.Steel: 1
Enter the distance: 6000
A sound wave takes 5.0000 seconds to travel 6000 feet in 1100 feet per second