tags:

views:

293

answers:

4
  Thanks alot for the help
+1  A: 

It's because your character input reading loop is the last block in case 1. When that loop quits, you leave the case block and start again from the menu. Rearrange your code so that the part where you ask the user for the answer is inside the loop where you ask questions:

case 1:
    /* set directory for the file path*/
    using (StreamReader sr = new StreamReader("./quiz.txt"))
    {
        while (!sr.EndOfStream)
        {
            Console.Clear();
            int correct = 0;

            for (int i = 0; i < 5; i++)
            {
                String line = sr.ReadLine();
                if (i == 0)
                {
                    // Print question.
                    Console.WriteLine(line);
                }
                else
                {
                    if (line.Substring(0, 1) == "#")
                    {
                        // Correct answer.
                        correct = i;
                        line = line.Substring(1);
                    }
                    // Print possible answer.
                    Console.WriteLine("{0}: {1}", i, line);
                }
            }
            while (true)
            {
                Console.Write("Select Answer: ");
                ConsoleKeyInfo cki = Console.ReadKey();
                if (cki.KeyChar.ToString() == correct.ToString())
                {
                    Console.WriteLine(" - Correct!");
                    Console.WriteLine("Press any key for next question...");
                    Console.ReadKey();
                    break;

                }
                else
                {
                    Console.WriteLine(" - Try again!");
                    Console.Clear();
                }
            }
        }
    }
    break;

But I have a problem in general with this code - the method is too long. I think it would be much easier to follow the code if you separated the logic for parsing the questions, displaying the questions, and reading and checking the user's answers into separate functions.

The "try again" doesn't display because you clear the console immediately afterwards. Swap these two lines:

Console.WriteLine(" - Try again!");
Console.Clear();
Mark Byers
A: 

So add one more if statment after the user finish the question?

arg!! just test it again if you input the wrong answer the line of code which say try again not working aswell>.>

Tony
If you have a question to an answer, post it as a comment to that answer. Don't post it as a new answer.
Mark Byers
A: 

Thanks alot !!!!

So it was my coding method which make it hard for myselfs, i am new to programming in C# hopefully ican manage it .

Tony
A: 

I think you should eliminate this part altogether:

while (iChoice < 1 || iChoice > 4) /* Vaildation if the user input anything lower than 1 or higher than 4 */
{
    /*Message output the message to the user if they input invalid data*/
    Console.Write("You must enter 1-4, please try again: ");
    /* input data convert to integer*/
    iChoice = Convert.ToInt32(Console.ReadLine());
}

To make it more robust, use a simple regex to guarantee a numeric input

 System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"(?<optNo>\d{1,})", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);

Console.WriteLine("Hello Welcome to Litte Quiz World, We hope you will have fun here");
bool loopy = true;
while (loopy){
   /* Draw the menu */
   string sInput = Console.ReadLine();
   bool bValid = false;
   int Choice = 0;
   System.Text.RegularExpressions.Match m = re.Match(sInput);
   if (m.Success)
   {
      bValid = true;
      Choice = int.Parse(m.Groups["optNo"].Value);
   }
   else
        bValid = false;
   if (!bValid)
        Console.WriteLine("Invalid choice");
   else
   {
        switch (Choice)
        {
            /* Other cases here that corresponds to option on menu */
            case 4: loopy = false;
                    break;
        }
   }
}

Now, notice the loop will function and will continue... The reason I used a regex is to eliminate the while loop that checks for the value of choice in the original code, and the choice will naturally fall through the switch statement!

Hope this helps, Best regards, Tom.

tommieb75