C4552: '>' : operator has no effect
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
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 10
Solved Threads: 0
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 codewill compile, but a warning is given to indicate that although the code is 'correct' the user probably made an error.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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 :)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 10
Solved Threads: 0
> 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 ( <em>for-init-statement</em> <em>condition(opt)</em> ; <em>expression(opt)</em> ) <em>statement</em>
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:
<em>for-init-statement:
expression-statement
simple-declaration</em>
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) ?
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 10
Solved Threads: 0
>>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)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
woops never mind - saw the posting - thank you :-D
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 10
Solved Threads: 0