Skip to main content
2 of 7
edited tags
IEatBagels
  • 12.4k
  • 3
  • 47
  • 99

Quiz program that asks 10 questions, repeats incorrectly answered ones a second time, gives results at end

I'm currently attending ERAU (online), and in my Computing for Engineers class we are learning C#. An ongoing project throughout the class is to build a "quiz" program that asks 10 questions, in order, and then repeats missed questions (in order) on a second iteration. There are 2 types: T/F and multiple choice. 4 T/F and 6 mc; the mc questions can have 4 possible answers. The program must keep track of the number of each type of question that was answered correctly and display that at the end.

I'm just learning about loops, conditional statements, arrays and methods (last week), and here's the code I came up with (it compiles and runs, and does exactly what I want it to do, but my instructor said it is too long and not optimized):

 using System;

namespace CH5KennethMyers
{
    class Quiz
    {
        static void Main(string[] args)
        {   //start the quiz by telling the student the details of the structure of the quiz and the subject being tested
            System.Console.WriteLine("Kenneth Myers, ENGR115, Assignment: Checkpoint 2");
            System.Console.WriteLine("You are about to take a 10 question quiz on Binary Number Systems.");
            System.Console.WriteLine("Each question will be displayed with either a True/False or Multiple-choice answer key. Select the correct answer by pressing the corresponding key.");
            System.Console.WriteLine("If you get the question wrong, you will be notified of the wrong answer and the next question will be displayed.");
            System.Console.WriteLine("Once all questions have been displayed once, questions answered incorrectly will be displayed in the original order; you may attempt the question again.");
            System.Console.WriteLine("After all missed questions have been displayed a second time and answered, the results of your quiz will be displayed, by category of question and number answered correctly, and the total number answered correctly.");
            System.Threading.Thread.Sleep(5000);//adds a 5 second delay before the next operation to give the user time to read the instructions
            System.Console.Clear(); //clears the screen before starting the test
            //time to define the questions (using an array this time)
            var question = new string[] { "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros? Press T or F and enter to submit", "Question 2: Multiple choice – Add the following binary numbers: 1011+0011. Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 4: True or False – Binary numbers are base-10? Press T or F and enter to submit", "Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. Answers: 1) 1010, 2) 1001, 3) 1000, 4) 1000. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 7: True or False – Binary numbers are base-2? Press T or F and enter to submit", "Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? Answers: 1) 11, 2) 22, 3) 14, 4) 7. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? Press T or F and enter to submit" };
            //tried to make the previous line more human readable by putting each question on it's own line, but got a compile error; need to find out if there is a better way to do this
            //time to create the question counters to be used in the loop
            var questionCount = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };//creating array to replace original code
            //create a few question counters to track the number of correctly answered questions (by type) for use at the end to give feedback on number of correctly answered questions
            byte trueFalseCount = 0; byte multipleChoiceCount = 0; //sets the true/false counter and multiple choice counters to zero (default value), used byte since max value should not exceed 255 and won't be negative; these will be used later on in successive iterations of code
            //time to define the answers to the questions and assign to a constant we can compare to later
            var questionAnswer = new string[] { "t", "1", "4", "f", "2", "3", "t", "1", "2", "f" };//creating array to replace original code
            //time to create the input variables for each question to accept user input
            var questionInput = new string[10];
            //define the variable that will count the number of times the quiz questions have been displayed (test iteration)
            byte testIteration = 0;
            //start the quiz - display each question, in order starting at question 1, and accept the user input for that question, then display the user input and move to the next question
            do //start of the loop to display all questions in order, up to 2 times, depending on whether the question is answered correctly the first time
            {   //set the test iteration number
                testIteration++;
                System.Console.Clear();//clear the screen
                System.Threading.Thread.Sleep(3000);//add time delay
                System.Console.WriteLine("Test iteration " + testIteration);//tell user what iteration of the test it is
                System.Threading.Thread.Sleep(3000);//add time delay
                //question 1 (true/false)
                //incrementing question 1 counter by 1
                questionCount[0]++;
                if (questionCount[0] <= 2) //tests question 1 counter to see if equal to or less than 2, if false, should skip to next question. This logic (if loop) is repeated for each question
                {
                    System.Console.WriteLine(question[0]); //displays question 1
                    questionInput[0] = invalidTrueFalseEntry(); //accepts user input for answer to question 1 and stores in variable; later, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[0]); //displays the answer chosen by the user
                    if (questionInput[0] == questionAnswer[0])//tests for correct answer to question
                    {
                        //increments question 1 counter by 1
                        questionCount[0]++;
                        trueFalseCount++;//increments T/F counter by 1 (TF counter counts number of correctly answered TF questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 2 (multiple choice)
                questionCount[1]++;//increment question counter; do this for each question
                if (questionCount[1] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[1]); //displays question 2
                    questionInput[1] = invalidMultipleChoiceEntry(); //accepts user input for answer to question 2 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[1]); //displays the answer chosen by the user
                    if (questionInput[1] == questionAnswer[1])//tests for correct answer to question
                    {
                        //increments question 2 counter by 1
                        questionCount[1]++;
                        multipleChoiceCount++;//increments multiple choice counter by 1 (multiple choice counter counts number of correctly answered multiple choice questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 3 (multiple choice)
                questionCount[2]++;
                if (questionCount[2] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[2]); //displays question 3
                    questionInput[2] = invalidMultipleChoiceEntry(); //accepts user input for answer to question 3 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[2]); //displays the answer chosen by the user
                    if (questionInput[2] == questionAnswer[2])//tests for correct answer to question
                    {
                        //increments question 3 counter by 1
                        questionCount[2]++;
                        multipleChoiceCount++;//increments multiple choice counter by 1 (multiple choice counter counts number of correctly answered multiple choice questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 4 (true/false)
                questionCount[3]++;
                if (questionCount[3] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[3]); //displays question 4
                    questionInput[3] = invalidTrueFalseEntry(); //accepts user input for answer to question 4 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[3]); //displays the answer chosen by the user
                    if (questionInput[3] == questionAnswer[3])//tests for correct answer to question
                    {
                        //question4Count++;
                        //increments question 4 counter by 1
                        questionCount[3]++;
                        trueFalseCount++;//increments T/F counter by 1 (TF counter counts number of correctly answered TF questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 5 (multiple choice)
                questionCount[4]++;
                if (questionCount[4] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[4]); //displays question 5
                    questionInput[4] = invalidMultipleChoiceEntry(); //accepts user input for answer to question 5 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[4]); //displays the answer chosen by the user
                    if (questionInput[4] == questionAnswer[4])//tests for correct answer to question
                    {
                        //increments question 5 counter by 1
                        questionCount[4]++;
                        multipleChoiceCount++;//increments multiple choice counter by 1 (multiple choice counter counts number of correctly answered multiple choice questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 6 (multiple choice)
                questionCount[5]++;
                if (questionCount[5] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[5]); //displays question 6
                    questionInput[5] = invalidMultipleChoiceEntry(); //accepts user input for answer to question 6 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[5]); //displays the answer chosen by the user
                    if (questionInput[5] == questionAnswer[5])//tests for correct answer to question
                    {
                        //question6Count++;
                        //increments question 6 counter by 1
                        questionCount[5]++;
                        multipleChoiceCount++;//increments multiple choice counter by 1 (multiple choice counter counts number of correctly answered multiple choice questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 7 (true/false)
                questionCount[6]++;
                if (questionCount[6] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[6]); //displays question 7
                    questionInput[6] = invalidTrueFalseEntry(); //accepts user input for answer to question 7 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[6]); //displays the answer chosen by the user
                    if (questionInput[6] == questionAnswer[6])//tests for correct answer to question
                    {
                        //increments question 7 counter by 1
                        questionCount[6]++;
                        trueFalseCount++;//increments T/F counter by 1 (TF counter counts number of correctly answered TF questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 8 (multiple choice)
                questionCount[7]++;
                if (questionCount[7] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[7]); //displays question 8
                    questionInput[7] = invalidMultipleChoiceEntry(); //accepts user input for answer to question 8 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[7]); //displays the answer chosen by the user
                    if (questionInput[7] == questionAnswer[7])//tests for correct answer to question
                    {
                        //increments question 8 counter by 1
                        questionCount[7]++;
                        multipleChoiceCount++;//increments multiple choice counter by 1 (multiple choice counter counts number of correctly answered multiple choice questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 9 (multiple choice)
                questionCount[8]++;
                if (questionCount[8] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[8]); //displays question 9
                    questionInput[8] = invalidMultipleChoiceEntry(); //accepts user input for answer to question 9 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[8]); //displays the answer chosen by the user
                    if (questionInput[8] == questionAnswer[8])//tests for correct answer to question
                    {
                        //increments question 9 counter by 1
                        questionCount[8]++;
                        multipleChoiceCount++;//increments multiple choice counter by 1 (multiple choice counter counts number of correctly answered multiple choice questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                    System.Threading.Thread.Sleep(3000);
                }
                //question 10 (true/false)
                questionCount[9]++;
                if (questionCount[9] <= 2)
                {
                    System.Console.Clear();
                    System.Console.WriteLine(question[9]); //displays question 10
                    questionInput[9] = invalidTrueFalseEntry(); //accepts user input for answer to question 10 and stores in variable, passes input to method to test if valid input type
                    System.Console.WriteLine("Your answer was: " + questionInput[9]); //displays the answer chosen by the user
                    if (questionInput[9] == questionAnswer[9])//tests for correct answer to question
                    {
                        //increments question 10 counter by 1
                        questionCount[9]++;
                        trueFalseCount++;//increments T/F counter by 1 (TF counter counts number of correctly answered TF questions)
                        System.Console.WriteLine("Correct answer!");
                    }
                    else
                    {
                        System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                    }
                }
                if (testIteration < 2)//this will determine if this is the first iteration, and if so, tell the user that they can try missed questions again
                {
                    System.Console.WriteLine("Incorrectly answered questions will be displayed a second time, so try again!");
                }
                System.Threading.Thread.Sleep(3000);
            }
            while (questionCount[9] <= 2);
            System.Console.WriteLine("Test Complete!");//tells user the test is over
            System.Console.WriteLine("You answered " + trueFalseCount + " T/F questions correctly and " + multipleChoiceCount + " multiple choice questions correctly.");//tells user their performance
            System.Console.WriteLine("You missed " + (4 - trueFalseCount) + " T/F questions and " + (6 - multipleChoiceCount) + " multiple choice questions.");
            System.Threading.Thread.Sleep(5000);

        }

        static string invalidTrueFalseEntry()//this method will check true/false answers to ensure input was a "t" or an "f"
        {
            var answer = System.Console.ReadLine();
            if (answer != "t" && answer != "f")
            {
                System.Console.WriteLine("Invalid answer type (not t or f), try again.");
                return invalidTrueFalseEntry();
            }
            return answer;
        }
        static string invalidMultipleChoiceEntry()//this method will check multiple choice answers to ensure they are a number 1-4
        {
            var answer = System.Console.ReadLine();
            if (answer != "1" && answer != "2" && answer != "3" && answer != "4")
            {
                System.Console.WriteLine("Invalid answer type (not 1-4), try again.");
                return invalidMultipleChoiceEntry();
            }
            return answer;
        }
    }
}

It seems to me that there is alot of duplicate code, and I've been trying to figure out the best way to go about this, but I'm struggling to do so. I re-worked my code as such:

using System;

namespace CH5testing
{
    class Quiz
    {
        static void Main(string[] args)
        {   //start the quiz by telling the student the details of the structure of the quiz and the subject being tested
            System.Console.WriteLine("Kenneth Myers, ENGR115, Assignment: Checkpoint 4");
            System.Console.WriteLine("You are about to take a 10 question quiz on Binary Number Systems.");
            System.Console.WriteLine("Each question will be displayed with either a True/False or Multiple-choice answer key. Select the correct answer by pressing the corresponding key.");
            System.Console.WriteLine("If you get the question wrong, you will be notified of the wrong answer and the next question will be displayed.");
            System.Console.WriteLine("Once all questions have been displayed once, questions answered incorrectly will be displayed in the original order; you may attempt the question again.");
            System.Console.WriteLine("After all missed questions have been displayed a second time and answered, the results of your quiz will be displayed, by category of question and number answered correctly, and the total number answered correctly.");
            System.Threading.Thread.Sleep(5000);//adds a 5 second delay before the next operation to give the user time to read the instructions
            System.Console.Clear(); //clears the screen before starting the test
            //define the test iteration counter to track the number of test iterations
            var testIteration = 0;
            //define the questions (using an array this time)
            var questions = new string[] { "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros? Press T or F and enter to submit", "Question 2: Multiple choice – Add the following binary numbers: 1011+0011. Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 4: True or False – Binary numbers are base-10? Press T or F and enter to submit", "Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. Answers: 1) 1010, 2) 1001, 3) 1000, 4) 1000. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 7: True or False – Binary numbers are base-2? Press T or F and enter to submit", "Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? Answers: 1) 11, 2) 22, 3) 14, 4) 7. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110. Select the number (1,2,3 or 4) of the correct answer and enter to submit", "Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? Press T or F and enter to submit" };
            //tried to make the previous line more human readable by putting each question on it's own line, but got a compile error; need to find out if there is a better way to do this
            //create a few question counters to track the number of correctly answered questions (by type) for use at the end to give feedback on number of correctly answered questions
            byte trueFalseCount = 0; byte multipleChoiceCount = 0; //sets the true/false counter and multiple choice counters to zero (default value), used byte since max value should not exceed 255 and won't be negative; these will be used later on in successive iterations of code
            var questionCount = 0;
            //define the answers to the questions and assign to a constant we can compare to later
            var questionAnswer = new string[] { "t", "1", "4", "f", "2", "3", "t", "1", "2", "f" };//creating array to replace original code
            //create the input variables for each question to accept user input
            var questionInput = new string[10];
            //create question status array to track if the question was answered correctly the first time
            var questionStatus = new string[] { "n", "n", "n", "n", "n", "n", "n", "n", "n", "n" };
            //define the question types (tf or mc) in an array
            var questionType = new string[] { "tf", "mc", "mc", "tf", "mc", "mc", "tf", "mc", "mc", "tf" };
            //start the quiz - display each question, in order starting at question 1, and accept the user input for that question, then display the user input and move to the next question
            for (testIteration = 1; testIteration < 3; testIteration++) //start of the loop to display all questions in order, up to 2 times, depending on whether the question is answered correctly the first time
            {
                System.Console.Clear();//clear the screen
                System.Console.WriteLine("Test iteration " + testIteration);//tell user what iteration of the test it is
                //define the question counter to keep track of which question we are on 
                questionCount = 0;
                //foreach loop to handle each question and the answer input by the user, plus logic to test for invalid and correct/incorrect answers
                foreach (string question in questions)
                {
                        if (questionStatus[questionCount] == "n")
                        {
                            System.Console.WriteLine(questionCount);//used for debugging while running to see what this variable is during execution
                            System.Console.WriteLine(question); //displays the question, starting with question 1
                                if (questionType[questionCount] == "tf")
                                {
                                    questionInput[questionCount] = invalidTrueFalseEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
                                }
                                else if (questionType[questionCount] == "mc")
                                {
                                    questionInput[questionCount] = invalidMultipleChoiceEntry(); //accepts user input for answer to question and stores in variable; later, passes input to method to test if valid input type
                                }
                                else if (questionInput[questionCount] == questionAnswer[questionCount])//tests for correct answer to question
                                {
                                System.Console.WriteLine("Your answer was: " + questionInput[questionCount]); //displays the answer chosen by the user
                                System.Console.WriteLine("Correct answer!");
                                questionStatus[questionCount] = "y";
                                    questionCount++;
                                    System.Console.Clear();//clear the screen
                                        if (questionType[questionCount] == "tf")
                                        {
                                            trueFalseCount++;//increments T/F counter by 1 (TF counter counts number of correctly answered TF questions)
                                        }
                                        else if (questionType[questionCount] == "mc")
                                        {
                                            multipleChoiceCount++;//increments multiple choice counter by 1 (multiple choice counter counts number of correctly answered multiple choice questions)
                                        }
                                }
                                else if (questionInput[questionCount] != questionAnswer[questionCount])
                                {
                                    System.Console.WriteLine("Sorry, wrong answer. :-(");//or if the answer was wrong, display this!
                                    System.Console.Clear();//clear the screen
                                }
                        }
                        System.Threading.Thread.Sleep(3000);
                        System.Console.Clear();//clear the screen
                }
                string invalidMultipleChoiceEntry()//this method will check multiple choice answers to ensure they are a number 1-4
                {
                    var answer = System.Console.ReadLine();
                    if (answer != "1" && answer != "2" && answer != "3" && answer != "4")
                    {
                        System.Console.WriteLine("Invalid answer type (not 1-4), try again.");
                        return invalidMultipleChoiceEntry();
                    }
                    return answer;
                }
                string invalidTrueFalseEntry()//this method will check true/false answers to ensure input was a "t" or an "f"
                {
                    var answer = System.Console.ReadLine();
                    if (answer != "t" && answer != "f")
                    {
                        System.Console.WriteLine("Invalid answer type (not t or f), try again.");
                        return invalidTrueFalseEntry();
                    }
                    return answer;
                }
                System.Console.WriteLine("Test Complete!");//tells user the test is over
                System.Console.WriteLine("You answered " + trueFalseCount + " T/F questions correctly and " + multipleChoiceCount + " multiple choice questions correctly.");//tells user their performance
                System.Console.WriteLine("You missed " + (4 - trueFalseCount) + " T/F questions and " + (6 - multipleChoiceCount) + " multiple choice questions.");
                System.Threading.Thread.Sleep(5000);
            }
            
        }
    }
}

This is much shorter, but now I'm not getting the same behavior or even what I'm expecting to get sometimes. I'm attempting to get the same behavior as before, but with optimized code. Also, this week we are learning about OOP, but I'm still stuck on this and I feel like I can't move on to OOP until I figured this out. I spent quite a few hours developing the second code last night (until 2 am), with numerous errors that I had to work through to get to this point, and now I'm frustrated.

FWIW, I'm using Visual Studio 17 on Win10. Any suggestions, tips, pointers, etc. would be greatly appreciated. Thanks in advance!

MX372
  • 41
  • 3