mercredi 27 septembre 2017

Why do my if statements not work in for loops?

So this is my second program I've tried writing an if statement in a for loop. The first one was unnecessary, but this one "needs" it. I put needs in quotes because I'm new to C++ and have not learned/thought of another method.

My program:

int main() {

int x, z;
cout << " x | z |                  y\n"
     << "---------------------------\n";
for (x = 1; x <=5; x++)
{
    for (z = 2; z <= 6; z++)
    {
        double y = x*z/(x-z);
        if (x - z == 0)
        {
            cout << setw(2) << x << setw(4) << z << setw(21) << "Function Undefined\n";
        }
        else if (x - z != 0)
        {
            cout << setw(2) << x << setw(4) << z << setw(21) << y << endl;
        }
    }
}
return 0;

}

When I run this, I get a table with the first loop sequence (x = 1) completed, but then it crashes (Windows is looking for solution box pops up). It writes out the correct values only for x = 1 (the outer loop does not repeat to x = 2 and so on).

Any help is appreciated, thanks!

Aucun commentaire:

Enregistrer un commentaire