Help – for both of my for header components, error reads the following, but I do not understand how to fix this because I am not sure what is wrong about it. Please help.

warning C4552: '>' : operator has no effect; expected operator with side-effect

void inputGrades (int numStudents,int numGrades)
{
	for(numStudents > 0; numStudents -= 1;)
	{
		cout << "For this student enter the grades.";
		double score = 0;
		double grade, avg;

		int exams = numGrades;
		for(exams > 0; exams -= 1;)
		{
			cout << "Grade: ";
			cin >> grade;
			score += grade;
		}
		
		avg = static_cast<double>(score) / numGrades;
		cout << "The average test score for the student is: " << avg << endl;
	}
}

int main()
 {
	 int students, tests;

	 cout << "Enter the number of students: ";
	 cin >> students;
	 cout << "Enter the number of tests: ";
	 cin >> tests;

	inputGrades(students, tests);

 cout << endl;
 return 0;

 } // end main

Odd, I'm not getting that error at all...

Did you include the preprocessor directives for cstdlib and iostream? Also are you using namespace std?

It ran fine for me, no weird operator errors.

for(numStudents > 0; numStudents -= 1;)

Generally it's good form to declare the variables used in the for loop, like:

for (numStudents; numStudents > 0; numStudents--)

I don't know if the compiler considers what you've done an error, but it might, I've never actually tried it anyway besides the three section for declaration above. That might be your problem. Also, is your compiler just spitting out a warning or an error, they're different.

Hi QuantNeeds

. . .
warning C4552: '>' : operator has no effect; expected operator with side-effect
. . .

int numStudents = 10;  // assignment necessary 
	for(numStudents > 0; numStudents -= 1;)

Your for loop is correct. It corresponds completely with the Standard ISO/IEC 14882:1998(E) of C++. So your compiler seems to be something outdated, especially the phrase "expected operator with side-effect", what means that an for-init statement would have been expected, tells that your compiler isn't that conform to the standard.

brs,
tesu

Hi QuantNeeds

Your for loop is correct.

No it's not, TacklesMcCaw is right. There's a semicolon in the wrong place:
this: for(numStudents > 0; numStudents -= 1;) Should be this: for(;numStudents > 0; numStudents -= 1) same goes for for(exams > 0; exams -= 1;) ; this should be for(;exams > 0; exams -= 1)

So your compiler seems to be something outdated, especially the phrase "expected operator with side-effect", what means that an for-init statement would have been expected, tells that your compiler isn't that conform to the standard.

VS2008 pro will give you the exact same warning-message and I wouldn't call that compiler 'outdated'. The code will compile, but a warning is given to indicate that although the code is 'correct' the user probably made an error.

Hi niek_e

You are right, I have overlooked the semicolon at he far right.

Intuitively I also would put a semicolon at left side if for-init statement is off. But the ISO standard tells the opposite. You may have a look at this standard on http://www-d0.fnal.gov/~dladams/cxx_standard.pdf
In the lower half page 95 you will find the definition of the for-loop. Where
you can see that the first semicolon is integral part of the for-init statement,
just consider the gap between for-init statement and condition statement, also see
the given note. That means, if one omits the for-init statement the semicolon also
disappears.

Obviously, almost all C++ compilers do not implement the for loop in concordance with the ISO Standard, do they?

krs,
tesu

commented: Good post! +6

In the lower half page 95 you will find the definition of the for-loop. Where
you can see that the first semicolon is integral part of the for-init statement,
just consider the gap between for-init statement and condition statement, also see
the given note. That means, if one omits the for-init statement the semicolon also
disappears.

I've read it and it appears you're right, but I've never seen a compiler that will compile this:

int a = 0;
for (a<50;a++)

Obviously, almost all C++ compilers do not implement the for loop in concordance with the ISO Standard, do they?

Apparently not :)

The for statement is actually explained on page 97. paragraph 6.5.3.3 shows the correct syntax. Without doing any research I'll bet there has been a later correction to 6.5.3.1 and what was shown at the botton of page 95.

> That means, if one omits the for-init statement the semicolon also disappears.
That's not quite right, but Edward can easily see why you'd think that. Like most stuff in the standard, you have to look in several different places to get the whole picture. Specifically, the syntax for a for loop is:

for ( [I]for-init-statement[/I] [I]condition(opt)[/I] ; [I]expression(opt)[/I] ) [I]statement[/I]

The first semicolon is missing because it's implied to always exist. You can see that if you follow the production for for-init-statement:

[I]for-init-statement:
        expression-statement
        simple-declaration[/I]

In 6.2 on page 93 you find the production for expression-statement and 7 on page 101 is the production for simple-declaration:

expression-statement:
        expression(opt) ;

simple-declaration:
        decl-specifier-seq(opt) init-declarator-list(opt) ;

The semicolon isn't optional in expression-statement or simple-declaration, and for-init-statement isn't optional either. This means that a semicolon is always required, even if the for-init-statement is otherwise empty.

> Obviously, almost all C++ compilers do not implement the for loop in concordance with the ISO Standard, do they?
It's not obvious to be sure, but almost all compilers seem to get it right. ;) Keep reading the standard! It's good to challenge assumptions with your own interpretation and we can all learn from this kind of discussion. :)

commented: Good explanation +6

This was the quickest solution that helped. This is what I changed it to:

for(numStudents; numStudents > 0; numStudents -= 1)
	{
		double score = 0;
		double grade, avg;
		
		for(int exams = numGrades; exams > 0; exams -= 1)
		{
			cout << "Grade: ";
			cin >> grade;
			score += grade;
		}

Generally it's good form to declare the variables used in the for loop, like:

for (numStudents; numStudents > 0; numStudents--)

I don't know if the compiler considers what you've done an error, but it might, I've never actually tried it anyway besides the three section for declaration above. That might be your problem. Also, is your compiler just spitting out a warning or an error, they're different.

> That means, if one omits the for-init statement the semicolon also disappears.
That's not quite right, but Edward can easily see why you'd think that. Like most stuff in the standard, you have to look in several different places to get the whole picture. Specifically, the syntax for a for loop is:

for ( [I]for-init-statement[/I] [I]condition(opt)[/I] ; [I]expression(opt)[/I] ) [I]statement[/I]

The first semicolon is missing because it's implied to always exist. You can see that if you follow the production for for-init-statement:

[I]for-init-statement:
        expression-statement
        simple-declaration[/I]

In 6.2 on page 93 you find the production for expression-statement and 7 on page 101 is the production for simple-declaration:

expression-statement:
        expression(opt) ;

simple-declaration:
        decl-specifier-seq(opt) init-declarator-list(opt) ;

The semicolon isn't optional in expression-statement or simple-declaration, and for-init-statement isn't optional either. This means that a semicolon is always required, even if the for-init-statement is otherwise empty.

> Obviously, almost all C++ compilers do not implement the for loop in concordance with the ISO Standard, do they?
It's not obvious to be sure, but almost all compilers seem to get it right. ;) Keep reading the standard! It's good to challenge assumptions with your own interpretation and we can all learn from this kind of discussion. :)

By this, do you mean what niek_e was trying to show above? he stated the following:

for(;numStudents > 0; numStudents -= 1)

So basically whether you are declaring a variable or not, you must always have the semicolon? then the condition and then the counter(s) ?

>>for(numStudents; numStudents > 0; numStudents -= 1)

Or just simple this: for( ; numStudents > 0; numStudents -= 1) >>So basically whether you are declaring a variable or not, you must always have the semicolon? then the condition and then the counter(s) ?

Yes, there must always be two semicolons. As a minimum like this: for( ;; ) which is the same as this: while(true)

woops never mind - saw the posting - thank you :-D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.