Need help with switch statement

Hey guys,
I need help with the switch statement at the bottom of the program. Error is that the expression must have a constant value and that the value of the variable 'physics' cannot be used as a constant.

Switch statement is incomplete btw.

In this case, would you recommend me to use an if statement instead?
Here's the code anyway.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
	int physics;
	int maths;
	int acc;
	int phy_points;
	int maths_points;
	int acc_points;
	int num = 1;
	char option = 'N';

	cout << "Please enter your mark for physics: ";
	cin >> physics;
	cout << "\n";

	if (physics > 100 || physics < 0)
	{
		for (;;) 
		{
			cout << "Data value is out of range, please try again.";  
			cout << "\n";
			cout << "Please enter your mark for physics: ";
			cin >> physics;
			cout << "\n";

			if (100 >= physics && physics >= 0)
			{
				break;
			}
		}

	}

	cout << "Please enter your mark for maths: ";
	cin >> maths;
	cout << "\n";

	if (maths > 100 || maths < 0)
	{
		for (;;) 
		{
			cout << "Data value is out of range, please try again."; 
			cout << "\n";
			cout << "Please enter your mark for maths: ";
			cin >> maths;
			cout << "\n";

			if (100 >= maths && maths >= 0)
			{
				break;
			}
		}

	}

	cout << "Please enter your mark for acc: ";
	cin >> acc;
	cout << "\n";

	if (acc > 100 || acc < 0)
	{
		for (;;) 
		{
			cout << "Data value is out of range, please try again.";  
			cout << "\n";
			cout << "Please enter your mark for acc: ";
			cin >> acc;
			cout << "\n";

			if (100 >= acc && acc >= 0)
			{
				break;
			}
		}

	}

	switch (physics)
	{
	case  (100 >= physics && physics >= 90):

	}
	system("pause");
	return 0;

}
In this case, would you recommend me to use an if statement instead?

Yes, use an if statement. A case label can only handle a single value.
I realize you are just learning loops and such. Just thought I'd mention that placing an empty for loop inside the if statement can be accomplished a little bit more cleanly with a single while loop:

1
2
3
4
5
6
7
8
9
10
11
12
	cout << "Please enter your mark for physics: ";
	cin >> physics;
	cout << "\n";

	while (physics > 100 || physics < 0)
	{
		cout << "Data value is out of range, please try again.";  
		cout << "\n";
		cout << "Please enter your mark for physics: ";
		cin >> physics;
		cout << "\n";
	}
Also you could avoid repeating your code and your condition multiple times. Generally speaking, when you have multiple lines doind the same you're doing something wrong. If you don't know the do-while you could do this:
1
2
3
4
5
6
7
8
9
10
int physics = -1;

while (physics > 100 || physics < 0)
	{
		cout << "Data value is out of range, please try again.";  
		cout << "\n";
		cout << "Please enter your mark for physics: ";
		cin >> physics;
		cout << "\n";
	}

This way you don't have to repeat your if condition and the entire input request.
If you have done the do while it can get even better:
1
2
3
4
5
6
7
8
9
10
11
int physics;

do
	{
		cout << "Data value is out of range, please try again.";  
		cout << "\n";
		cout << "Please enter your mark for physics: ";
		cin >> physics;
		cout << "\n";
	}
while (physics > 100 || physics < 0);

With a do while you will perform the action inside the cycle before checking its result, which allows you to get the input at least once before the loop condition is verified.

Anyway since you're already using break, there's no reason at all to follow your structure
1
2
3
4
5
6
7
8
9
10
11
//pseudocode
get input
if input not correct
	{
	forever
		{
		get input
		if input correct
			{break}
		}
	}

You're repeating the same things over and over, which is not good. Sticking with a forever loop you could do way better.
1
2
3
4
5
6
7
8
9
10
11
12
int physics;
for(;;)
	{
	cout << "Data value is out of range, please try again.";  
	cout << "\n";
	cout << "Please enter your mark for physics: ";
	cin >> physics;
	if (100 >= physics && physics >= 0)
		{
		break;
		}
	}

As you can see there's no reason to repeat the if and the input request twice even using forever with break.
Registered users can post here. Sign in or register to post.