Problem with switch structure

Can anybody tell me what is my problem here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
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<<fixed<<setprecision(4);
                amountOfTime=distance*medium;
 
                cout<<"A sound wave takes " << amountOfTime << "seconds to travel " << distance << " feet in " << medium;
                return (0);
}
Hi there,

Welcome to the forums. Please consider this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
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.