0

[SOLVED] (Thank you) Removed - sololearn C# challenge

I'm attempting to complete this challenge and I'm close, but for some reason it seems to be outputting an additional number (sometimes two additional numbers) that i cant account for. the challenge reads like this: "The program you are given defines the evenNums stack and pushes 3 even numbers (6, 8 and 4) onto it. The 4th number is taken from the user and also pushed. But we need to check whether it is also an even number. Complete the program to perform that check process, and if the last pushed number N isn't even, remove it from stack and output "N: Removed" (see sample output). At the end, output all the elements of the stack, separated by spaces. Sample Input 1 Sample Output Checking the last number: 1 1: Removed 6 8 4 Pop() - Returns the element at the top of the stack and removes it. The output should end with a space." my code is here: https://code.sololearn.com/cA22A1a0A14A can anyone explain why im getting that extra number(s), and offer a solution for getting rid please? thank you.

21st Jun 2021, 5:54 AM
Andrew Hall
Andrew Hall - avatar
2 odpowiedzi
+ 2
I think you haven't noticed that you didn't started the foreach loop.Your code is like this: foreach(int i in evenNums) Console.Write(i + " "); Console.Write(evenNums.Count); here how can this foreach loop start? So give the brackets of the foreach loop and remove the evenNums.Count.Because you only need to print the value of i. https://code.sololearn.com/cA9A8a24a110
21st Jun 2021, 6:22 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
Ahhh! Thank you! This worked 👍 The code in the link has been adjusted to show code that works for the challenge for anyone who has issues with this in future.
21st Jun 2021, 7:33 AM
Andrew Hall
Andrew Hall - avatar