Rary56
Rary56

Reputation: 1

C++ warning: for increment expression has no effect [-Wunused-value]

In my code, I have the user input a lower limit, upper limit, and step value. c2 is the lower limit + step. celsius starts at the lower limit.

    for (int x = c2; x <= upperLimit; x + step){
            celsius = celsius + step;
            cout << celsius;
        }

Why am I getting the warning that the for increment expression has no effect?

Also, this for loop is infinite as of now even though it's supposed to stop at the upper limit. Does that have something to do with the warning?

Upvotes: 0

Views: 2743

Answers (1)

ppm
ppm

Reputation: 178

You need x += step to update the value of x. Like so:

for (int x = c2; x <= upperLimit; x += step) {
    celsius += step;
    cout << celsius;
}

You had x + step, which computes the sum but doesn't do anything with the result. Thus the warning that it has no effect.

The loop was infinite because x never changed, and so never exceeded upperLimit.

Upvotes: 4

Related Questions