Ok, I am having a minor issue with my code below. In short, after I enter a polynomial, if I evaluate the polynomial with a certain value x (choice 3), then choose selection 3 again after it's evaluated, the program gives me the cout "Please enter a polynomial first." The odd thing though is that if I choose selection 2 first (print the poly) and then choose 3 again, it works fine and evaluates properly. Can anyone tell me where my bug is, as I've spent like 20mins on it and am at a complete and utter loss?
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
usingnamespace std;
void MainMenu (){ // to print the main menu
cout << "MAIN MENU\n";
cout << "1. Input a polynomial\n";
cout << "2. Print a polynomial\n";
cout << "3. Evaluate a polynomial\n";
cout << "4. Solve for roots\n";
cout << "Press 0 to quit.\n";
cout << "Please select an option: ";
}
float a=0, b=0, c=0, d=0, e=0;
void PrintPoly (){ // to print the polynomial (options 2 or 3)
if (a != 0){
if (a > 0)
cout << "+" << a << "x^4";
else
cout << a << "x^4";
}
if (b != 0){
if (b > 0)
cout << "+" << b << "x^3";
else
cout << b << "x^3";
}
if (c != 0){
if (c > 0)
cout << "+" << c << "x^2";
else
cout << c << "x^2";
}
if (d != 0){
if (d > 0)
cout << "+" << d << "x";
else
cout << d << "x";
}
if (e != 0){
if (e > 0)
cout << "+" << e;
else
cout << e;
}
}
int main(){
cout << "COP 2271 POLYNOMIAL CALCULATOR\n\n";
MainMenu ();
string input = "-1";
cin >> input;
while (input != "0"){ // keeps this loop running until the user enters "0"
if (input == "2" || input == "3" || input == "4" && a+b+c+d == 0){ // situation where user attempts to choose a selection before having a polynomial
cout << "Please enter a polynomial first.\n";
cin >> input;
}
if (input != "0" && input != "1" && input != "2" && input != "3" && input != "4"){ // situation where selection is not a valid option
cout << "No such option.\n";
cin >> input;
}
if (input == "1"){ // option 1 - entering the polynomial
cout << "Please enter the polynomial, one coefficient after the other: ";
cin >> a >> b >> c >> d >> e;
cout << endl << endl;
MainMenu ();
cin >> input;
}
if (input == "2" && a+b+c+d != 0){ // option 2 - printing the polynomial
cout << "The polynomial you entered is ";
PrintPoly ();
cout << endl << endl;
MainMenu ();
cin >> input;
}
if (input == "3" && a+b+c+d != 0){ // option 3 - evaluating the polynomial
float x = 0; // value for 'x' in the equation
cout << "Please enter a value for x: ";
cin >> x;
cout << endl << "For x=" << x << ", the polynomial ";
PrintPoly ();
cout << " is equal to ";
float y = 0; // value for the solution to the evaluated polynomial
if (a != 0)
y += a * pow(x, 4);
if (b != 0)
y += b * pow(x, 3);
if (c != 0)
y += c * pow(x, 2);
if (d != 0)
y += d * x;
if (e != 0)
y += e;
cout << y << endl << endl;
MainMenu ();
cin >> input;
}
}
if (input == "0")
return 0;
}
Thanks for the info. I'll give it a try as soon as I finish the last of the code. What is it that my original code was read as and how is it that your suggested code is read differently?