2

I have a program that executes basic aritmetic operations.

First the operator is inputed, then the two numbers of the operation.

The problem is, if I input, for example, "plus" or other string as the Oper character, for example:

plus 4 10

instead of

+ 4 10

it enters an infinite loop, and doesn't reset or stop for new inputs. Where does it go wrong?

Here is the code:

#include <iostream>
using namespace std;

void calc()
{
    char Oper;
    int num1, num2;
    cout << "Enter operator and two numbers: ";
    cin >> Oper >> num1 >> num2;
    if (Oper == '+')
        cout << num1 + num2 << endl;
    else if (Oper == '/')
        cout << num1 / num2 << endl;
    else if (Oper == '*')
        cout << num1 * num2 << endl;
    else if (Oper == '-')
        cout << num1 - num2 << endl;
    else
        calc();
}

int main()
{
    while (true)
    {
        calc();
    }
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
Eso
  • 45
  • 6
  • 2
    Because you have an infinite loop: `while(true)`. You also have a recursive call to `calc` in that `else` block. And what do you mean by *"and doesn't reset"*? – Ardent Coder Apr 09 '20 at 11:56
  • I mean shouldnt i get an option to assign it a new value because of the cin >> Oper? I mean that it should get a NULL value because of the reset: char Oper; once again. @ArdentCoder . also the calc() in else is just to loop back so i can add new values because of the wrong input by the user – Eso Apr 09 '20 at 11:59
  • Out of your unexpected *while* I encourage you to replace at least one of the two numbers by something else like *aze* as input and to appreciate the behavior ;-) – bruno Apr 09 '20 at 12:01
  • @EnesZulic *Re: "shouldnt i get an option to assign it a new value"* Yes, you will get that from your code, didn't you run the code or you mean something else? *Re: "it should get a NULL value"* No, `char Oper;` is uninitialized, it can be anything. – Ardent Coder Apr 09 '20 at 12:01
  • @EnesZulic when you reenter in *calc* it is exactly like when you enter the very first time. What is you *real* problem ? – bruno Apr 09 '20 at 12:04
  • @ArdentCoder it doesnt work it just spams the first string from the void... – Eso Apr 09 '20 at 12:04
  • 1
    @EnesZulic I'm not able to understand you. Edit your question with the ***actual output** vs your **expected output***, which will make things clear to us. – Ardent Coder Apr 09 '20 at 12:05
  • @ArdentCoder It is the exact code. And if I enter a string for the char assignment it spams the 1st string from the void. I think this is as clear as I can get. – Eso Apr 09 '20 at 12:07
  • @EnesZulic Ardent Coder is very true, edit your question to give an execution (not sentences describing it) and what you expected. For us your program does what it must do – bruno Apr 09 '20 at 12:09
  • @EnesZulic "*I enter a string for the char assignment it spams the 1st string from the void*" what string for instance ? I cannot understand what you mean by "*spams the 1st string from the void*". – bruno Apr 09 '20 at 12:18
  • @bruno "Enter operator and two numbers: ".. i mean isnt it obvious? – Eso Apr 09 '20 at 14:00
  • @EnesZulic yes it is, but the non obvious topic is : what is your problem. But you know, it is for you, *you* request help, if we cannot because you are not enough clear this is not a problem for *us* ;-) – bruno Apr 09 '20 at 14:01
  • @bruno the problem is that the program goes on a loop without the requirement of the user entering the values (the "cin >>..." part) so the string "Enter operator and two numbers: " is getting executed to the infinite. There is a "cin.break" followed by cin.clear which helps. – Eso Apr 09 '20 at 14:12
  • @EnesZulic if I well understand for me you have that behavior if you enter a non number when a number is expected, this is exactly what I said in my very first remark : **Out of your unexpected while I encourage you to replace at least one of the two numbers by something else like aze as input and to appreciate the behavior ;-)** This is that ? – bruno Apr 09 '20 at 14:15
  • The problem was if the char gets a string value, nothing with the numbers.. @bruno – Eso Apr 09 '20 at 14:19
  • @EnesZulic "*char gets a string value*" I cannot understand that, how a char can gets a string rather than just a char ? – bruno Apr 09 '20 at 15:00

1 Answers1

1

Considering a plus 4 10 input, p will be assigned to Oper, then operator >> will try to assign lus to the following variables, which will fail, because they are expecting int values, failbit flag will be set and you will enter the infinite loop, failbit will not be reseted.

To avoid this you should use a condition to reset failbit in the case of a bad input. You can use clear:

Live sample

#include <limits> //for numeric_imits and max()
//...
void calc()
{
    char Oper;
    int num1, num2;
    cout << "Enter operator and two numbers: ";
    cin >> Oper >> num1 >> num2;

    if (cin.fail()){  //condition to reset cin flags in case of a bad input
        cout << "Bad input\n";
        cin.clear();          //reset failbit
        cin.ignore(numeric_limits<streamsize>::max(), '\n'); //ignore everything till newline

        return;
    }

    if (Oper == '+')
        cout << num1 + num2 << endl;
    //...

Side notes:

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • @anastaciu It kinda works but the cout << "Enter operator and two numbers: "; gets executed one extra time before it clears and does the function again. – Eso Apr 09 '20 at 14:06
  • @EnesZulic, it will repeat itself as you input more characters, one way to solve this is to clear the buffer instead, I edited the question to account for that. – anastaciu Apr 09 '20 at 14:15