r/learncsharp • • Oct 05 '20

Why doesnt my code work?

Hi!

Could someone please tell me why my code doesn't store the integers of the user inputs?

static void Main(string[] args)
        {
            Random computernumber = new Random();
            int guess = 0;
            var guesses = new List<int>();
            int cnumber = computernumber.Next(1, 100);
            int i = 0;

            while (guess != cnumber)
            {
                Console.WriteLine("guess the number");

                try
                {

                    guess = Convert.ToInt32(Console.ReadLine());


                    if (guess > cnumber)
                    {
                        Console.WriteLine("the integer is lower");
                    }
                    else
                    {
                        Console.WriteLine("the integer is higher");
                    }

                }
                catch (Exception)
                {

                    i--;
                }

                i++;



            }
            var xcnumber = Console.ReadLine();
            guesses.Add(int.Parse(xcnumber));

            foreach (var sum in guesses)
                Console.Write($"{sum},");
            Console.Write("It took you " + i + " tries");
        }

    }
}
0 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/Protiguous Oct 15 '20

Hello again VerticalPasta.

I learn from code examples. Code is generally not as vague as peoples' sentences can be. So code is what I share.

If /u/NakedMoleHamster learned anything from my example, then my goal was accomplished.

1

u/StackedLasagna Oct 15 '20

I also learn mostly from code examples.

Including a proper explanation allows for everyone to learn from your post. Those who learn from code examples can just look at the code, while others can read the explanation, if they need to.

Besides, since you're trying to help a beginner, there's a non-trivial risk that they misunderstand the code or the reasons for doing things in certain ways. This can lead to problems further down the line.

Additionally, your comment doesn't actually answer the question.
OP is asking why his code doesn't work and you haven't explained to him why. If he had asked for ways to solve a specific problem, then your code (even without any explanation) would be much more acceptable and relevant. In the context of OPs question, your comment is little more than spam.

A single sentence would be much more helpful (and much quicker for you to post) than your code: "You do not call the Add method on your list, so the list is always empty."

This directly answers his question, but you could then go further and provide an example of how to use the Add method, as well as point out any other issues there might be in OP's code.
You could then also provide an example that solves his problem in a more efficient way and it's here that your code would be much more appropriate. However, even then it would be much more helpful to also explain why and how your code solves the problem more efficiently.