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.

3rd Feb 2020, 8:19 PM
Haze
8 Answers
+ 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..
3rd Feb 2020, 8:31 PM
Jayakrishna 🇮🇳
+ 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;
3rd Feb 2020, 8:34 PM
Haze
+ 1
the very first one? how is it initialized then?
3rd Feb 2020, 8:52 PM
Haze
+ 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);
3rd Feb 2020, 9:22 PM
Jayakrishna 🇮🇳
+ 1
how do i keep values greater than 0 from being input into n though?
3rd Feb 2020, 10:55 PM
Haze
+ 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??
4th Feb 2020, 12:14 AM
Haze
+ 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,..
4th Feb 2020, 6:58 AM
Jayakrishna 🇮🇳
0
-1 is taken into n. Remaing -2 + -3 = -5 So remove first outside cin>>n ;
3rd Feb 2020, 8:39 PM
Jayakrishna 🇮🇳