• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

c++ question.

Peks

Member
my objective is to gather data from 10 numbers inputted from the user, and add them all...as well as add the negatives and add the positives.

i can gather the data and add them all, but how do i only add negatives together? some kind of if/else statement?
 
#include <iostream>

int main(void) {

const int size = 10;
int data, pos = 0, neg = 0;

for(int i = 0; i < size; i++) {
cout << "number " << i+1 << ": ";
cin >> data;
if(data > 0)
pos += data;
else if(data < 0)
neg += data;
}

cout << "positive sum: " << pos << endl
<< "negative sum: " << neg << endl
<< "total : " << pos + neg << endl;
}
 
#include <iostream.h>

int main()
{
int counter = 0, pos = 0, neg = 0, input;
while ( counter++ < 10 )
{
cin >> input;
if ( input < 0 ) neg+=input;
else pos+=input;
}
cout << "Pos = " << pos << endl;
cout << "Neg = " << neg << endl;
cout << "Total = " << pos + neg << endl;

return 0;
}
 
Back
Top