在c ++中返回true或false

Bob*_*non 0 c++ boolean

当我在c ++中运行bool类型的方法时,返回语句如下:

bool method() {
    return true;
}
Run Code Online (Sandbox Code Playgroud)

控制台没有输出.要获得输出,我必须这样做:

bool method() {
    cout << "true";
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?

Vla*_*cow 7

该程序已成功编译和执行,输出1为true的值.

#include <iostream>

bool method() {
    return true;
}
int main()
{
   std::cout << method() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

如果你想要而不是1,那么你可以写出真实的文字

#include <iostream>
#include <iomanip>

bool method() {
    return true;
}

int main()
{
   std::cout << std::boolalpha << method() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

问题可能还在于你的程序在执行后会关闭窗口并且你没有时间看到结果.你应该在程序的最后插入一些输入语句,它会等到你输入的东西.


Man*_*726 5

C++ 不像 python 那样是一种解释语言,它是一种编译语言。因此,您不必在解释器上编写函数调用,它会打印结果。您正在编译程序并稍后执行它。因此,如果您需要在程序中向控制台输出某些内容,则必须编写一条指令来执行此操作(就像std::cout <<这样做)。