Sentinel didn't working

Hello, I use the sentinel to control the loops to find the smallest n numbers. However, after I run it, the loops keep running even though I input the Sentinel value. Can someone point out my mistakes. I'm new in programming. Thank you in advance!

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

void calculateScore(int num) {
	
cout << "The lowest test score : \n" << num;
}
int main()
{
 unsigned short int test_score = 0, high, low  , sum_score = 0 , counter = 0;
 
 cout << "Enter a test score (-1 to stop): ";
 cin >> test_score;

 while (test_score != -1)
 { 
	 cout << "Enter a test score (-1 to stop): ";
	 cin >> test_score;
	 
	 if (counter == 0) {
		 low = test_score; 
		 high = test_score;
	 }
	 else
	 {
	   if (test_score < low && test_score != -1)
			 low = test_score;
	   else if (test_score > high && test_score != -1)
		   high = test_score;
	 }
	 counter++;
 } 
 
  
 calculateScore(low);

 sum_score += test_score;
 
 cout << "\n";
 cout << "The sum of test score : " << sum_score;
 cout << "\n";


 return 0;
 

}
test_score is unsigned. So can never have a negative value! When you enter -1 this gets converted into a very large positive number. Change the type of test_score to int (which is signed).
Oh! I see, btw after I run the code again after I deleted the unsigned, why I got the sum score as -1?
Why are you adding test_score (-1) to sum_score after the loop has terminated? You aren't adding to sum_score inside the loop, so sum_score will be the final value of test_score which is -1.
Also consider which only has the one input:

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

int main()
{
	int test_score = 0, high, low, sum_score = 0, counter = 0;

	while (cout << "Enter a test score (-1 to stop): " && (cin >> test_score) && (test_score != -1))
	{
		if (counter == 0)
			low = high = test_score;
		else {
			if (test_score < low)
				low = test_score;
			else
				if (test_score > high)
					high = test_score;
		}

		sum_score += test_score;
		counter++;
	}

	cout << "\nThe lowest test score : " << low;
	cout << "\nThe sum of test score : " << sum_score << '\n';
}

alright, I understand it much better now, however when I compile and run my program here, I can get the correct sum of score, but when I run it with my compiler, I get the wrong sum of score.
What compiler are you using? It sounds like you are using an un-initialized variable some where. Post your current code so we can advise further.
wait, I can get the answer correctly now, hahahaha btw thank you for helping me :D
Topic archived. No new replies allowed.