[Solved] C++, for, Infinite loop


The main problem is, you are changing the value of s in inner loop but when the inner loop terminates, you don’t reset s to 0. Hence s == p never become true.

Try this:

for (g = 0; a == 0; g++) {
    for (c = 0; p > s; c++) {
        s = 5 * c - 3 * g;
        if (s == p)
            a = 1;
    }
    s = 0; // reset s for next checking
}

For input,

p = 12

The output may be:

12

4

1

solved C++, for, Infinite loop