简体   繁体   中英

How to make negative numbers not count when doing addition (C++)

i tried making a program like this for schoolwork but it dosent work can somebody help me please, i dont really know where to go

#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;

You may want to try something like this:

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

In the above code, terms are only added to the d variable if they are positive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM