-11

cmd input and output

I'm new to c++ and I just started using argc and argv for my project. However, when I try to print out argv like in the picture, it keeps removing the character ^ or asks me for more. I'm using visual studio and I just updated it to the latest version.

Ali Heikal
  • 3,790
  • 3
  • 18
  • 24
Hoang_10
  • 11
  • 3
  • 4
    please include code and output as text in the question. Not everybody can follow links and not everybody can view images, and nobody can copy-paste the image to compile it (see [here](https://stackoverflow.com/questions/5508110/why-is-this-program-erroneously-rejected-by-three-c-compilers)) – 463035818_is_not_an_ai Jan 07 '20 at 14:41
  • 1
    `^` means something in the command window. Type echo `^^` to see this in cmd.exe – drescherjm Jan 07 '20 at 14:41
  • there is nothing much with the code really. I just enter the input via cmd and print out argv[1] using cout << argv[1] < – Hoang_10 Jan 07 '20 at 14:45
  • 1
    `^` is cmd's escape character. See [here](https://ss64.com/nt/syntax-esc.html), for instance. – molbdnilo Jan 07 '20 at 14:46
  • 2
    The code is not the problem. The problem is an understanding of `cmd.exe` behavior. To avoid this behavior make sure the user uses double quotes around the argument and remove the outer quotes in your c++ code. For example look at `echo "^^"` versus `echo ^^` in cmd.exe – drescherjm Jan 07 '20 at 14:52
  • 1
    See the section _Parsing C++ command-Line arguments_ at https://learn.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=vs-2019 –  Jan 07 '20 at 17:42
  • This is important from the previous comment / link ***The caret character (^) is not recognized as an escape character or delimiter. The character is handled completely by the command-line parser in the operating system before being passed to the argv array in the program.*** – drescherjm Jan 07 '20 at 20:31

1 Answers1

2

That's because in cmd, ^^ is an escape sequence for ^. So if you enter ^^ it will only send one ^ to your program, as explained here.

You can see this if you put echo ^^ into the command line, which will print ^.

The reason why it asks for more sometimes is because you inputted an odd amount of ^, so the escape sequence isn't complete any you have to enter more characters to fix that.

Blaze
  • 16,736
  • 2
  • 25
  • 44