【问题标题】:Compliling C++ program in Visual Studio using g++ compiler generates % at the end使用 g++ 编译器在 Visual Studio 中编译 C++ 程序最后生成 %
【发布时间】:2020-09-15 21:17:38
【问题描述】:

使用 g++ 编译器在 Visual Studio Code 中编译我的程序时,生成的输出带有 % 符号。

#include <iostream>

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

终端截图:

我已经删除了问号,所以我认为没有控制字符。

【问题讨论】:

  • 请将您的代码包含为formatted code block 而不是图像。
  • 我怀疑你的输出后面的% 是你终端的提示,因为你在cout 之后没有endl。程序运行完毕后你有没有按回车键?
  • cout 行末尾和Hello 行上添加另一个cout

标签: c++ visual-studio-code compiler-errors


【解决方案1】:

On POSIX compliant systems, individual lines are by convention delimited by a newline character. zsh(这是您正在使用的 shell)使用 % 来指示缺少终止换行符的输出。

尝试在您的 shell 中输入 echo -n hello 以复制该行为。

要解决此问题(并删除 % 输出),请在代码输出中添加换行符:

…
std::cout << "Hello World!\n";
…

【讨论】:

    【解决方案2】:

    你是在 *nix 系统上写的吗?您没有在“hello world”之后打印行尾,因此 % 符号可能是您的命令行提示符。

    【讨论】:

    • 提示告诉你是哪个系统:macOS。
    • 提示说明你使用的是什么shell,这不是一回事。从技术上讲,macOS 是一个 *nix 系统。
    • 我的意思是,提示指示您希望它指示的任何内容。 OP 的 提示符(在屏幕截图中 — 单击链接)显示了他们正在使用的 系统(技术上是计算机名称),而不是 shell。并且 OP 的 shell (zsh) 确实默认使用 % 作为提示终止符,但是在输出之后显示的 % 不是提示的一部分,它是 zsh 在没有换行时显示的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    相关资源
    最近更新 更多