A little help with switch statements

So i have a task : enter N , and a "social score".
After which you write N amount of marks, for an F you get "-5" score , for a D "-3",
for a C "-1" , and for a B you get 0, or your score remains the same. After you've written N amounts of marks , you need to print your remaining score , or if you have already dropped to 0 , print ," you have lost all your points."
#include <iostream>
using namespace std;
int main()
{
int N, Score;
char Mark;
cin >> n >> Score;
if ((n < 1 || n > 10) || (Score < 10 || Score > 50)) {
return -1;
}
for (int i = 0; i < N; ++i) {
if (Score > 0) {
cin >> Mark;
}

}
switch (Mark) {
case 'F': Score -= 5;

case 'D': Score-= 3;

case 'C': Score -= 1;

case 'B': Score -= 0;
}
if (Score > 0) {
cout << Score;
}
else {
cout << "you have lost all your points.";
}

return 0;
}


I really don't understand where I'm going wrong with this , but im pretty new at C++ , so any help will be highly appreciated
Last edited on
After the statement(s) for a case, you need break; - otherwise the case 'drops' through to the following code. So for case 'F', 5 is subtracted from Score, then 3 subtracted, then 1 subtracted. So you end up with 9 subtracted from Score for F!

1
2
3
case 'F':
    Score -= 5;
    break;

Thanks for the fast answer , still doesn't work though :(
It looks like the switch() is not inside the loop. So you need to move the switch() inside the loop.
Yeah - it helps if you use code tags when you post code so that it's readable!

There also seems to be confusion over N and n. These are different variables!

As formatted, the code above is:

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
#include <iostream>
using namespace std;

int main()
{
	int n, Score;
	char Mark;

	cin >> n >> Score;
	if ((n < 1 || n > 10) || (Score < 10 || Score > 50)) {
		return -1;
	}

	for (int i = 0; i < n; ++i) {
		if (Score > 0) {
			cin >> Mark;
		}
	}

	switch (Mark) {
		case 'F': Score -= 5;

		case 'D': Score -= 3;

		case 'C': Score -= 1;

		case 'B': Score -= 0;
	}

	if (Score > 0) {
		cout << Score;
	} else {
		cout << "you have lost all your points.";
	}

	return 0;
}


which shows that coder777 is correct.
Thank you guys , it works fine now
Friendly tip: When you say "it doesn't work", tell us specifically what doesn't work. If it doesn't compile, post the compiler error.
Topic archived. No new replies allowed.