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?
*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.
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?
#include <string>
#include <iostream>
usingnamespace 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;
}
elseif ((counter = 2))
{
PolyNums[2] = coefficient;
PolyNums[3] = exponent;
}
elseif ((counter = 3))
{
PolyNums[4] = coefficient;
PolyNums[5] = exponent;
}
elseif ((counter = 4))
{
PolyNums[6] = coefficient;
PolyNums[7] = exponent;
}
elseif ((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.
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:
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: