怎么办?用户输入一些数字,然后另一个函数返回这些数字相加的结果? (C/C++)

发布于 2024-09-15 08:14:56 字数 138 浏览 5 评论 0原文

怎么办?用户输入一些数字,然后另一个函数返回这些数字相加的结果? (C/C++)

例如,用户输入 3 4 7,然后他会在屏幕 14 上看到打印内容 另一位,用户输入 5 6 并得到 11

我是 C 编程初学者,所以我请你帮助我,请

how to do? User type some numbers and then another function return result of addition of these numbers? (C/C++)

for example, user type in 3 4 7 and then he see printed on screen 14
another one, user type 5 6 and gets 11

I'm a beginner in programming C, so I ask you to help me, please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡看悲欢离合 2024-09-22 08:14:56

在高层次上:

  • 获取输入字符串,
  • 用空格将其分割
  • ,将每个部分解析为整数,
  • 添加整数,
  • 返回结果,

这很简单,无需实际发布代码,我不会这样做,因为我怀疑这是这样的家庭作业。

At a high level:

  • take the input string
  • split it by white space
  • parse each part to an integer
  • add the integers
  • return the result

This is as simple as it can get without actually posting code, which I won't do as I suspect this is a homework assignment.

月依秋水 2024-09-22 08:14:56

因为参数的数量没有指定,我认为你需要 va_list 但正如你所说,你是 C++ 新手,所以你可以这样写

#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    int sum=0;
    int t;
    while ((cin>>t)!=EOF)//in windows EOF is  ctrl+z
    {
        sum+=t;
    }

    cout<<sum<<endl;

    return 0;
}

because the number of arguments is not specified i think you need va_list but as you said you are new in c++ so you can write it like this

#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
    int sum=0;
    int t;
    while ((cin>>t)!=EOF)//in windows EOF is  ctrl+z
    {
        sum+=t;
    }

    cout<<sum<<endl;

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文