0

My program is a quiz. The questions start easy at level one questions when the user gets a question correct they go to a level two question, if they get this question correct they go to a level 3 question and so on. This is working correctly (except the beginning of the quiz when the program asks the user two level one questions for some reason before going to level two) However, the main problem is that when the user gets a question wrong the program doesn't go down the the previous level question and ask them another questions, the cursor just blinks repeatedly in the top right hand corner. This also happens when they reach the top level questions and get one correct (the program must be expecting level 6 questions but there is no level six questions). The questions are coming from a text file and there is 10 questions for each level so 50 questions in total. The questions are stored as a struct of strings in the text file the struct contains: Question Number Question Level Question Question Answer

Here is where the problem in the code lies. Please help, Thanks.

        static void quiz(QuestionStruct[] _quiz)
    {
        bool asked = true;
        int score = 0;
        int AmountAsked = 0;

        string level = "1";
        string ans;
        int pos = 1;
        var pathToFile = @"..\..\..\Files\questions.txt";
        using (StreamReader sr = new StreamReader(pathToFile, true))
        {

            while (AmountAsked < 20 || score >= 50)
            {

                Console.Clear();
                string whatquestionnum =  questions[pos].QuestionNum = sr.ReadLine();
                string whatlevel =  questions[pos].Level = sr.ReadLine();
                //Level 1 questions
                while (level == "1" && questions[pos].Level == level)
                {
                    AmountAsked++;
                    string whatques = questions[pos].Question = sr.ReadLine();
                    Console.Write(whatques);
                    string selection = Console.ReadLine();
                    string decider = questions[pos].answer = sr.ReadLine();

                    if (selection == decider)
                    {
                        level = "2";
                        score = score + 1;

                    }
                    else
                    {
                        pos++;
                    }
                }

                //Level 2  questions
                while (level == "2" && questions[pos].Level == level)
                {
                    AmountAsked++;
                    questions[pos].Question = sr.ReadLine();
                    Console.Write(questions[pos].Question);
                    string selection2 = questions[pos].answer = sr.ReadLine();
                    ans = Console.ReadLine();
                    if (ans == selection2)
                    {
                        level = "3";
                        score = score + 2;

                    }
                    else
                    {
                        level = "1";
                        //questions[pos].Level == "1";
                    }
                }

                //Level 3 questions

                while (level == "3" && questions[pos].Level == level)
                {
                    AmountAsked++;
                    questions[pos].Question = sr.ReadLine();
                    Console.Write(questions[pos].Question);
                    string selection3 = questions[pos].answer = sr.ReadLine();
                    ans = Console.ReadLine();

                    if (ans == selection3)
                    {
                        level = "4";
                        score = score + 3;

                    }
                    else
                    {
                        level = "2";
                    }
                }

                //Level 4 

                while (level == "4" && questions[pos].Level == level)
                {
                    AmountAsked++;
                    questions[pos].Question = sr.ReadLine();
                    Console.Write(questions[pos].Question);
                    string selection4 = questions[pos].answer = sr.ReadLine();
                    ans = Console.ReadLine();

                    if (ans == selection4)
                    {
                        level = "5";
                        score = score + 4;

                    }
                    else
                    {
                        level = "3";
                    }
                }

                //Level 5

                while (level == "5" && questions[pos].Level == level)
                {
                    AmountAsked++;
                    questions[pos].Question = sr.ReadLine();
                    Console.Write(questions[pos].Question);
                    string selection5 = questions[pos].answer = sr.ReadLine();
                    ans = Console.ReadLine();

                    if (ans == selection5)
                    {
                        level = "5";
                        score = score + 5;

                    }
                    else
                    {
                        level = "4";
                    }
                }
            }
        }
    }
COYG
  • 1,538
  • 1
  • 16
  • 31

2 Answers2

2

This doesn't work because you only loop through the file once (exposing a fundamental problem with your design in the process!)

In pseudo-code you are doing this:

  1. Read and ask level 1 questions until it is time for level 2
  2. Read the rest of the level 1 questions
  3. Repeat for levels 2-5

If you go to level 6, there is nothing left to read so the loop terminates, and your program sits in the bigger loop (which I suspect never exits given your conditions). If you go down a level, there are no remaining questions for that level in the file, so the same thing happens.

Instead, you should read the file once into Question objects and choose from those when asking. You still need to define what happens for the non-existent level 6, but thats a requirement, not a bug. You also need to make the larger loop exitable more easily, but that again seems to be a requirments problem.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
0

Here's a quick pseudo program. Essentially you want an outer loop to be hit whenever you 'exit' a level. Then it'll determine where to send you next.

public void Tester()
{
    int level = 1;

    while (level < 6)
    {
        bool direction;

         switch (level)
         {
            case 1:
                direction = DoLevel1Stuff();
                level += direction ? 1 : -1;
                break;
            case 2:
                direction = DoLevel2Stuff();
                level += direction ? 1 : -1;
                break;

            //etc
          }
     }

     Console.WriteLine("You win!");
 }

 public bool DoLevel1Stuff()
 {
     bool didTheyPass;

     while (condition)
     {
         //stuff
     }

      return didTheyPass;
  }

   public bool DoLevel2Stuff()
   {

   }
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • This doesn't address the OPs problem with going down in level that stems from how he is reading the file. Otherwise this is reasonable advice. – BradleyDotNET Jul 23 '14 at 19:52
  • you're right, i overlooked the file reading. It should definitely be loaded up beforehand like you stated – Jonesopolis Jul 23 '14 at 19:54