Search code examples
c++argvargc

How to pass character `^` to my program from commnd prompt?


I have this simple example which is about a calculator:

  • Enter the first operand then the operator, If the operator is a unary then there's no need to enter the second one. So print the result e.g: the square if a number doesn't need second operand.

  • The input can be passed into the program from command prompt otherwise after program starts from input stream.

  • The reason why I want to pass the input as arguments to the program is I want to invoke my program from a command prompt sometimes, so I can issue: calc 57 + 12 + enter: I get 69.

  • The program works fine but when it comes to the square operator which in my case I've used ^ it is ok from input stream (std::cin) But if I pass it through command prompt I cannot?!

    int main(int argc, char* argv[]){
    
        int a = 0;
        int b = 0;
        char op = '\0';
    
        if(argc < 2){
            std::cout << "a: ";
            std::cin >> a;
            std::cout << "op: ";
            std::cin >> op;
    
            switch(op){
                case '^':
                    std::cout <<  a << " ^2 " << " = " 
                        << a * a << std::endl;
                break;
                case '+':
                    std::cout << "b: ";
                    std::cin >> b;
                    std::cout <<   a << " + " << b << " = " 
                    << a + b << std::endl;
                break;
                case '-':
                    std::cout << "b: ";
                    std::cin >> b;
                    std::cout << a << " - " << b << " = " 
                    << a - b << std::endl;
                break;
                case '*':
                    std::cout << "b: ";
                    std::cin >> b;
                    std::cout << a << " * " << b << " = " 
                    <<  a * b << std::endl;
                break;
                case '/':
                    std::cout << "b: ";
                    std::cin >> b;
                    std::cout << a << " / " << b << " = " 
                    << a / b << std::endl;
                break;
                case '%':
                    std::cout << "b: ";
                    std::cin >> b;
                    std::cout << a << " % " << b << " = " 
                    <<  a % b << std::endl;
                break;
            }
    
        }
        else{
            a = atoi(argv[1]);
            op = argv[2][0];
        }
    
        if(argc == 3){
            std::cout << "argc = 3" << std::endl;
            std::cout << "op: " << op << std::endl;
            switch(op){
                case '^':
                    std::cout <<  a << " ^2 " << " = " 
                        << a * a << std::endl;
                break;
            }
        }
        else
            if(argc == 4){
                b = atoi(argv[3]);
                switch(op){
                    case '+':
                        std::cout <<   a << " + " << b << " = " 
                        << a + b << std::endl;
                    break;
                    case '-':
                        std::cout << a << " - " << b << " = " 
                        << a - b << std::endl;
                    break;
                    case '*':
                        std::cout << a << " * " << b << " = " 
                        <<  a * b << std::endl;
                    break;
                    case '/':
                        std::cout << a << " / " << b << " = " 
                        << a / b << std::endl;
                    break;
                    case '%':
                        std::cout << a << " % " << b << " = " 
                        <<  a % b << std::endl;
                    break;
                }
            }
    
        std::cin.get()
        return 0;
    }
    
  • If I from command prompt issue: calc 7 ^ I didn't get 49 but the command asks me more ?.


Solution

  • This is actually not a C++ issue, it's a terminal issue. In cmd and windows batch ^ is the symbol for line continuations.

    To actually pass a ^ you have to escape it. And the symbol for escaping is ... [[drum roll]] ^. So you have to write ^^ in cmd to actually pass a ^ to your program.