0

When compiling my program in Visual Studio Code using the g++ compiler, the output generated has the % sign.

#include <iostream>

int Main() {
  std::cout << "Hello World!";
  return 0;
}

Terminal screenshot:

here

I have deleted the question mark, so I think there is no control character.

Alexz S.
  • 2,366
  • 4
  • 21
  • 34
  • Please include your code as a [formatted code block](https://stackoverflow.com/help/formatting) instead of an image. – Pranav Hosangadi Sep 15 '20 at 21:20
  • 1
    I suspect the `%` after your output is your terminal's prompt, because you don't `endl` after you `cout`. Did you hit enter once after your program finished running? – Pranav Hosangadi Sep 15 '20 at 21:21
  • Add another `cout` line with `std::endl` at the end and on the `Hello` line – rioV8 Sep 15 '20 at 22:13

2 Answers2

2

On POSIX compliant systems, individual lines are by convention delimited by a newline character. zsh (which is the shell you’re using) uses % to indicate output that’s missing that terminating newline character.

Try entering echo -n hello on your shell to replicate the behaviour.

To fix this (and remove the % output), add a newline to your code’s output:

…
std::cout << "Hello World!\n";
…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
0

Are you writing this on a *nix system? You are not printing a end-of-line after "hello world", so the % symbol is likely your command-line prompt.

jkb
  • 2,376
  • 1
  • 9
  • 12
  • The prompt tells you which system it is: macOS. – Konrad Rudolph Sep 15 '20 at 21:23
  • The prompt indicates what *shell* you're using, which is not the same thing. And technically macOS is a *nix system. – jkb Sep 15 '20 at 21:29
  • I mean, the prompt indicates whatever you want it to indicate. *OP’s* prompt (in the screenshot — click on the link) shows the *system* they’re using (technically the computer name), not the shell. And OP’s shell (zsh) indeed uses `%` as the prompt terminator by default but the `%` being displayed after the output isn’t part of the prompt, it’s what zsh displays when there’s no newline. – Konrad Rudolph Sep 15 '20 at 22:11