incorrect output

This program is supposed to display a polynomial based on user input. Everything works fine until I make a or b = 0. A sample input would be a=0, b=6, c=-3. the output is: + 6x - 3 = 0
the output should be: 6x - 3 = 0. here is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, b, c;

    cout << " Enter a: " << endl;
    cin >> a;
    cout << " Enter b: " << endl;
    cin >> b;
    cout << " Enter c: " << endl;
    cin >> c;

    if (a == 1)
        cout << "x^2";
    else if (a == -1)
        cout << "-x^2";
    else if (a < 0)
        cout << "-" << abs(a) << "x^2";
    else if (a > 1)
        cout << a << "x^2";
    if (b == 1)
        cout << " + x";
    else if (b == -1)
        cout << " - x";
    else if (b < 0)
        cout << " - " << abs(b) << "x";
    else if (b > 0)
        cout << " + " << b << "x";
    if (c >= 1)
        cout << " + " << c;
    else if (c < 1)
        cout << " - " << abs(c);
    cout << " = 0" << endl;

    return 0;
}

can anyone point me in the right direction?
What's the problem? You just need more if checks.

If a == 0, then don't output a "+" before b (but do output a "-").
im not sure i understand what you are saying. I guess i can't grasp how i would code that statement?
I figured it out, is there a cleaner way to do this, just curious?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, b, c;

    cout << " Enter a: " << endl;
    cin >> a;
    cout << " Enter b: " << endl;
    cin >> b;
    cout << " Enter c: " << endl;
    cin >> c;

    if (a == 1)
        cout << "x^2";
    else if (a == -1)
        cout << "-x^2";
    else if (a < 0)
        cout << "-" << abs(a) << "x^2";
    else if (a > 1)
        cout << a << "x^2";
    if (b == 1)
    {
        if (a == 0 && b == 1)
            cout << "x";
        else
            cout << " + x";
    }
    else if (b == -1)
    {
        if (a == 0 && b == -1)
            cout << "-x";
        else
            cout << " - x";
    }
    else if (b < 0)
    {
        if (a == 0 && b < 0)
            cout << "-" << abs(b) << "x";
        else
            cout << " - " << abs(b) << "x";
    }
    else if (b > 0)
    {
        if (a == 0 && b > 0)
            cout << b << "x";
        else
            cout << " + " << b << "x";
    }
    if (c >= 1)
        cout << " + " << c << " = 0";
    else if (c < 0)
        cout << " - " << abs(c) << " = 0";
    else
        cout << " = 0";

    return 0;
}
For your less than zero checks to set the coefficient negative, why not just let it print it. For example, if they input -5 you should be able to just print that. the abs() call is useless if you are adding the negative to it. Depending on organization you may want to have it fall into categories based on a and do checks that way. Like using a switch based on a case. case 1, -1, default (if you follow the above about abs() you can have the default) and then do the b cases inside of that, ridding you of the if(a == 0) check in two cases. You may also want to break it up as multiple functions for ease of writing. Depending on what this is for speed may not be an issue, and if you want practice using multiple functions breaking it up that way would be reasonable.
Or you could do it this way...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <cmath>

using namespace std;

int main( )
{
    int a, b, c;

    cout << "Enter a: ";
    cin >> a;

    cout << endl;

    cout << "Enter b: ";
    cin >> b;

    cout << endl;

    cout << "Enter c: ";
    cin >> c;

    cout << endl;

    if( a == -1 )
    {
        cout << " -" << "x^2";
    }
    else
        if( a < 0 )
        {
            cout << " -" << abs(a) << "x^2";
        }
        else
            if( a > 0 )
            {
                cout << a << "x^2";
            }
            else
                if( a == 0 )
                {
                    cout << " ";
                }

    if( b == -1 )
    {
        cout << " - " << "x ";
    }
    else
        if( b < 0 )
        {
            cout << " - " << abs(b) << "x ";
        }
        else
            if( b > 0 && a == 0 )
            {
                cout << b << "x ";
            }
            else
                if( b > 0 )
                {
                    cout << " + " << b << "x ";
                }
                else
                    if( b == 0 )
                    {
                        cout << " ";
                    }
    if( c > 0 && b == 0 )
    {
        cout << c;
    }
    else
        if( c < 0 )
        {
            cout << " - " << abs(c);
        }
        else
            if( c > 0 )
            {
                cout << " + " << c;
            }
            else
                if( c == 0 )
                {
                    cout << " ";
                }

    if ( c == 0 && b == 0 && a == 0 )
    {
        cout << " 0 ";
    }


    cout << " = 0 " << endl;



    return( 0 );
}


Last edited on
No offense, but all of this code is an ugly, tangled mess of if chains, a lot of which can be removed.

You're doing more or less the same code for 3 tasks. Rather than copy/pasting that code 3 times, put the code in a function and call it. Functions are your friend.

If you ever find yourself copy/pasting code, or writing code that's very similar to code you already wrote, or having a million if/else chains.... these are usually indications that you're going about a problem the wrong way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//  assuming we want to print "4x^2"
//  'num' is the 4
//  'var' is the "x^2"
//  first is true if this is the first monomial in the polynomial
//     and is false otherwise.
//
//  note first is passed by reference and is changed by the function
//      the function will set it to false if this monomial is printed

void PrintMonomial(int num,const char* var,bool& first)
{
    //  if the num is zero, we have nothing to print
    if(!num)
        return;
        
    // see if the number is negative.  If so, make it positive
    //  but keep track of the sign
    bool neg = (num < 0);
    if(neg)
        num = -num;  // num is now always positive

    // we will need different logic if this is the first
    //  monomial or not.  IE:
    //  " + 3x" vs. "3x" or
    //  " - 3x" vs. "-3x"
    if(first)
    {
        // print the sign only if it's negative
        if(neg)         cout << '-';
        
        first = false;  // now set 'first' to false so that future monomials are not printed as the first
    }
    else
    {
        // here we need to put spaces before and after the +/-
        if(neg)        cout << " - ";
        else           cout << " + ";
    }
    
    // now, just print the number if it's > 1
    if(num > 1)     cout << num;
    
    // and print the variable and exponent
    cout << var;
}

// now the rest is simpe

int main()
{
    int a, b, c;

    // ...get a,b,c from user here...

    bool first = true;
    PrintMonomial(a,"x^2",first);
    PrintMonomial(b,"x",first);
    PrintMonomial(c,"",first);

    // as an added bonus, if a,b,c were all zero, nothing was printed, so 'first' will still be true here
    if(first)
        cout << "0";

    return 0;
}



EDIT: I just realized this code will print the "1" which isn't desirable. I'll update it if I have time, but right now I have to get ready for work!


EDIT 2: Fixed
Last edited on
Thanks for this insight I would of never thought of doing it that way... but then again I don't know points to well.
Topic archived. No new replies allowed.