Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to build a multiple choice quiz for class in C#. I've declared the variables, this week's project checkpoint is to implement loops into my code. I've read my text book and while it explains loops, it doesn't provide me with an idea to implement it in. I fell i need to utilize the "Foreach Loop", is this correct? Can anyone give me a visual? this is what I have so far. I have no idea how to write or structure a line of code.
C#
using System;

namespace Checkpoint_2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Quiz");
            //Variables
            bool answerCorrect = false; // will be used to evaluate if answer is correct or not
            string[] questions = { "2+2=", "4x5=", "8+2=", "2x100=", "3+3=", "2+100", "2-2=", "1+2=", "3x3=15", "45-5+8=48" }; // will be questions used on test. 8 Multiple choice and 2 true/false
            string[] answers = { "a.4 b.2 c.6 d. 10", "a.40 b.20 c.16 d. 11", "a.6 b.12 c.16 d. 10", "a.104 b.200 c.65 d. 102", "a.4 b.2 c.6 d. 10", "a.14 b.200 c.60 d. 102", "a.4 b.2 c.0 d. 10", "a.3 b.2 c.6 d. 102", "true false", " true false" }; // these will stand as answer bank for all questions
            string[] correctanswer = { "a", "b", "d", "b", "c", "d", "c", "a", "false", "true" }; //these are the correct answers to all the question. any other selections will be countd as incorrect
            int scoreCard = 0; // everyone starts with 0 correct out of ten. once quiz is completed score will be listed as 1-10. 
            int[] questionsIncorrect = { };


            //Welcome To Program
            Console.WriteLine("Ryan Hinds, ENGR101- Quiz");
            Console.WriteLine();


            Console.WriteLine(questions[0]);



            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }
    }
}


What I have tried:

I've dug into my text book and some other online resources. I've also tried plugging in my code, but receive errors
Posted
Updated 5-Dec-17 20:28pm
Comments
Patrice T 6-Dec-17 0:02am    
"but receive error"
And you plan to tell us which errors and positions?

Loops are pretty simple. What you are looking for is a for loop - because you need an index into several arrays in order to access the question, it's answers, and the correct result. This explains them: for (C# Reference) | Microsoft Docs[^]
So you can write a simple loop to process teh questions:
C#
for (int questionNumber = 0; questionNumber < numberOfQuestions; questionNumber++)
   {
   // Body of your loop: ask a question, get an answer, check it here.
   // (Use questionNumber to access each array)
   string question = questions[questionNumber];
   ...
   }
Read the link, and give it a try.
 
Share this answer
 
Comments
Member 13560407 6-Dec-17 19:56pm    
Thanks for the the lead OG. I tried to implement what you have here and off the link, but it will not pull from my array.

This is what I have.

using System;

namespace Checkpoint_2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Quiz");
//Variables
bool answerCorrect = false; // will be used to evaluate if answer is correct or not
string[] questions = { "2+2=", "4x5=", "8+2=", "2x100=", "3+3=", "2+100", "2-2=", "1+2=", "3x3=15", "45-5+8=48" }; // will be questions used on test. 8 Multiple choice and 2 true/false
string[] answers = { "a.4 b.2 c.6 d. 10", "a.40 b.20 c.16 d. 11", "a.6 b.12 c.16 d. 10", "a.104 b.200 c.65 d. 102", "a.4 b.2 c.6 d. 10", "a.14 b.200 c.60 d. 102", "a.4 b.2 c.0 d. 10", "a.3 b.2 c.6 d. 102", "true false", " true false" }; // these will stand as answer bank for all questions
string[] correctanswer = { "a", "b", "d", "b", "c", "d", "c", "a", "false", "true" }; //these are the correct answers to all the question. any other selections will be countd as incorrect
int scoreCard = 0; // everyone starts with 0 correct out of ten. once quiz is completed score will be listed as 1-10.
int[] questionsIncorrect = { };


//Welcome To Program
Console.WriteLine("Ryan Hinds, ENGR101- Quiz");
Console.WriteLine();


for (int i = 0; i < 9; i++);
{ string question = questions [0];
string answer = answers[0];



}




Console.ReadKey();

}
}
}
OriginalGriff 7-Dec-17 3:45am    
Is there any particular reason why you want to access the first question every time you go around the loop?
Hint: myArray[indexNumber] instead of myArray[0] might be worth thinking about...
Let imagine the test is conducted in such a way that
0. Test starts with all parameters initialized, e.g. i = 0, scoreCard = 0

1. a teacher reads out one question (questions[i]) and the answer options (answers[i]) (at iteration i)

2. a student replies with an answer option, say "a" (student_answer = "a" )

3. the teacher checks the student's answer against the correct answer: ( student_answer == correctanswer[i])
   
   3.1 if the answer is correct, add one to the score ( scoreCard++ )

4. Repeat 1 to 3 till all questions are attempted.
 
Share this answer
 
v2

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