This code is just a function in a bigger code, but I'm having a problem with the break. I'm not sure I'm doing it right, but basically if the factors are zero, then the program should say the equation can't be factored. It does that, but then continues through the rest of the program.

void display(int a, int c, int g, int f1, int f2)
{   
    while(f1==0&&f2==0)
    {
        cout << "This equation does not factor.";
        break;
    }

    cout << "The equation is now (" << a << "x^2+" << f1 << "x)+(" << f2 << "x+" << c << ")" << endl;

    int g1=gcd(a, f1);
    int g2=gcd(f2, c);

    a=a/g1, f1=f1/g1, f2=f2/g2, c=c/g2; 

    cout << "The equation is now " << g1 << "x(" << a << "x+" << f1 << ")+" << g2 << "(" << f2 << "x+" << c << ")=(" << a << "x+" << f1 << ")(" << g1 << "x+" << g2 << ")" << endl;

    cout << "The factored equation is " << g << "(" << a << "x+" << f1 << ")(" << g1 << "x+" << g2 << ")" << endl;

}

Dani AI

Generated

The issue is not that break failed — it did exactly what it is supposed to do: exit the innermost loop. In your code the while runs (or would run) once, break leaves that loop, and execution continues with the rest of the function. If the intent is "check once and stop the function if unfactorable", use a single conditional and exit early.

A clear, minimal fix is a guard clause that prints the message and returns immediately:

if (f1 == 0 && f2 == 0) {
    std::cout << "This equation does not factor\n";
    return;   // stop the function here
}

As pointed out, a flag works, but a direct return is simpler and more readable. Returning a boolean (or throwing) are also valid designs if the caller should handle the failure instead of printing here.

Two important cautions:

  • Do the early return before you call gcd or divide by its result. If gcd yields zero (or your own code divides by zero), you will get undefined behavior or a crash. Always check g1/g2 before using them as divisors.
  • Prefer '\n' over std::endl unless you need the flush; it is faster and makes intent clearer.

For readability and maintenance, give variables descriptive names (for example leftCoef/rightCoef rather than f1/f2) and keep the function focused on one job: either compute/factor and return status, or print results. For this change will stop the function when the factors are zero and avoid the later computations that rely on nonzero divisors.

Recommended Answers

All 3 Replies

here's a few options I could think as of the moment:
1. use a conditional for the rest of the code in the function, e.g.

    void display(int a, int c, int g, int f1, int f2)
    {
        int i = 0; //or you could use a boolean

        while(f1==0&&f2==0)
        {
            cout << "This equation does not factor.";
            i = 1;
            break;
        }

            if(i == 0){
            //rest of the code
            }
    }
  1. you could exit the program once this happens
  2. you could use a function with a return type so you could return a value(end the function) after it prints equation does not factor.
  3. use a goto statement( not recommended)

my favorite is option 1 ;)

thank you. that worked, but I'm not sure exactly what that does. could you explain it to me?

which one did you use?

if it's option 1 once the value of integer i changes the condition won't be met for the if statement

note: for my first post I didn't notice that the numbering changed when I entered the code, that's actually 4 options

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.