### Problem with switch structure

Can anybody tell me what is my problem here?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include using namespace std; int main() { char medium [6] ; int distance; double amountOfTime; const int Air = 1100; const int Water = 4900; const int Steel = 16400; cout << "Select your medium: Air, Water, Steel:\n"; cin >> medium; switch (medium) { case Air: cin.getline (medium, 6); break; case Water: cin.getline (medium, 6); break; case Steel: cin.getline (medium, 6); break; } cout<<"Enter the distance:"; cin>>distance; if (distance > 0) { cout<<"Distance must be greater than zero."; exit (0); } else cout<
Hi there,

Welcome to the forums. Please consider this code:

 ``1234567891011121314151617181920212223242526`` ``````char medium [6] ; //medium is a char-array int distance; double amountOfTime; const int Air = 1100; //these are ints const int Water = 4900; const int Steel = 16400; cout << "Select your medium: Air, Water, Steel:\n"; cin >> medium; switch (medium) //you are comparing a char array { case Air: //to integers cin.getline (medium, 6); break; case Water: cin.getline (medium, 6); break; case Steel: cin.getline (medium, 6); break; }``````

The easiest solution would be to make "medium" an int. Then ask the user to enter 1 for air, 2 for water, etc. Then you would do:

 ``1234567891011121314151617181920212223242526`` ``````int medium; // int now int distance; double amountOfTime; const int Air = 1100; const int Water = 4900; const int 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.

All the best,
NwN
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?
thank you
I'm new here, sorry for my vague question
I just fixed it and this is what I got so far
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````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<

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
Integers division, I guess. Try:

`amountOfTime = static_cast <double>(distance) / medium;`
It worked.
Then I just need to change ` int distance;` to ` double distance; ` ?
But I don't know why?
Last edited on
Integers tend to be whole numbers while float and double are used for numbers that have a decimal in it.
integer divided by integer returns integer. Throw a float in there and it returns a float.
Topic archived. No new replies allowed.