C++ for loop help

I'm trying to get a for loop that prompts for grades 1-5, but for some reason it starts at grade #6 and I can't for the life of me figure out why.

1
2
3
4
5
6
7
8
9
10
11
12
13
  for(course = 1; course <= 5; course = course + 1);
	{
		cout << "Enter Letter Grade #" << course << ": ";
		cin >> grade;

		if(grade == 'A') score = 4;
		else if(grade == 'B') score = 3;
		else if(grade == 'C') score = 2;
		else if(grade == 'D') score = 1;
		else score = 0;

		totalScore += score;
	}
What starts at "#6"?

You do initialize totalScore before the loop, yes?
Remove the semicolon from the end of your "for" declaration. The loop does nothing more than add 5 to course. Once that's done, the bracketed code executes -- only once, with course = 6. Just remove the semicolon and everything will function as you intended.
Last edited on
Sorry, course outputs at 6 and then the loop ends.
It's because your for loop contains no code.
Awesome thanks!
And I believe that, because this code is bracketed, any changes made to the variables will only last until the closing bracket, because of how the scoping works.

No. This is absolutely NOT true.

If you declare a variable within a scope, then it will be destroyed once it falls out of scope. And it will hide any variable of the same name that was declared outside that scope - which is a dangerous and confusing thing to do, so don't do it.

But if you change the value of a variable that was declared outside the current scope, then that value change won't magically be reversed just because you exit that scope.
My mistake. Updated. Thanks for correcting me!
Topic archived. No new replies allowed.