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;

}

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.