0

My answer is for some reason +1 off every time any help would be appreciated

https://code.sololearn.com/cU1TZbkYGXNq This happens when I start with a positive integer.

c++

2/3/2020 8:19:03 PM

Haze

8 Answers

New Answer

+1

int sum=n; Why you assigning n to sum? int sum=0; If you your summing only negative numbers.. First cin for n, has no use anywhere.. You must enter 0, to terminate while loop at end..

+1

I originally had int sum = 0; however it was still giving me +1 to the answer. -1 -2 -3 1 2 3 0 returns -5 when int sum = 0;

+1

the very first one? how is it initialized then?

+1

Remove the 1st cin, then initialize n other than 0. Or else use do while like int n; int sum=0; do{ cin>>n; if(n<0) sum+=n; }while(n!=0);

+1

how do i keep values greater than 0 from being input into n though?

+1

cause even with the do while it inputs all numbers instead of just the negatives do { cin >> n; if (n < 0); sum += n; } while (n != 0); Cout << “Sum of negatives: “ << sum << endl; it inputs every number even if its not < 0??

+1

No. I only calculates negatives... Can you update your code by what you are trying? And after if(n<0); error, remove ;. there should be no semi colon,..

0

-1 is taken into n. Remaing -2 + -3 = -5 So remove first outside cin>>n ;