code is producing wrong sum of numbers. Cant find error

hi all my code is producing a sum of 19 when it is meant to be producing a sum of 14? Can anyone see where my error is? need a fresh pair of eyes been staring at it for too long

#include <iostream>
using namespace std;

int main()
{
int next = 2, sum = 1;
while (next <= 5)
{
next++;
sum = sum + next;
}
cout << "The sum of 2 through 5 is " << sum << endl;
}
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) Look at the order of the actions you've written.

Hint: You're not calculating the sum of 2 through 5. You're calculating the sum of 3 through 6. Can you see why?
Last edited on
thank you and i apologize for not using the code tags, i am new here so still learning my way around
You're welcome - hope it helped you realise what's going wrong?
yes i did thank you. Ive been looking at it for so long my brain wasn't picking up what i was doing wrong but your hint helped me see it
Cool, glad it helped!
daniel7 - please start your own thread and post your problem there
@halleyc Google "rubber duck debugging".
thank you! I will do that
Topic archived. No new replies allowed.