mercredi 1 juillet 2020

Unable to detect enter key in C++

Now, before this question gets marked for duplicate. I have already gone through most of the questions and their relative answers of C++. These are the links that I have tried and none of them work for me. It maybe because they are using an older version of C++, and I have the latest version of C++. Here are the links that I have tried:

Now, with the duplicates out of the way. I am making an expression calculator. So, for example if the user input is: 2+2*6*9/9, then the output should be 14.
The code where I suspect that the problem lies is in:

        cout << "Enter an expression: ";
        double l_Value = 0, r_Value = 0, result = 0, count = 0, previous_number;
        char op;
        while (cin >> l_Value) { // 1+2*3+6-4/2+3
            if (!cin) {
                error("Invalid operand entered!");
            }
            else {
                bool is_Error = 0; // false
                vector<double> numbers;
                numbers.push_back(l_Value);
                previous_number = l_Value;
                while (cin >> op) { // using 'x' works fine!
                    if (op == 'x') {
                         break;
                    }
                   cin >> r_Value;
                   switch (op)
                   {
                   case '+':
                       numbers.push_back(r_Value);
                       previous_number = r_Value;
                       break;
                   case '-':
                       numbers.push_back((-1 * r_Value));
                       previous_number = (-1 * r_Value);
                       break;
                   case '*':
                       numbers.pop_back(); // take out the number
                       r_Value *= previous_number;
                       numbers.push_back(r_Value);
                       previous_number = r_Value;
                       break;
                   case '/':
                       if (r_Value == 0) {
                          error("Sorry, division by zero has occured. Please re-evaluate your expression!\n");
                           is_Error = 1; // true
                           break;
                       }
                       else {
                           numbers.pop_back(); // take out the number
                           previous_number /= r_Value;
                           numbers.push_back(previous_number);
                           break;
                       }
                   }
               }
               if (!is_Error) {
                  for (int i = 0; i < numbers.size(); i++) {
                  result += numbers[i];
               }
               cout << result << '\n';
            }
            numbers.clear();
            result = 0;
            l_Value = 0;
            r_Value = 0;    
        }
        cout << "Enter an expression: ";
      }

None of the links above seemed to work for me.
When I press the Enter key, it expects me to give another input, and that is not supposed to happen. So when I used
cin.get() == 'n' or cin.get() == (int)'\n', it expects for another input. But, when I have an 'x' at the end of the expression, it works perfectly fine. So, I need the "cin" operator to help me detect an Enter character at the end of the expression and then terminate the program.
Here, is a sample run of a program with 'x':

[![running as x-terminator][1]][1]
[1]: https://ift.tt/3gjpwvU

When I try the above solution such as "cin.get() == '\n':

running as enter-terminator
Then, I thought that maybe it is reading the null character and so, I tried if (op == '\0'):

null character terminator

For the enter key and null character I had to press Ctrl+Z to terminate the program. Please help!

Aucun commentaire:

Enregistrer un commentaire