0

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 ?.

Itachi Uchiwa
  • 3,044
  • 12
  • 26

2 Answers2

7

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.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • But When I pass `^^`it didn't work for me e.g: `calc 5 ^^` when I press enter I get: `argc = 3 op: ^` and not `25`? – Itachi Uchiwa Sep 05 '19 at 12:44
  • 1
    @ItachiUchiwa Works for me when I fix other problems in your code that prevent it from compiling. Show us your _actual_ [mcve], not some facsimile with an unknown quantity of alterations. – Lightness Races in Orbit Sep 05 '19 at 13:19
  • @LightnessRacesinOrbit: This is all the code. What are the errors in the code? You'd rather state them. – Itachi Uchiwa Sep 05 '19 at 13:52
4

The ^ character is nothing special to c++. There's nothing wrong with processing it as any other character. Your problem stems from your shell. However, you're not telling us which shell you're using. As you're having trouble with ^ I am guessing you run your program in a Windows environment. In Windows command prompt the ^ is the escape character. You're escaping the next character (cmd prompt discards the ^ and treats the next character as just that: a character) so that's why you're not finding the ^ itself in your program's arguments.

Some more info on special shell characters can be found here:

Nathilion
  • 309
  • 1
  • 7