0

I have been learning c++ recently and that's a simple calculator app I have made, the problem comes when I receive an operator and I start to validate that it's one of (+, -, *,). but it doesn't seem to work

    if(op != "+" || op != "*" || op != "/" || op != "-"){
      std::cerr << "Error Unsupported Operator";
      return 0;
    };

#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

// km = 1.6 * miles
// Enter miles: 5

int main(int argc, char** argv) {

    std::string num1, num2, op;
    double dNum1, dNum2;
    std::cout << "Enter first num: ";
    getline(std::cin, num1);
    std::cout << "Enter Operator: ";
    getline(std::cin, op);  
    if(op != "+" || op != "*" || op != "/" || op != "-"){
      std::cerr << "Error Unsupported Operator";
      return 0;
    };
    std::cout << "Enter second num: ";
    getline(std::cin, num2);
    dNum1 = std::stod(num1);   
    dNum2 = std::stod(num2);

    double result;
    
    if      (op == "+") result = dNum1 + dNum2;
    else if (op == "-") result = dNum1 - dNum2;
    else if (op == "*") result =  dNum1 * dNum2;  
    else if (op == "/") result =   dNum1 / dNum2;  

    // std::cout << dNum1 << " " << op << " " << dNum2 << " = " << result << "\n";
    printf("%.1f %s %.1f = %.1f", dNum1 , op ,dNum2, result);

    return 0;
}
  • I've never seen `!==` before. What do you suppose it does? – Mark Ransom Apr 26 '21 at 15:56
  • 1
    `!==` isn't an operator. You probably mean `!=`. But regardless, `op != "+" || op != "*" || op != "/" || op != "-"` isn't right either. No matter what value `op` is that statement will always be true. If `op` is "+", then `op != "*"`. If `op` is "*", then `op != "+"`, and so on. – Kevin Apr 26 '21 at 15:58
  • You haven't told us what the problem is. I _hope_ the first problem is that you get compilation errors. `printf("%.1f %s %.1f = %.1f", dNum1 , op ,dNum2, result);` would make your program have undefined behavior if it compiles. – Ted Lyngmo Apr 26 '21 at 16:05

1 Answers1

2

C++ doesn't have operator !==, you should use !=.

As @largest-prime-is-463035818 pointed out, you also should use &&.

if(op != "+" && op != "*" && op != "/" && op != "-")
  • 3
    code is wrong also with `!=`. The condition needs `and` not `or` – 463035818_is_not_an_ai Apr 26 '21 at 15:58
  • Yup! it worked, Don't you know why the other sol. didn't work ? – Mohamed Tarek Apr 26 '21 at 16:06
  • 1
    @MohamedTarek Please see the linked duplicate target, which explains why in some detail. – cigien Apr 26 '21 at 16:06
  • @MohamedTarek Also, fix that `printf`. It can't handle a `std::string` – Ted Lyngmo Apr 26 '21 at 16:08
  • @TedLyngmo yes, it doesn't output clearly and I cant solve it. could you suggest a fix? – Mohamed Tarek Apr 26 '21 at 16:13
  • 1
    @MohamedTarek I suggest using `std::cout << ...` instead, but if you must use `printf` you should use the argument `op.c_str()` instead `op`. Also, turn on more compiler warnings. That code would _usually_ generate errors or warnings. – Ted Lyngmo Apr 26 '21 at 16:18
  • 1
    printf() is a `c` function. It does not handle classes at all. `std::string` is a class. Related: [https://stackoverflow.com/questions/10865957/printf-with-stdstring](https://stackoverflow.com/questions/10865957/printf-with-stdstring) – drescherjm Apr 26 '21 at 16:20