943,902 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5330
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 16th, 2008
0

C4552: '>' : operator has no effect

Expand Post »
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



C++ Syntax (Toggle Plain Text)
  1.  
  2. void inputGrades (int numStudents,int numGrades)
  3. {
  4. for(numStudents > 0; numStudents -= 1;)
  5. {
  6. cout << "For this student enter the grades.";
  7. double score = 0;
  8. double grade, avg;
  9.  
  10. int exams = numGrades;
  11. for(exams > 0; exams -= 1;)
  12. {
  13. cout << "Grade: ";
  14. cin >> grade;
  15. score += grade;
  16. }
  17.  
  18. avg = static_cast<double>(score) / numGrades;
  19. cout << "The average test score for the student is: " << avg << endl;
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. int students, tests;
  26.  
  27. cout << "Enter the number of students: ";
  28. cin >> students;
  29. cout << "Enter the number of tests: ";
  30. cin >> tests;
  31.  
  32. inputGrades(students, tests);
  33.  
  34. cout << endl;
  35. return 0;
  36.  
  37. } // end main
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
QuantNeeds is offline Offline
96 posts
since May 2008
Jun 16th, 2008
0

Re: C4552: '>' : operator has no effect

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 16th, 2008
0

Re: C4552: '>' : operator has no effect

Click to Expand / Collapse  Quote originally posted by QuantNeeds ...



C++ Syntax (Toggle Plain Text)
  1.  
  2. for(numStudents > 0; numStudents -= 1;)
Generally it's good form to declare the variables used in the for loop, like:

C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
TacklesMcCaw is offline Offline
9 posts
since Jun 2008
Jun 16th, 2008
0

Re: C4552: '>' : operator has no effect

Hi QuantNeeds
Click to Expand / Collapse  Quote originally posted by QuantNeeds ...
. . .
warning C4552: '>' : operator has no effect; expected operator with side-effect
. . .
C++ Syntax (Toggle Plain Text)
  1. int numStudents = 10; // assignment necessary
  2. 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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Jun 16th, 2008
0

Re: C4552: '>' : operator has no effect

Click to Expand / Collapse  Quote originally posted by tesuji ...
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)

Click to Expand / Collapse  Quote originally posted by tesuji ...
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.
Last edited by Nick Evan; Jun 16th, 2008 at 6:50 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 16th, 2008
1

Re: C4552: '>' : operator has no effect

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
Reputation Points: 158
Solved Threads: 98
Master Poster
tesuji is offline Offline
720 posts
since Apr 2008
Jun 16th, 2008
0

Re: C4552: '>' : operator has no effect

Click to Expand / Collapse  Quote originally posted by tesuji ...
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:
cpp Syntax (Toggle Plain Text)
  1. int a = 0;
  2. for (a<50;a++)
Click to Expand / Collapse  Quote originally posted by tesuji ...
Obviously, almost all C++ compilers do not implement the for loop in concordance with the ISO Standard, do they?
Apparently not
Last edited by Nick Evan; Jun 16th, 2008 at 9:11 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 16th, 2008
0

Re: C4552: '>' : operator has no effect

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jun 16th, 2008
1

Re: C4552: '>' : operator has no effect

> 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 ( for-init-statement condition(opt) ; expression(opt) ) statement
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:
for-init-statement:
        expression-statement
        simple-declaration
In 6.2 on page 93 you find the production for expression-statement and 7 on page 101 is the production for simple-declaration:
C++ Syntax (Toggle Plain Text)
  1. expression-statement:
  2. expression(opt) ;
  3.  
  4. simple-declaration:
  5. 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.
Last edited by Radical Edward; Jun 16th, 2008 at 10:24 am.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Jun 16th, 2008
0

Re: C4552: '>' : operator has no effect

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

C++ Syntax (Toggle Plain Text)
  1.  
  2. for(numStudents; numStudents > 0; numStudents -= 1)
  3. {
  4. double score = 0;
  5. double grade, avg;
  6.  
  7. for(int exams = numGrades; exams > 0; exams -= 1)
  8. {
  9. cout << "Grade: ";
  10. cin >> grade;
  11. score += grade;
  12. }

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

C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
QuantNeeds is offline Offline
96 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C2668: 'pow' : ambiguous call
Next Thread in C++ Forum Timeline: How do I make a type that recognizes brackets[] like vector?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC