| | |
C4552: '>' : operator has no effect
Thread Solved
![]() |
•
•
Join Date: May 2008
Posts: 96
Reputation:
Solved Threads: 0
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
warning C4552: '>' : operator has no effect; expected operator with side-effect
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Jun 2008
Posts: 9
Reputation:
Solved Threads: 2
Generally it's good form to declare the variables used in the for loop, like:
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
Hi QuantNeeds
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
•
•
•
•
. . .
warning C4552: '>' : operator has no effect; expected operator with side-effect
. . .
C++ Syntax (Toggle Plain Text)
int numStudents = 10; // assignment necessary for(numStudents > 0; numStudents -= 1;)
brs,
tesu
No it's not, TacklesMcCaw is right. There's a semicolon in the wrong place:
this:
Should be this:
same goes for
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.
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.
Last edited by niek_e; Jun 16th, 2008 at 6:50 am.
•
•
Join Date: Apr 2008
Posts: 296
Reputation:
Solved Threads: 42
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
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
•
•
•
•
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.
cpp Syntax (Toggle Plain Text)
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?
Last edited by niek_e; Jun 16th, 2008 at 9:11 am.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
> 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:
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:
In 6.2 on page 93 you find the production for expression-statement and 7 on page 101 is the production for simple-declaration:
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.
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 ( for-init-statement condition(opt) ; expression(opt) ) statement
for-init-statement:
expression-statement
simple-declaration C++ Syntax (Toggle Plain Text)
expression-statement: expression(opt) ; simple-declaration: decl-specifier-seq(opt) init-declarator-list(opt) ;
> 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.
Last edited by Radical Edward; Jun 16th, 2008 at 10:24 am.
If at first you don't succeed, keep on sucking until you do succeed.
•
•
Join Date: May 2008
Posts: 96
Reputation:
Solved Threads: 0
This was the quickest solution that helped. This is what I changed it to:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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.
Last edited by QuantNeeds; Jun 16th, 2008 at 12:39 pm.
![]() |
Similar Threads
- assign elements to a multi-d array (C)
- How to select data frm text file based on a condition (C)
- stack palindrome problem? (C++)
Other Threads in the C++ Forum
- Previous Thread: C2668: 'pow' : ambiguous call
- Next Thread: How do I make a type that recognizes brackets[] like vector?
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






