PLEASE WHY AM I GETTING THE BUG IN THE CODE? [SOLVED] | SoloLearn: Learn to code for FREE!

+15

PLEASE WHY AM I GETTING THE BUG IN THE CODE? [SOLVED]

I am almost done with my C# lessons on sololearn and I decided to practise with some of the newly acquired ideas on stack collection. Why am I encountering the bug in the code? https://code.sololearn.com/cxlvndP3Bgou/?ref=app

9/29/2020 12:39:58 PM

👑 Tchybooxuur!

8 Answers

New Answer

+10

The error occurs because on line 48, in the foreach loop, you are modifying the stack while iterating on it. Just replace `foreach (string i in friends)` with `for (int i=0; i < friends.Count; i++)` And it will work fine https://code.sololearn.com/c2VFDrrrKNMq/?ref=app

+9

The mistake in the lines below, you should change tp another loop, because you modify your collection friends you cannot use foreach for it. foreach (string i in friends) Console.WriteLine("Pop: " + friends.Pop());

+9

It worked! Thanks XXX, Vladimir & elvisreal 👍🏼

+7

Not expert at cs, but it has to do with your try block https://code.sololearn.com/crdjMZbGInuS/?ref=app

+7

As said, Modifiying collection in Foreach causing error. Also for normal for loop use copy of friends.count as maxI= friends.count ; and use i<maxI. Because friends.count also modifed, you miss some elements poping.. Here Iterator works fine for modifying collections...

+6

Jayakrishna🇮🇳 Oh that's true. Thanks 👍🏼. I'll add that too.

+4

The 6-Step Technique To Reliably Fix Any Bug Step 1 – Replicate the bug yourself. ... Step 2 – Make sure you truly understand the problem. ... Step 3 – Fix the bug. ... Step 4 – Prove the fix. ... Step 5 – Don't test your fix. ... Step 6 – Closing the loop.

0

In html, When i use / for ending code it displays the same results as without / why?plz answer me also