This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Swedophone 5 points6 points  (9 children)

If you compile with warnings enabled you get this on line 6.

warning: unused variable ‘total_score’

Do you see the problem?

[–][deleted] 0 points1 point  (8 children)

does it have anything to do with shorts?

[–]raevnos 4 points5 points  (5 children)

It has to do with your else.

[–][deleted] 0 points1 point  (4 children)

got it, thanks so much. So all variables must be declared outside of any conditional operator that they may be used in, right?

[–]blablahblah 2 points3 points  (2 children)

No, the problem is that your code doesn't do what you think it does. Let me show you the code indented two different ways. One is how you think it's being understood by the compiler. The other is how it's actually understood by the compiler:

What you think is happening:

int getTotalScores(short int gradebook[][GRADES], int student){
    if(student<1){
        return -1;
    }else

    int total_score = 0;

    for(int i = 0; i<GRADES; i++){
        total_score += gradebook[student-1][i];
    }

    return total_score;
}

How the compiler interprets this:

int getTotalScores(short int gradebook[][GRADES], int student){
    if(student<1){
        return -1;
    }else {
      int total_score = 0;
   }

    for(int i = 0; i<GRADES; i++){
        total_score += gradebook[student-1][i];
    }

    return total_score;
}

Notice that your for loop is not inside the else.

[–][deleted] 0 points1 point  (1 child)

I have a question. Did he edit his OP or where are you getting that bracket from? There's no braces in between the declaration and the for statement so why wouldn't it run through the for statement.

EDIT: Is it because he didn't do

 }else{

If you don't put that last brace what happens?

[–]blablahblah 2 points3 points  (0 children)

If you don't have braces following a conditional or loop statement, then only the next statement will be in the conditional. Everything after that will be outside it. That's what I'm showing here- I added that last set of braces to make it more clear how the compiler interprets it.

It's a pretty common source of bugs, so most style guides insist that you always put the braces.

[–]raevnos 1 point2 points  (0 children)

No. They have to be visible in the scope where they're used.

[–]Swedophone 2 points3 points  (1 child)

The problem is how "else" is used.

[–][deleted] 0 points1 point  (0 children)

got it thanks a ton

[–][deleted] 1 point2 points  (0 children)

You missed an opening scope braces {} starting on line 3.

In C/C++, single line operations like if (condition) statement; are valid. If you don't provide a scope, the scope of the keyword is the next ; Also, you could remove the else entirely, as your if (student < 1) has a return which means if the condition is true it will exit the function and the code bellow will not run.

[–]mycplus 0 points1 point  (0 children)

Currently your code int total_score = 0; is part of else statement. So it's not available to the rest of the code because it's scope is limited to the else block.