c++ else if statement

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
	void printScores()
	{
		for(int s= 1; s < 25; s++)
		{
			if(testscores[s] s = 0 && testscores[s] s = 24)
			{
				total[0]++;
			}
			else if(testscores[s] s = 25  && testscores[s] s = 49)
			{
				total[1]++;
			}
			else if(testscores[s] s = 50  && testscores[s] s = 74)
			{
				total[2]++;
			}
			else if(testscores[s] s = 75  && testscores[s] s = 99)
			{
				total[3]++;
			}
			else if(testscores[s] s = 100  && testscores[s] s = 124)
			{
				total[4]++;
			}
			else if(testscores[s] s = 125  && testscores[s] s = 149)
			{
				total[5]++;
			}
			else if(testscores[s] s = 150  && testscores[s] s = 174)
			{
				total[6]++;
			}
			else if(testscores[s] s = 175  && testscores[s] s = 200)
			{
				total[7]++;
			}
		}



don't know whats wrong with this else if. gives me errors by testscores[s] s=175
Do you realize that there is a difference between the comparison operator== and the assignment operator=?

Also wouldn't it be better to be checking if the number is within a range instead of assigning it a value?
Last edited on
Also, your syntax is wrong. The test should be
if(testscores[s] == 175 ....

You have an extra 's' in each of your if statements.

And guessing what you are trying to do, I suspect you are checking to see if each value is within a range. You are currently checking that the value equals both the top and bottom values of the range (which it never will). Each of the if statements should be

if (testscores[s] >= ... && testscores[s] <= ...)
doug4 explained how to fix your code.

But this can easily be calculated as
1
2
3
4
5
6
for(int s= 1; s < 25; s++) {
    if(testscores[s] == 200) //this case is different from all other you have
        total[7]++;
    else
        total[ testscores[s] / 25 ] ++ ; //calculating result
}


Calculating the solution, rather than using a lot of if else is much faster.
Topic archived. No new replies allowed.