Program addition help

Apr 6, 2011 at 10:19am
Hey Everyone,

I'm quite new to C++ and I'm doing a little assignment right now for school. Basically a program which adds the biggest and middle number when you enter 3 digits in. My program currently works but it will add the biggest and the smallest, I need it the other way around LOL.

I know my code to find the largest works (since it will find the largest no matter what order I enter it), but the code to find the 2nd largest might be my issue.

In my program, for it to find the second largest number I have:


if (num3 < largest)
largest2 = num3;

sum = largest + largest2;

I know the error is somewhere in there, but can't quite pick it out. Where did I go wrong?

Thanks.
Last edited on Apr 6, 2011 at 12:53pm
Apr 6, 2011 at 10:38am
use [ code ] [/ code ] tags.

if (num3<largest)

I think you meant to check if the last number is greater than the 2nd largest number. Try:

if (num3>largest2)
Last edited on Apr 6, 2011 at 10:39am
Apr 6, 2011 at 12:47pm
Oh no, we're meant to write it so that it finds the largest number, then the 2nd largest... well basically any way so that it finds the largest and second largest. So should it be incrementally where it finds the 2nd largest, then finds one bigger than that?
Apr 6, 2011 at 12:51pm
Oh thanks! I did exactly what you said and it works now.... in the end these tiny errors screw everything up. lol.
Last edited on Apr 6, 2011 at 12:51pm
Apr 6, 2011 at 11:27pm
Your tellin me... Try looking through 700 lines of code for possible causes of a SegFault.
Topic archived. No new replies allowed.