gpt4 book ai didi

c++ - 函数不返回值,但 cout 显示它

转载 作者:太空狗 更新时间:2023-10-29 23:26:15 24 4
gpt4 key购买 nike

我学习了一段时间的 C++,并尝试制作一个返回房间面积的简单函数。 return 语句不输出值,但是使用 cout 我可以看到结果。我在这里遗漏了什么吗?

#include <iostream>
using namespace std;

int Area(int x, int y);

int main()
{
int len;
int wid;
int area;
cout << "Hello, enter the length and width of your room." << endl;
cin >> len >> wid;
cout << "The area of your room is: ";
Area(len, wid);
return 0;
}

int Area(int len, int wid)
{
int answer = ( len * wid );
return answer;
}

最佳答案

std::cout用于在屏幕上打印数据。函数仅返回 值,因此 Area函数将返回要传递给 std::ostream::operator<< 的值打印它的功能。你需要写:

std::cout << Area(len, wid) << "\n";

关于c++ - 函数不返回值,但 cout 显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50390600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com