首页 > 解决方案 > 加法时如何使负数不计入(C++)

问题描述

我试着为功课制作一个这样的程序,但它没有用,有人可以帮我吗,我真的不知道该去哪里

#include <iostream>

using namespace std;

int main()
{
   int a;
   int b;
   int c;
   int d;
   cout<<"enter 3 numbers"<<endl;
   cin>>a;
   cin>>b;
   cin>>c;
   d = a + b + c
   if ( a <= 1 - = 0 )
    cout<<d<<endl;
   if ( b < 1 - = 0 )
   cout<<d<<endl;
   else ( c < 1 - = 0 )



   return 0;

标签: c++

解决方案


您可能想尝试这样的事情:

int d = 0; // Initial value
if (a > 0)
{
    d = d + a;
}
if (b > 0)
{
    d = d + b;
}
if (c > 0)
{
    d = d + c;
}

在上面的代码中,只有d当它们是正数时,它们才会被添加到变量中。


推荐阅读