Issue with loops in my code

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?

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
104
105
106
107
108
109
110
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace 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;
}
1
2
3
  if (input == "2" || input == "3" || input == "4" && a+b+c+d == 0){ 

}


Thats your problem

You are mixing your && operator with your || or operators.

try this

if (input == "2" || input == "3" || (input == "4" && a+b+c+d == 0)){
Last edited on
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?
Last edited on
Ok, well I tried your suggestion, but I'm still running into the same issue?
Topic archived. No new replies allowed.