Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having an issue displaying the proper output for the array .... This is my main class using System;

i need it to store the array[index] on the questions that are incorrect and then output each question.... Please let me know if i need to edit this question or be more indepth in my information
C#
private const string STR_RESOURCE_PATH = @"Resources\\";

    static void Main(string[] args)
    {
        MultipleChoiceQuestion[] questionList = new MultipleChoiceQuestion[30];
        int option;
        int correct = 0;
        int incorrect = 0;
        int index = 0;

        do
        {
            DisplayMenuOptions();
            option = Valid.GetValidInteger("\nOption?", 0, 8);

            switch (option)
            {
                case 1:
                    CreateNewQuestion(ref questionList);
                    break;
                case 2:
                    DisplayAllQuestions(ref questionList);
                    break;
                case 3:
                    EditQuestions(ref questionList);
                    break;
                case 4:
                    DeleteQuestion(ref questionList);
                    break;
                case 5:
                    ImportQuiz(ref questionList);
                    break;
                case 6:
                    ExportQuiz(ref questionList);
                    break;
                case 7:
                    TakeQuiz(ref questionList, ref correct, ref incorrect);
                    break;
                case 8:
                    MarkQuiz(ref questionList, ref correct, ref incorrect);
                    break;
            }

        } while (option != 0);
        Console.ReadLine();
    }
    private static void DisplayMenuOptions()
    {
        Console.WriteLine("Multiple Choice Exam");
        Console.WriteLine("----------------------");
        Console.WriteLine("Please enter: ");
        Console.WriteLine("   1 - to create a new question");
        Console.WriteLine("   2 - to display all questions");
        Console.WriteLine("   3 - to edit question");
        Console.WriteLine("   4 - to delete question");
        Console.WriteLine("   5 - to import questions for a file");
        Console.WriteLine("   6 - to export questions to a file");
        Console.WriteLine("   7 - to start exam");
        Console.WriteLine("   8 - to mark exam");
        Console.WriteLine("   0 - to exit program");
    }
    public static int GetNextArrayIndex(ref MultipleChoiceQuestion[] questionList)
    {
        int index = -1;


        for (int i = 0; i < questionList.Length; i++)
        {
            if (questionList[i] == null)
            {
                index = i;
                break;
            }
        }

        return index;
    }

    private static void CreateNewQuestion(ref MultipleChoiceQuestion[] questionList)
    {            ;
        string question;
        string choice1;
        string choice2;
        string choice3;
        string choice4;
        char correctChoice;

        int index = GetNextArrayIndex(ref questionList);

        Console.WriteLine("Multiple Choice Exam - New Question");
        Console.WriteLine("--------------------------------------");
        Console.WriteLine("There are currently {0} questions in the exam.", index);

        if (index != -1)
        {
                Console.Write("Enter question: ");
                question = Console.ReadLine();
                Console.Write("Enter Choice 1 for the question: ");
                choice1 = Console.ReadLine();
                Console.Write("Enter Choice 2 for the question: ");
                choice2 = Console.ReadLine();
                Console.Write("Enter Choice 3 for the question: ");
                choice3 = Console.ReadLine();
                Console.Write("Enter Choice 4 for the question: ");
                choice4 = Console.ReadLine();

                do
                {
                    Console.Write("Enter the correct choice: (A, B, C or D): ");
                    correctChoice = char.Parse(Console.ReadLine().ToUpper());
                    if (correctChoice == 'A' || correctChoice == 'B' || correctChoice == 'C' || correctChoice == 'D')
                    {
                        questionList[index] = new MultipleChoiceQuestion(question, choice1, choice2, choice3, choice4, correctChoice);
                        Console.WriteLine("Successfully added question {0} to the exam\n", index);
                    }
                    else
                    {
                        Console.WriteLine("Sorry invalid input.");
                    }

                } while (correctChoice != 'A' && correctChoice != 'B' && correctChoice != 'C' && correctChoice != 'D');


            Console.WriteLine("Press any key to continue");
        }
    }

    public static char TakeQuiz(ref MultipleChoiceQuestion[] questionList, ref int correct, ref int incorrect)
    {
        char answers = 'w'; //so the computer will not allow use to input a w
        int index = GetNextArrayIndex(ref questionList);

        for (int i = 0; i < questionList.Length; i++)
        {
            if (questionList[i] != null)
            {
                questionList[i].DisplayQuestion();
                Console.Write("\nYour anwser (A,B,C,D): ");
                answers = char.Parse(Console.ReadLine().ToUpper());//Stores users answers to that question

                if (answers == 'A' || answers == 'B' || answers == 'C' || answers == 'D')//If these values are entered than will run next if else 
                {
                    if (answers == questionList[i].correctChoice)//If the user enters a value that matches its correct answers it will store in correct
                    {
                        correct++;

                    }
                    else
                    {
                        index = i;
                        questionList[incorrect].question += index;
                        incorrect++;

                        if (questionList[index] == null)
                        {
                            for (int p = index; p < questionList.Length; p++)
                            {
                                if (i + 1 < questionList.Length)
                                {
                                    questionList[i] = questionList[i + 1];
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("ERROR: Invalid input");
                    i = i - 1;//Gose back in array to give the user the ability to re-input there valid answers

                }

            }
        }
        return answers;
    }

    public static void MarkQuiz(ref MultipleChoiceQuestion[] questionList, ref int correct, ref int incorrect)
    {
        int index = ArrayIndex(ref questionList);
        double rightAnwser;
        double percent = 0.00;

        Console.WriteLine("Multiple Choice Exam - Mark Exam");
        Console.WriteLine("---------------------------------------");

        rightAnwser = correct;
        percent = (rightAnwser / (rightAnwser + incorrect));

        if (percent >= .83)
        {
            Console.WriteLine("You got {0:P2}. You got {1} questions correct out of {2}", percent, correct, correct + incorrect);
            Console.WriteLine("You passed the exam.");
            Console.WriteLine("Press any key to continue.");
        }
        else
        {
            Console.WriteLine("You got {0:P2}. You got {1} questions correct out of {2}", percent, correct, correct + incorrect);
            Console.WriteLine("You failed the exam.");
            Console.WriteLine("The following questions were anwsered incorrectly:  ");

            for (int n = 0; n < incorrect; n++)//Loops question numbers
            {
                if (index > 0)
                {
                    Console.Write("{0}, ",n + index);
                }
            }
        }
        Console.Write("\b\b ");
        Console.ReadLine();
    }
}

}


What I have tried:

i am having an issue displaying the proper output for the array .... This is my main class using System;

i need it to store the array[index] on the questions that are incorrect and then output each question.... Please let me know if i need to edit this question or be more indepth in my information
Posted
Updated 13-Dec-16 0:43am
Comments
dan!sh 13-Dec-16 1:34am    
From what I understood, you are having issues with finding questions with incorrect answers. Is that correct?
Member 12815488 13-Dec-16 2:09am    
array.indexof()

seems maybe it would be easier to build a list of the incorrect questions and just output the list?

1 solution

There are a few things you need to change to start with.
Firstly, stop using ref everywhere - you don't need it for the questions collection, and it's misleading for your integers.
Secondly, stop re-inventing the wheel and use the "built in" features of the framework.

So, delete the GetNextArrayIndex method completely, and change the questionList to a List<MultipleChoiceQuestion> instead.

You can then use Add to add a question to the end, without having to think about what it's index might be:
C#
private static MultipleChoiceQuestion CreateNewQuestion()
   {
   ... Read your question details here ...
   return new MultipleChoiceQuestion(question, choice1, choice2, choice3, choice4, correctChoice);
   }
Then when you use it, you add it to your collection:
C#
case 1:
    questionList.Add(CreateNewQuestion());
    break;
That way, the method has only one thing to do - create a question - without having to know or think about what the rest of the code is going to do with it. This is called separation of concerns and it makes your code cleaner, easier to read and a lot more reliable / maintainable.
Do the same thing with out other methods, and then change the way TakeQuiz
works:
C#
public static void TakeQuiz(List<MultipleChoiceQuestion> questions, List<MultipleChoiceQuestion> correct, List<MultipleChoiceQuestion> incorrect)
   {
   ...
   }
Inside the method, you can just use foreach to get each question in turn (without worrying about it's index) and if he gets it right it goes in the correct collection, otherwise incorrect.
The calling code can then tell the number of correct and incorrect answers by using the collections Count property, and can iterate through the wrong ones with another foreach on the incorrect collection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900