0

So, I'm programming a function that asks a random question from the dictionary until all the questions have been asked and I ran into an issue. When the list of randomly generated numbers already contains the new generated number, the program calls the Ask function, as it should. But then, when it gets to the final return line, it calls the function Ask again, instead of returning the list of numbers.

        static List<int> Ask(Dictionary<string,string> Questions, List<int> random, int count)
    {
        bool contains = false;
        var rando = new Random();
        int num = Convert.ToInt32(rando.Next(0, count));

        if (random.Count >= count)
        {
            Console.WriteLine("No more questions! Quitting");
            System.Environment.Exit(1);
        }

        foreach (int number in random)
        {
            if (number == num)
            {
                contains = true;
            }
        }

        if (contains == true)
        {
            Ask(Questions, random, count);
        }

        random.Add(num);
        var randomEntry = Questions.ElementAt(num);
        String randomKey = randomEntry.Key;
        String randomValue = randomEntry.Value;
        Console.WriteLine(randomKey);

        if (Console.ReadLine() == randomValue)
        {
            Console.WriteLine("Correct!");
        }

        else
        {
            Console.WriteLine("Wrong!");
        }
        return random;
    }
6
  • 1
    What debugging have you done to confirm this? The behavior you describe assumes that your logic must be correct and the .NET runtime itself must be fundamentally broken. This is usually an invalid assumption. A return statement does, in fact, exit a method. If it didn't, everything would be broken. Commented Apr 5, 2015 at 21:43
  • Just simple debugging in VS 2013. Sorry if that doesn't answer your question, I'm still learning. It calls the Ask function from the if(contains == true) statement, if it helps. Commented Apr 5, 2015 at 21:48
  • Step through the executing code in the debugger. If contains is true then, according to your code, it should recursively call the Ask() function again. So why are you surprised that it's doing exactly that? Also, why aren't you doing anything with the result of calling that function? Commented Apr 5, 2015 at 21:49
  • I'm surprised because it checks the if statement AFTER getting to the return line. Commented Apr 5, 2015 at 21:50
  • No. It doesn't. Code executes in the order in which it's read. You must be misinterpreting the results and observations of your debugging. It's far, far more likely that you have made a mistake then that the entire .NET runtime is mis-designed and nobody has ever noticed. Commented Apr 5, 2015 at 21:52

2 Answers 2

1

I think the problem is in your recursive call to the function Ask. When you exit from the recursive call you are in the previous call still inside the Ask method and there is no way to avoid the remainder of the code following the Ask call.
I don't think yor really need a recursive method for this. Just check if the random number generated is already in your list of asked questions and repeat the random generation until you find a question not asked before....

Here the refactored code with variable names less confusing

List<int> Ask(Dictionary<string,string> Questions, int count)
{
    List<int> askedList = new List<int>();
    var rnd = new Random();
    int questionIndex = rnd.Next(0, count);

    while(askedList.Count < count)
    {
        if(!askedList.Any(number => number == questionIndex))
        {
           askedList.Add(questionIndex);
           var questionEntry = Questions.ElementAt(questionIndex);
           string questionText = questionEntry.Key;
           string questionAnswer = questionEntry.Value;
           Console.WriteLine(questionText);

           if (Console.ReadLine() == questionAnswer)
           {
               Console.WriteLine("Correct!");
           }
           else
           {
               Console.WriteLine("Wrong!");
           }
        }
        questionIndex = rnd.Next(0, count);
    }
    return askedList;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Works like a charm. Only I needed to exit the function after a question has been asked, but that's not a hard fix to do.
1

Your code is a little bit messy. But random var isn't updated after recursively calling the function. Try this:

random = Ask(Questions, random, count);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.