Using numbers in Switch statements

Hi all, I have a problem with this. Let's say for distance one, the user enters a number less than 0. It would be a negative number and show that to the user. If the user enters a number greater than 0, then it will show the user that it's positive. When I tried to respond to the question "You have selected air. Enter the distance" with a number, it didn't reply anything back to me. How can I solve this?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


int main()
{
	int distance1,
		distance2,
		distance3;


	double choice1,
		choice2,
		choice3;
		

	const double air1 = 1100,
	water1 = 4900,
	steel1 = 16400;

	string distance;

	enum medium {air, water, steel};

	cout << "Select a medium:" << endl;
	cout << "\n";
	cout << "1. Air" << endl;
	cout << "\n";
	cout << "2. Water" << endl;
	cout << "\n";
	cout << "3. Steel" << endl;
	cout << "\n";
	cout << "Enter Your Choice: ";
	cin >> choice1;

	if (choice1 = 1)
	{
		cout << "You have selected air. Enter the distance:" << endl;

		cin >> distance1;

	switch (distance1)

	{
		case '<= 0': cout << "This is a negative number";
		break;
	}
	
	switch (distance1)
	{
		case '>= 0': cout << "This is a positive number";
		break;
	}

	}

	else if (choice1 = 2)
	{
		cout << "You have selected water. Enter the distance:" << endl;
	}

	else if (choice1 = 3)
	{
		cout << "You have selected steel. Enter the distance:" << endl;
	}






	return 0;
}
The syntax you've used does something completely unrelated to what you want.

Just use if.
1
2
3
4
5
if (distance1 < 0){ // 0 is not negative.
    //...
}else if (distance1 > 0){ // 0 is not positive
    //...
}

Also note that your equals on lines 38, 59, and 64 should be ==, not =. = changes a value, it doesn't compare two values.

That's improper use of the switch/case commands.

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
49
50
51
52
53
54
55
56
57
#include <iostream>

using namespace std;

int main()
{

int Choice;

cout << "Choose 1 - 5  ";
cin >> Choice;

switch (Choice)
{
case 1:
{
    cout << "This is a negative number";
  // More code here if you wish
}
break;
case	 2:
{
    cout << "This is a positive number";
 // More code here if you wish
}
break;

case 3:
{
    cout << "You have selected water. Enter the distance:" << endl;
 // More code here if you wish
}
break;

case 4:
{
    cout << "You have selected air. Enter the distance:" << endl;
 // More code here if you wish
}
break;

case 5:
{

    cout << "You have selected steel. Enter the distance:" << endl;
 // More code here if you wish
}
break;

default :
{
    cout << "What the hell dude?" << endl;
}
};

return 0;
}
Topic archived. No new replies allowed.