【问题标题】:C++ Very Beginner Message-Help AppreciatedC++ 非常初学者的消息-帮助赞赏
【发布时间】:2021-09-26 04:28:41
【问题描述】:

基础知识:使用 Visual Studio Code 的 C++,错误消息在终端中,“问题”或“输出”部分中没有任何内容。

#include <iostream>

using namespace std;

int main() 

{  

    int number;
    cout << "Enter your age: ";
    cin >> number;
    cout << "Your age is: " << number;
    return 0;
}
Basics: Visual Studio Code, C++

When I ran the above code it responded with the following in the terminal: "Enter your age: 22 Your age is: 22%"

Does this mean the user would only see 22 or would they see 22%?

【问题讨论】:

  • 错误信息很明确:“use of undeclared identifier 'cout'; 你是说'std::cout'吗?”
  • 您发布的代码不是编译器抱怨的代码。 std:cout &lt;&lt; "Hello world!"; 没有出现在此代码中任何地方。无论如何,我在我的 vscode 中使用了完整的 clang 命令行工具的 xcode-select 安装,并且从未遇到过这个问题。
  • 问题是缺少换行符,而你的 shell(可能是 zsh)告诉你很多。

标签: c++


【解决方案1】:

我们需要使用#include &lt;iostream&gt; 来使用允许我们在屏幕上打印输出的 cout。

编辑:

缺少双重std:: 但是,由于您已经提到 using namespace std,因此不需要在前面加上 std::

编辑 2:

下一行被附加在同一行问题上—— 要修复它,我们需要在 cout 之后添加一个换行符

   ...
    int number;
    cout << "Enter your age: ";
    cin >> number;
    cout << "Your age is: " << number << "\n";
    return 0;
    ...

【讨论】:

  • 我确实包含了它。它位于第一个灰色框的最顶部。它还有什么问题?
  • 错误日志是否与实际代码一致?我在上面的代码中找不到任何 Hello world。
  • 通过阅读我之前的代码,它看起来好像程序本身有问题。我重新启动程序并运行。新问题,当我运行上面的代码时,它在终端中回复了以下内容:“输入您的年龄:22 您的年龄是:22% alexandermorse@Alexanders-MacBook-Air C++ Practice %”这是否意味着用户只会看到 22还是他们会看到 22%?
  • 用户会看到 22,但是由于我们没有明确添加像 ... number&lt;&lt; "\n"; 这样的新行,因此控制台在同一行上进行了下一个输出。所以只需在最后一个 cout 中添加一个新行
  • 请更正您的问题,我会更新我的答案,如果您满意,请将答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 2021-04-11
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多