Running a loop for input

I am attempting to write some code that will run in a loop each time the input is specified numbers (or other characters). As I'm sure you can see from the code below, I am trying to make it so that the user will get a specific output each time they enter keys that are invalid. However, I have tried this a few different ways and I either end up having it run in an infinite loop without asking me for cin again, or even when I give it input that falls outside the parameters, it continues to stay stuck in the loop. Can someone please help me out here?

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
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int main()
{
    cout << "COP 2271 POLYNOMIAL CALCULATOR\n\n";
    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: ";
    int a=0, b=0, c=0, d=0;
    int input;
    cin >> input;

    while (input == 2 || 3 || 4 && a+b+c+d == 0){
        cout << "Please enter a polynomial first.\n";
        cin >> input;
    }

    while (input != 1 || 2 || 3 || 4){
        cout << "No such option.\n";
        cin >> input;
    }

    return 0;
    }
Last edited on
Common mistake:
input != 1 || 2 || 3 || 4 is parsed as (input != 1) ||( 2) || (3 )|| (4) as any nonzero value is true it is equivalent to input != 1 || true || true || true, after simpifying: input != 1 || true which is always true due to boolean operation properties.
so first what i see is on line 23 A+B+C+D == 0 (im not shore why you have that)

1
2
3
4
while (input == 2 && 3 && 4){
        cout << "Please enter a polynomial first.\n";
        cin >> input;
    } 


or do a 'do / while' loop.

1
2
3
4
 do{
        cout << "Please enter a polynomial first.\n";
        cin >> input;
    } while(input == 2 && 3 && 4 );  


Hope this helps
Last edited on
MiinNiPaa - While I appreciate the help, can you tell me the proper way to code multiple statements in a row like that then with the || operator?

freecody - I appreciate you trying to help, but it should not be "&&" but "||" for each of the numbers regarding the input. Like MiiNiPaa already explained, I don't know how to properly use the || operator. If I could figure that out, your code here would be really useful. Basically, all I need different is some way to make the "while" statement give conditions of the input being either 2 or 3 or 4, not 2 and 3 and 4 like your statement is giving me. If you can help me with that, I'd really appreciate it.
Also, when I run that code freecody (the 2nd one), i get an infinite loop. In addition, since it is a "do" statement, it is always running at least once, which I only want to happen if the conditions are met.
proper way is to explicitely compare values in each subcondition:
while(input !=1 && input != 2 /*etc.*/) (Note the &&. It says while input is neither of these. If we use || condition would be always true)
In your case as we only need to check if input in continuous range we can do this:
while(input < 1 || input > 4)
this is how i wrote the code if you dont want to use '&&'

//you can make it a while loop it will also work
1
2
3
4
5
6
7
8
9
10
    cout << "Please select an option: ";
    int input;

    do{
        cout << "Please enter a polynomial first.\n";
        cin >> input;
    } while(input == 2||input ==3||input ==4 );

    return 0;
    }
Last edited on
Thanks for the input guys. Now, how do I keep it from running in an infinite loop and actually wait to check for the next cin? No matter how I try to work out my while loops, it seems as soon as the loop gets information that initiates the loop, it just keeps running the cout over and over and doesn't give me a chance to put in a new input.
Ok, well I got my first loop working right with this code:
while (input == 2 || input == 3 || input == 4){
cout << "Please enter a polynomial first.\n";
cin >> input;
}

However, the second loop still runs infinitely without giving me the chance to put in any new cin, even though it is very similar to the first one:
while (input != 1){
cout << "No such option.\n";
cin >> input;
}

So, why am I getting an infinite loop in this one and what do I need to do in order to make it pause and wait for the cin before evaluating the value again?
What did you wrote before infinite looping began? Right now you do not have any protection against incorrect input: if user enters a letter instead of number, input will fail again and again trying to read this character
Thanks for the info. I originally had input set as an int, so it seems like I had the problem you mentioned. So long as I input an int, I realized it worked fine. However, after changing it to a string, now I am having issues with my while statements. Not sure the best way to go about dealing with this?

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
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int main()
{
    cout << "COP 2271 POLYNOMIAL CALCULATOR\n\n";
    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: ";
    int a=0, b=0, c=0, d=0;
    string input = 0;
    cin >> input;

    while (input == '2' || input == '3' || input == '4'){
        cout << "Please enter a polynomial first.\n";
        cin >> input;
    }
    while (input != '1'){
        cout << "No such option.\n";
        cin >> input;
    }

    return 0;
}
Last edited on
First of all now that you have a string, you should compare to strings, or string literals: input == "2"
Oh yes, I completely forgot. Thanks again. I rearranged some things and it is working good now, at least that part of my code. I really appreciate the help.
Last edited on
Topic archived. No new replies allowed.