c++ - 编写一个程序,提示用户输入五个十进制数

标签 c++

这就是问题。

编写一个程序,提示用户输入五个十进制数。然后程序应将五个十进制数相加,将总和转换为最接近的整数 m 并打印结果。

这是我到目前为止得到的:

// p111n9.cpp : Defines the entry point for the console application.
//

#include <iostream>
using namespace std;

double a, b , c , d , e, f;

int main(int argc, char* argv[])
{
 cout << "enter 5 decimals: " << endl;
 cin >> a >> b >> c >> d >> e;
 f = a + b + c + d + e;
 return 0;
}

现在我只需要将 sum(f) 转换为最接近的整数 m 并打印结果。我该怎么做?

最佳答案

"declare m" means say

int m;

如果你说

m = (int)f; // it means the int value of f is assigned to m.

这里甚至不需要强制转换:

m=f; //works just as well

现在你可以打印m了

cout<<m;

关于c++ - 编写一个程序,提示用户输入五个十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2603611/

相关文章:

c++ - 为 lldb 中的大型数据结构设置观察点

c++ - 在读取包含空白表格的 .txt 文件时需要帮助吗?

c++ - 模板基类 C++ 中的重写函数指针问题

c++ - 关于容器中元素的引用

c++ - 检测是否将 int 转换为枚举会导致非枚举值

c++如何在运行时获取COM对象Coclass的progid

c++ - 如果构造函数被显式默认或删除,为什么自 C++20 以来聚合初始化不再起作用?

c++ - 类中的 Boost::Mutex 不是线程安全的

c++ - DrawTextA() 的结果不理想

c++ - 如何在 Visual Studio 2008 中键入 ':' 时禁用自动缩进