"Parse issue, expected expression" when using counter

Hi, I'm trying to write a piece of code, but am having a problem using counter, and I have no idea what's going on! (The code's not finished yet, but it should be able to run as a program regardless). I'm getting "Parse issue" "expected expression error" for all of the else if (counter = __) lines. Please help! What am I doing wrong?

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

#include <string>
#include <iostream>

using namespace std;

class Polynomial
{
private:
    int coefficient;
    int exponent;
    int counter;
public:
    Polynomial(int coefficient, int exponent);
    void set();
    void get();
    int PolyNums[10];
};

Polynomial::Polynomial(int c, int e)
{
    coefficient = c;
    exponent = e;
}

void Polynomial::set()
{
    for (int counter = 1; counter <= 5; counter++)
    {
        int coefficient;
    cout << "Input the polynomial integer coefficient: ";
    cin >> coefficient;
    
    int exponent;
    cout << "Input the polynomial integer exponenet: ";
    cin>> exponent;
    
    if ((counter = 1))
        PolyNums[0] = coefficient;
        PolyNums[1] = exponent;
    else if ((counter = 2))
        PolyNums[2] = coefficient;
        PolyNums[3] = exponent;
    else if ((counter = 3))
        PolyNums[4] = coefficient;
        PolyNums[5] = exponent;
    else if ((counter = 4))
        PolyNums[6] = coefficient;
        PolyNums[7] = exponent;
    else if ((counter = 5))
        PolyNums[8] = coefficient;
        PolyNums[9] = exponent;
    }
    
    
    
}

void Polynomial::get()
{
    cout << PolyNums[0] << "x^" << PolyNums[1] <<endl;
    
}


int main()
{
    int coefficient = 0;
    int exponent = 0;
    
    Polynomial Polynomial(coefficient, exponent);
    Polynomial.set();
    cout << "The polynomial expression is: ";
    Polynomial.get();
    
}


*note, I am only concerned with fixing this aspect of the code.

Thanks again!
*note, I am only concerned with fixing this aspect of the code.


Then I won't point out that you're not using comparisons in the if expressions and that the local variable counter in Polynomial::set hides the class member of the same name.

1
2
3
4
5
6
    if ((counter = 1))
        PolyNums[0] = coefficient;
        PolyNums[1] = exponent;
    else if ((counter = 2))
        PolyNums[2] = coefficient;
        PolyNums[3] = exponent;


is equivalent to:

1
2
3
4
5
6
7
8
9
    if ((counter = 1))
        PolyNums[0] = coefficient;

    PolyNums[1] = exponent;
    
    else if ((counter = 2))
        PolyNums[2] = coefficient;
        
    PolyNums[3] = exponent;


As you can see, the else is not paired up with the previous if.

You want:
1
2
3
4
5
6
7
8
9
10
    if ((counter = 1))
    {
        PolyNums[0] = coefficient;
        PolyNums[1] = exponent;
    }
    else if ((counter = 2))
    {
        PolyNums[2] = coefficient;
        PolyNums[3] = exponent;
    }

Last edited on
Alright, thanks a lot for that part it worked, but it's still printing 0 when I call my array to print, so it's not assigning each new coefficient/exponent to the array. I also moved the for statement to main. How do I manipulate the code to make sure that the array is actually being assigned? Does this have to do with what you mentioned early in your post?

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
#include <string>
#include <iostream>

using namespace std;

class Polynomial
{
private:
    int coefficient;
    int exponent;
    int counter;
public:
    Polynomial(int coefficient, int exponent);
    void set();
    void get();
    int PolyNums[10];
};

Polynomial::Polynomial(int c, int e)
{
    coefficient = c;
    exponent = e;
}

void Polynomial::set()
{
    if ((counter = 1))
    {
        PolyNums[0] = coefficient;
        PolyNums[1] = exponent;
    }
    else if ((counter = 2))
    {
        PolyNums[2] = coefficient;
        PolyNums[3] = exponent;
    }
    else if ((counter = 3))
    {
        PolyNums[4] = coefficient;
        PolyNums[5] = exponent;
    }
    else if ((counter = 4))
    {
        PolyNums[6] = coefficient;
        PolyNums[7] = exponent;
    }
    else if ((counter = 5))
    {
        PolyNums[8] = coefficient;
        PolyNums[9] = exponent;
    }
}

void Polynomial::get()
{
    cout << PolyNums[0] << "x^" << PolyNums[1] <<endl;
    
}


int main()
{
    int coefficient = 0;
    int exponent = 0;

    Polynomial Polynomial(coefficient, exponent);
    
    for (int counter = 1; counter <= 5; counter++)
    {
        int coefficient;
        cout << "Input the polynomial integer coefficient: ";
        cin >> coefficient;
        
        int exponent;
        cout << "Input the polynomial integer exponenet: ";
        cin>> exponent;
    
    Polynomial.set();
    }
    
    cout << "The polynomial expression is: ";
    Polynomial.get();

}


*edit: I've fixed this by making the for brackets to not include the if else if statements, but then I get a print out of 0x^0 every time instead of what I'm trying to assign to the array.
Last edited on
Then I won't point out that you're not using comparisons in the if expressions


counter = 1 is an assignment. Of course, your compiler told you this at first, but then you surrounded it with parentheses to suppress the warning. When used in an if statement it always evaluates to true.

That makes your implementation of Polynomial::set() in the snippet above effectively:

1
2
3
4
5
6
7
void Polynomial::set()
{
    counter = 1 ;

    PolyNums[0] = coefficient;
    PolyNums[1] = exponent;
}


Naming a couple variables in main (coefficient and exponent) does not make them refer to the member variables of the same name in Polynomial. set() needs to have information fed to it in the form of a parameter or parameters.

You'd get equivalent behavior with this main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    Polynomial Polynomial(0, 0);
    
    for (int i = 1; i <= 5; i++)
    {
        int garbage;
        cout << "Input the polynomial integer coefficient: ";
        cin >> garbage;
        
        cout << "Input the polynomial integer exponenet: ";
        cin>> garbage;
    }
  
    cout << "The polynomial expression is: ";
    Polynomial.get(); 
}








Awesome, fixed it by adding those parameters to set (sorry about all these mistakes I'm new to C++). That said, I'm now having another issue. I added a new variable "count" to account for the if else statements, but when I print any of the array nodes, all I get is the last inputted values for coefficient and exponent. In addition, I printed my count number at the end to see if it was a problem with that, and the count prints 0 even though i have count = count++ in the program (also if the count number is equal to zero the entire time why is anything being placed into the array? The if else statements stipulates for count = 1-5 not zero). This is the changed code:

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

#include <string>
#include <iostream>

using namespace std;

class Polynomial
{
private:
    int coefficient;
    int exponent;
    int count;
public:
    Polynomial(int coefficient, int exponent, int count);
    void set(int coefficient, int exponent, int count);
    void get();
    int PolyNums[10];
};

Polynomial::Polynomial(int c, int e, int a)
{
    coefficient = c;
    exponent = e;
    count = a;
}

void Polynomial::set(int coefficient, int exponent, int count)
{
    if ((count = 1))
    {
        PolyNums[0] = coefficient;
        PolyNums[1] = exponent;
    }
    else if ((count = 2))
    {
        PolyNums[2] = coefficient;
        PolyNums[3] = exponent;
    }
    else if ((count = 3))
    {
        PolyNums[4] = coefficient;
        PolyNums[5] = exponent;
    }
    else if ((count = 4))
    {
        PolyNums[6] = coefficient;
        PolyNums[7] = exponent;
    }
    else if ((count = 5))
    {
        PolyNums[8] = coefficient;
        PolyNums[9] = exponent;
    }
}

void Polynomial::get()
{
    cout << PolyNums[0] << "x^" << PolyNums[1] << endl;
    
}


int main()
{
    int coefficient = 0;
    int exponent = 0;
    int count = 0;

    Polynomial Polynomial(coefficient, exponent, count);
    
    for (int counter = 1; counter <= 5; counter++)
    {
        
      count = count++;
    
        cout << "Input the polynomial integer coefficient: ";
        cin >> coefficient;

        cout << "Input the polynomial integer exponenet: ";
        cin>> exponent;
        
        Polynomial.set(coefficient, exponent, count);
    }
    
    cout << "The polynomial expression is: ";
    Polynomial.get();
    
    cout << "Count is " << count;

}


Once again, thanks cire you've been a huge help.
Do I need to mention it a third time? count = 1 is an ASSIGNMENT. It will ALWAYS evaluate to TRUE when used in an if condition.

And once again, the parameter count in Polynomial::set() is hiding the member variable of the same name.
Topic archived. No new replies allowed.