Hi

I am creating a quiz in C# and i want to know how to flag a question that the user is unsure about. At the end of the quiz i would like them to review flagged questions. How can i do this?

So far i have created the array of questions and answers.

Thanks for any help

You also need to create an array of flags (integers or characters), one for each question. Initialize the array to 0, then change the element to 1 for each question answered incorrectly. This means array[0] = answer to question[0], array[1] = answer to question[1], etc.

Have you every though about creating a custom object? Something along the lines of

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyProgram
{
    public class QuizProblems
    {
        public QuizProblems (string question)
        {
            this.question = question;
        }

        public string question
        {
            get;
            set;
        }

        public string answer
        {
            get;
            set;
        }

        public bool flagged 
        {
            get;
            set;
        }
    }
}

This is assuming you store the answers after the user enters them. Otherwise you could pass in the answers just like the questions.

Then at the end, you can simply use a for loop, and look to when the boolean is set to true.

I assume you know how to assign this and access values? If not, let us know

Comments
Not a bad idea.

Thanks for your reply, I have created array of questions. I wouldnt know how to assign and access the value mentioned. if it helps i have pasted my sourcecode for my program which might more sense how i have done things

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        int i = 0;
        int score = 0;

        //Array of questions
        const int NumQuestions = 10; //variable stroing number of questions
        string[] Question = new string[NumQuestions] // variable passed into the array
        { 
            " What component inside a computer is non volatile memory?", 
            "What component contains the ALU? ", 
            "What component can be static or dynamic? ", 
            "What component has the northbridge and southbridge?", 
            "Starting from the lowest put the computer file sizes in order?", 
            "Which of the following is a computer operating system?", 
            "What is the job of the kernel in a operating system?", 
            "What should be used to backup mutiple PCs?", 
            "Paging is a term used with what?" ,
            "Which hard drive format, restricts single file sizes to 4GB or less?"
        };

        //Array if answers
        string[] option1 = new string[] { "ROM", "HDD", "SSD","Graphics Card", "10KB  10GBs  10MBs  10TBs", "Xerox", "Manages  requests from input/output devices","Server", "CMOS", "FAT 16" };
        string[] option2 = new string[] { "RAM", "SSD", "RAM","Case", "10KB 10MBs 10GBs 10TBs", "Borax", "Used to run office packages","Desktop", "BIOS", "NTFS" };
        string[] option3 = new string[] { "SDRAM", "CPU", "USB","Hard drive", "10KB 10GBs 10TBs 10MBs", "Linux", "Allows multi tasking of multiple user windows","Laptop", "IP Addressing", "FAT 32" };
        string[] option4 = new string[] { "DRAM", "PSU", "PCI", "Motherboard", "10MBs  10GBs 10KBs 10TBs", "Xenon", "Allows connection to networks through wireless NIC","Tablet", "Virtual Memory","OSx Journaled" };

        //Array of correct anwesers

        int[] correct_answers = new int[] { 1, 3, 2, 4, 2, 3, 1, 1, 4 , 3};

        //Array of user answers
        int[] User_Answer = new int[10];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)// Events at run time (Before user interaction)
        {
            Previous.Visible = false; //Previous Button Not displayed
            RadioAnswer1.Visible = false;
            RadioAnswer2.Visible = false;
            RadioAnswer3.Visible = false;
            RadioAnswer4.Visible = false;
            Finish.Visible = false;
            Review.Visible = false;
            Previous.Enabled = false;
            A1.Visible = false;
            A2.Visible = false;
            A3.Visible = false;
            A4.Visible = false;

        }

        private void MainImage_Click(object sender, EventArgs e)
        {

        }

        private void Exit_Click(object sender, EventArgs e)//Event handler for "Exit button"
        {
            DialogResult dlgresult;
            dlgresult = MessageBox.Show(
                "Are you sure you want Exit the program",
                "Quit Program?",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1);

            if (dlgresult == DialogResult.Yes)//
            {
                Application.Exit();
            }

        }

        private void Instructions_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult;
            dlgResult = MessageBox.Show
                (

                "You will be asked 10 multiple choice questions of computer systems. You can only select ONE anwser from the 4 options. After the quiz you will be given your total score, and have the option to review incorrect questions",
                "Computer Systems Quiz Instructions",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button2
                );
        }

        private void StartQuiz_Click(object sender, EventArgs e)//"Event handler for Start Quiz button"
        {
           MainImage.Visible = false; //Hide main image
            Previous.Visible = true; //Show Previous Button

            //Change name of start quiz button and create new event handler for button
            StartQuiz.Text = "Next >>" ; //changes name of start quiz button to restart quiz
            StartQuiz.Click -= this.StartQuiz_Click; //unsubcribed orginal event handler of "start quiz"
            StartQuiz.Click += this.Next_Click; //create new event handler renamed button "restart" 

            //change name of instructions button and create new event handler for button
            Instructions.Text = "Flag";
            Instructions.Click -= this.Instructions_Click;
            Instructions.Click += this.Flag_Click;

        }

        private void Flag_Click(Object sender, EventArgs e)// Event handler for flag button
        {
            //flag instructions to go here
        }

        private void Line1_Click(object sender, EventArgs e)
        {

        }

        private void Line3_Click(object sender, EventArgs e)
        {

        }

        private void Next_Click(object sender, EventArgs e)
        {


            Previous.Enabled = true;//Enables the previous button
            RadioAnswer1.Visible = true;
            RadioAnswer2.Visible = true; //Quiz answers now visible to user
            RadioAnswer3.Visible = true;
            RadioAnswer4.Visible = true;
            A1.Visible = true; //answer numbers now visible to the user
            A2.Visible = true;
            A3.Visible = true;
            A4.Visible = true;
           // RadioAnswer1.Checked = false;
           // RadioAnswer2.Checked = false;
           // RadioAnswer3.Checked = false;
           // RadioAnswer4.Checked = false;

                QuestionLabel.Text = Question[i]; //print first question of array
                RadioAnswer1.Text = option1[i]; //print first option of array
                RadioAnswer2.Text = option2[i]; //print second option of array
                RadioAnswer3.Text = option3[i]; //print thrid option of array
                RadioAnswer4.Text = option4[i]; //print fourth option of array

                    if ((RadioAnswer1.Checked) || (RadioAnswer2.Checked) || (RadioAnswer3.Checked) || (RadioAnswer4.Checked))
                    {
                        if (RadioAnswer1.Checked)
                        {
                            User_Answer[i] = 1;
                        }
                        else if (RadioAnswer2.Checked)
                        {
                            User_Answer[i] = 2;
                        }
                        else if (RadioAnswer3.Checked)
                        {
                            User_Answer[i] = 3;
                        }
                        else if (RadioAnswer4.Checked)
                        {
                            User_Answer[i] = 4;
                        }


                    else
                    {
                        MessageBox.Show("You have not not answered the question. \n Please answer the question before proceeding");
                    }
                }

            i++;

                if (i==NumQuestions)
            {

                Finish.Visible = true;
                StartQuiz.Enabled = false;

            }
        }  

        private void Previous_Click(object sender, EventArgs e)
        {

        }

        private void MainImage_Click_1(object sender, EventArgs e)
        {

        }

        private void QuestionLabel_Click(object sender, EventArgs e)
        {

        }

        private void Finish_Click(object sender, EventArgs e)
        {

            Review.Visible = true;
            Previous.Enabled = false;
            Instructions.Enabled = false;

            for (int i = 0; i < 9; i++)
            {
                if (User_Answer[i] == correct_answers[i])
                score = score + 1;
            }
            if (score <= 30)
            {
                MessageBox.Show("Very poor. \nYou got " + score + "\nClick review to view your Incorrect Answers");//Prints User score
            }

            else if ((score > 30) && (score <= 50))
            {
                MessageBox.Show("Could be better, You got " + score + "\nClick review to view your Incorrect Answers");//Prints User score
            }
            else if ((score >50) && (score<=60))
            {
                MessageBox.Show("Not bad. Your score was " + score +"\nClick review to view your Incorrect Answers");//Prints User score
            }
            else if ((score >70) && (score<=90))
            {
                MessageBox.Show("Well done your score was " + score + "\nClick review to view your Incorrect Answers");//Prints User score
            }
            else if (score == 100)
            {
                MessageBox.Show("Fantastic you got every question right");//Prints User score
            }

        }

        private void Review_Click(object sender, EventArgs e)
        {

        }

    }
}

Thanks for the help so far, and for any more you can give

Michael

First of all, Ancient Dragon's way is not wrong in anyway, in fact he probably knows 20 times more then me. The only reason I suggested doing it this way, was because I personally don't like using multiple arrays, trying to sync up everything and having multiple names. It comes from a personal opinion.

Alright so I went and took your code, and changed it over to support the idea I gave you. After pasting it, I'll go over each part I did to explain what's going on. (Also here's the code in Pastebin, since DaniWeb doesn't always copy over all the formatting http://pastebin.com/T08ppS8V)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
//=============================================================================================
    public class QuizProblems
    {
        public QuizProblems (string question, string [] answers, int correctAnswer)
        {
            this.question = question;
            this.answers = answers; //watch as there will be inheritance here
            this.correctAnswer = correctAnswer;
            this.selectedAnswer = -1;
            this.flagged = false;
        }
        public string question
        {
            get;
            set;
        }
        public string [] answers
        {
            get;
            set;
        }
        public int correctAnswer //stores what the correct answer is
        {
            get;
            set;
        }
        public int selectedAnswer //stores which answer in the array they selected
        {
            get;
            set;
        }
        public bool flagged
        {
            get;
            set;
        }
    }
//=============================================================================================
    public partial class Form1 : Form
    {
        int i = 0;
        int score = 0;

        //Array of questions
        const int NumQuestions = 10; //variable stroing number of questions

        QuizProblems [] Question = new QuizProblems[NumQuestions]; //creates ours new 

        public Form1()
        {
            InitializeComponent();
            SetupQuestions();
        }

        private void Form1_Load(object sender, EventArgs e)// Events at run time (Before user interaction)
        {
            Previous.Visible = false; //Previous Button Not displayed
            RadioAnswer1.Visible = false;
            RadioAnswer2.Visible = false;
            RadioAnswer3.Visible = false;
            RadioAnswer4.Visible = false;
            Finish.Visible = false;
            Review.Visible = false;
            Previous.Enabled = false;
            A1.Visible = false;
            A2.Visible = false;
            A3.Visible = false;
            A4.Visible = false;
        }

        private void SetupQuestions() //used to setup the Question object
        {
            Question [0] = new QuizProblems("What component inside a computer is non volatile memory?", new string [] {"ROM", "RAM", "SDRAM", "DRAM"}, 1);
            Question [1] = new QuizProblems("What component contains the ALU?", new string [] {"HDD", "SDD", "CPU", "PSU"}, 3);
            Question [2] = new QuizProblems("What component can be static or dynamic?", new string [] {"SDD", "RAM", "USB", "PCI"}, 2);
            Question [3] = new QuizProblems("What component has the northbridge and southbridge?", new string [] {"Graphics Card", "Case", "Hard drive", "Motherboard"}, 4);
            Question [4] = new QuizProblems("Starting from the lowest put the computer file sizes in order?", new string [] {"10KB 10GBs 10MBs 10TBs", "10KB 10MBs 10GBs 10TBs", "10KB 10GBs 10TBs 10MBs", "10MBs 10GBs 10KBs 10TBs"}, 2);
            Question [5] = new QuizProblems("Which of the following is a computer operating system?", new string [] {"Xerox", "Borax", "Linux", "Xenon"}, 3);
            Question [6] = new QuizProblems("What is the job of the kernel in a operating system?", new string [] {"Manages requests from input/output devices", "Used to run office packages", "Allows multi tasking of multiple user windows", "Allows connection to networks through wireless NIC"}, 1);
            Question [7] = new QuizProblems("What should be used to backup mutiple PCs?", new string [] {"Server", "Desktop", "Laptop", "Tablet"}, 1);
            Question [8] = new QuizProblems("Paging is a term used with what?", new string [] {"CMOS", "BIOS", "IP Addressing", "Virtual Memory"}, 4);
            Question [9] = new QuizProblems("Which hard drive format, restricts single file sizes to 4GB or less?", new string [] {"FAT 16", "NFAS", "FAT 32", "OSx Journaled"}, 3);
        }

        private void MainImage_Click(object sender, EventArgs e)
        {
        }

        private void Exit_Click(object sender, EventArgs e)//Event handler for "Exit button"
        {
            DialogResult dlgresult;
            dlgresult = MessageBox.Show("Are you sure you want Exit the program", "Quit Program?", MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);
            if (dlgresult == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
        private void Instructions_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult;
            dlgResult = MessageBox.Show
            (
            "You will be asked 10 multiple choice questions of computer systems. You can only select ONE anwser from the 4 options. After the quiz you will be given your total score, and have the option to review incorrect questions",
            "Computer Systems Quiz Instructions",
            MessageBoxButtons.OK,
            MessageBoxIcon.Information,
            MessageBoxDefaultButton.Button2
            );
        }   
        private void StartQuiz_Click(object sender, EventArgs e)//"Event handler for Start Quiz button"
        {
            MainImage.Visible = false; //Hide main image
            Previous.Visible = true; //Show Previous Button

            //Change name of start quiz button and create new event handler for button
            StartQuiz.Text = "Next >>" ; //changes name of start quiz button to restart quiz
            StartQuiz.Click -= this.StartQuiz_Click; //unsubcribed orginal event handler of "start quiz"
            StartQuiz.Click += this.Next_Click; //create new event handler renamed button "restart"

            //change name of instructions button and create new event handler for button
            Instructions.Text = "Flag";
            Instructions.Click -= this.Instructions_Click;
            Instructions.Click += this.Flag_Click;
        }

        private void Flag_Click(Object sender, EventArgs e)// Event handler for flag button
        {
            //flag instructions to go here
        }

        private void Line1_Click(object sender, EventArgs e)
        {
        }

        private void Line3_Click(object sender, EventArgs e)
        {
        }

        private void Next_Click(object sender, EventArgs e)
        {
            Previous.Enabled = true;//Enables the previous button

            RadioAnswer1.Visible = true;
            RadioAnswer2.Visible = true; //Quiz answers now visible to user
            RadioAnswer3.Visible = true;
            RadioAnswer4.Visible = true;

            A1.Visible = true; //answer numbers now visible to the user
            A2.Visible = true;
            A3.Visible = true;
            A4.Visible = true;

            // RadioAnswer1.Checked = false;
            // RadioAnswer2.Checked = false;
            // RadioAnswer3.Checked = false;
            // RadioAnswer4.Checked = false;

            QuestionLabel.Text = Question[i].question; //print the question in that array index

            RadioAnswer1.Text = Question[i].answers[0]; //prints the first option for an answer
            RadioAnswer2.Text = Question[i].answers[1]; //prints the second option for an answer
            RadioAnswer3.Text = Question[i].answers[2]; //prints the third option for an answer
            RadioAnswer4.Text = Question[i].answers[3]; //prints the fourth option for an answer

            if ((RadioAnswer1.Checked) || (RadioAnswer2.Checked) || (RadioAnswer3.Checked) || (RadioAnswer4.Checked))
            {
                if (RadioAnswer1.Checked)
                {
                    Question[i].selectedAnswer = 1;
                }
                else if (RadioAnswer2.Checked)
                {
                    Question[i].selectedAnswer = 2;
                }
                else if (RadioAnswer3.Checked)
                {
                    Question[i].selectedAnswer = 3;
                }
                else if (RadioAnswer4.Checked)
                {
                    Question[i].selectedAnswer = 4;
                }
                else
                {
                    MessageBox.Show("You have not not answered the question. \n Please answer the question before proceeding");
                }
            }

            i++;

            if (i == NumQuestions)
            {
                Finish.Visible = true;
                StartQuiz.Enabled = false;
            }
        }

        private void Previous_Click(object sender, EventArgs e)
        {
        }

        private void MainImage_Click_1(object sender, EventArgs e)
        {
        }

        private void QuestionLabel_Click(object sender, EventArgs e)
        {
        }

        private void Finish_Click(object sender, EventArgs e)
        {
            Review.Visible = true;
            Previous.Enabled = false;
            Instructions.Enabled = false;

            for (int i = 0; i < 9; i++)
            {       
                if (Question[i].selectedAnswer == Question[i].correctAnswer) //checks if the answer is correct
                {
                    score = score + 1;
                }
            }

            if (score <= 30)
            {
                MessageBox.Show("Very poor. \nYou got " + score + "\nClick review to view your Incorrect Answers");//Prints User score
            }
            else if ((score > 30) && (score <= 50))
            {
                MessageBox.Show("Could be better, You got " + score + "\nClick review to view your Incorrect Answers");//Prints User score
            }
            else if ((score >50) && (score<=60))
            {
                MessageBox.Show("Not bad. Your score was " + score + "\nClick review to view your Incorrect Answers");//Prints User score
            }
            else if ((score >70) && (score<=90))
            {
                MessageBox.Show("Well done your score was " + score + "\nClick review to view your Incorrect Answers");//Prints User score
            }
            else if (score == 100)
            {
                MessageBox.Show("Fantastic you got every question right");//Prints User score
            }
        }

        private void Review_Click(object sender, EventArgs e)
        {
        }
    }
}

Okay so I pretty much change everything that involved your arrays Question, option1, option2, option3, option4, correct_answers, User_answer (sorry I didn't use the same variables names you did ... totally spaced on this)

Anyway, we did away with all these arrays and made 1 called Question. The array is first declared just where you declared all your others

QuizProblems [] Question = new QuizProblems[NumQuestions]; //creates ours new 

Now you will then see that I call a function in the Form1() function called SetupQuestions(). I did this to keep the code clean, but this class is where we now create all your arrays but at once, into one item.

Let's look at the first line in that function

Question [0] = new QuizProblems("What component inside a computer is non volatile memory?", new string [] {"ROM", "RAM", "SDRAM", "DRAM"}, 1);

Here we are assigning Question[0] a new type of QuizProblems (which is the custom object we made up above), and then passing in the variables. Here we first pass in the question, then the 4 possible options for the question, and then finally the correct answer.

If you were to look at the top of the of the whole code I posted, you'd see the constructor for type QuizProblems and see how I am assigning these values. Notice how I do not pass in a selectedAnswer or a flag. These are assigned the same for every new QuizProblem (which we do with each index of the Question array). (By the way I use -1 as a default because it's not a default assigned value and usually can't be achieved easily, that way if it is, you can check to see if something went wrong and got skipped or what not)

Alright so do that same process for all 10 indexes. Hopefully this makes sense.

Now let's work our way down the code, more specifically into StartQuiz_Click() where I changed the code up.

            QuestionLabel.Text = Question[i].question; //print the question in that array index

            RadioAnswer1.Text = Question[i].answers[0]; //prints the first option for an answer
            RadioAnswer2.Text = Question[i].answers[1]; //prints the second option for an answer
            RadioAnswer3.Text = Question[i].answers[2]; //prints the third option for an answer
            RadioAnswer4.Text = Question[i].answers[3]; //prints the fourth option for an answer

            if ((RadioAnswer1.Checked) || (RadioAnswer2.Checked) || (RadioAnswer3.Checked) || (RadioAnswer4.Checked))
            {
                if (RadioAnswer1.Checked)
                {
                    Question[i].selectedAnswer = 1;
                }
                else if (RadioAnswer2.Checked)
                {
                    Question[i].selectedAnswer = 2;
                }
                else if (RadioAnswer3.Checked)
                {
                    Question[i].selectedAnswer = 3;
                }
                else if (RadioAnswer4.Checked)
                {
                    Question[i].selectedAnswer = 4;
                }
                else
                {
                    MessageBox.Show("You have not not answered the question. \n Please answer the question before proceeding");
                }
            }

Notice how now we are working with the one single Question variable. The whole get/set we do at the very top of the original posted code allows us to pass back and forth variables. So for instance here we can retrieve the text for the question at index i with QuestionLabel.Text = Question[i].question;, but we can also assign the user's selected answer with Question[i].selectedAnswer = 1;.

Hopefully you are still with me here, cause we are almost done. The last piece of code I changed was in Finish_Click(). Just a simple if statement

for (int i = 0; i < 9; i++)
{       
    if (Question[i].selectedAnswer == Question[i].correctAnswer) //checks if the answer is correct
    {
        score = score + 1;
    }
}

By the way I wasn't sure, but I should ask, is that for loop only meant to run 9 times? Missing the score of the 10 answer? Also may I suggest changing that "9" to work off your constant NumQuestions. That way if you decided to change it to say 20, you don't have to go change the for loop's value (of course you could do other things such as detecting the size of the array, but we'll leave that alone for now).

Okay so there you have it. Oh I almost forgot, for your Flag option, all you simply have to do is Question[i].flagged. Then when assigning or retrieving values, do just as above with the other parts of the code.

Make sense? I know it's a long write up

Edited 3 Years Ago by JOSheaIV

First of all, Ancient Dragon's way is not wrong in anyway, in fact he probably knows 20 times more then me. The only reason I suggested doing it this way, was because I personally don't like using multiple arrays, trying to sync up everything and having multiple names

I agree, multiple arrays is not a good way to implement the program. -- but the OP may not have the experience/knowledge necessary to do it that way.

I agree, multiple arrays is not a good way to implement the program. -- but the OP may not have the experience/knowledge necessary to do it that way.

Doh! That is very true. I always get wrapped up sometimes like that.

I guess I just thought since this would be a better way, might be a good time to learn about it (why I decided to try and step through it and what not). I personally we really glad when I learned about this (even better when I finally decided to start using get/set).

Hi thank you so much for your replies. When looking at the code how you have posted, it does make a lot more sense how you have set up your arrays not to mention it being so much neater. As ancient dragon mwntioned,i am new to C# and a lot of the knowledge i know comes from C++

I am still a bit confused on how to setp the question[i].flagged option within the code. What i am trying to do is the user flags the question, if they are unsure of the answer. when they click finish with a pop up box will allow the usewr to review flagged questions and answer them, When the question is answered then the flag is removed.

Another thing probally sound really stupid but when i created my next button i used i++ as an incrmentral counter/ Would i need to use a decremental counter i-- to work backwards?

Thanks again for any help. You are already teaching me so much

Okay let's take a crack at the flagging first.

This is rather simple. Now when the user decides to mark a question as flagged, whether it be a radio button, check box, ext. You will do pretty much the exact same process as you would for storing the user's answer.

So when you are checking the radiobuttons if they are checked, simply add an 'if' statement around there that checks if the user indicated the question has been flagged. If so, simply do Question[i].flagged = true;.

Then once you get to the end of the program you can do something along the lines of

for(int j = 0; j < Question.Length; j++)
{
    if (Question[j].flagged == true) //if the user flagged the question
    {
        /* Have the user re-answer the question */
    }
}

So then you can retrieve the question with Question[j].question (using it like a string) and then of course you can store the value of their new answer, and then simply change the flag to false (however the way this loop works, it wouldn't matter to much)

(note that code above, the question, is done in the if statement of the code statement)

------------------------------------------------

Now onto the decrement question. I am not sure why you'd need to decrement anything. The example I did above simply goes back through the array (no need to treat it like a queue or stack). Remember that the array you can access any index at any time (well within limits of course, but you get the idea).

Got any more questions, I'll do what I can to help

Edited 3 Years Ago by JOSheaIV

Hi Thanks for your reply again.

So i have added a check box, called flagQuestion and have created the following if statement:

if (flagQuestion.checked==true)
{
    question[i].flagged=true;
    i++
}

I added the i++ because i need to increment through the questions. I am not sure i have done this right. To review the flagged questions i have created a button where they click on it and i would like a messagebox to pop up and say question number 1 has been flagged, but not sure how to do this.

My comment about the decremnetal counter. Is that for the user to move into the next question the click "Next" for them to go back to the previous questions they would click "previous". How could i get that buutton to work.

if this helps i could always email you a dropbox link to file so you could see what my program looks like and how i want iot to work?

Thanks for your help again

Perhaps it is just a matter of style, but personally I prefer if (flagQuestion.checked), rather then if (flagQuestion.checked==true)

Comments
I do too. Less code, but harder for non programmers to read.

First of all, I would apply what ddanbe suggested, makes it cleaner and easier.

Secondly, I don't think you want to i++ in that part, because if you look at the full code I posted awhile back, around line 200, you'll see that you already have i++ (plus putting it in that if statement, if they don't flag it, then you won't increment). Also make sure you uncheck it when you go to the next question, that way if the user checks it on one, it doesn't stay checked.

Now I wanted to point something out. Your variable 'i' is used to move through the question it's rather simple. If you click Next, you do "i++" (the display your message, the questions, ext).

Now if you want to make a Previous button, then you do the same thing but "i--". Remember, that 'i' is how you are navigating through your questions. Question 1 for instance will always be in array index 0, while Question 2 will always be in array index 1 (at least in the case here).

The way you are using 'i' is to manuever through your array. So next question it "i++" and previous is "i--". Also I'd advise you be careful, you might want to add a check like

if(i <= 0)
{
    //Disable "Previous" button
}

That way we don't get an array out of bounds error.

Now there's some more you might want to do for the Previous and Next, but for now, get this working, then we can talk about that

I tried using i-- followed by the above if statment you mentioned. I also tried putting the same section of code in the next button in two the previous button (accept i++) and it doesnt cycle through the questions correctly. When i click previous it will display the next question, then when clickjed again it will go back through the past questions

Alright, so I might have been a little to vague on the Previous button. So let's break this down a little more. We'll be looking at the Next_Click primarily (you'll have to write the previous click on your own, but hopefully after this it won't be to hard).

Okay let's break down the Next_Click() a little more, and do some tweaks to it to make it a little more effective and hopefully help you understand how to write the Previous_Click()

Looking at you Next_Click() currently it's set up to display the current question and answers, and store the user's answer and then proceed to the next problem (so technically you are out of sync). For instance you are storing the answer for say question #5 in array slot #5 (which would be the 6th slot, remember it starts at 0). Instead, why not move it around a little. The pieces are there, just in a different order.

Okay let's start.

First of all, when the user clicks next, we assume they are done with the current problem, so we should store their answer first before moving on. This means, take the following code and move it to the top of Next_Click() (below all your code that used to Enable or set Visible that is already at the top ... everything before QuestionLabel.Text = Question[i].question; //print the question in that array index)

if ((RadioAnswer1.Checked) || (RadioAnswer2.Checked) || (RadioAnswer3.Checked) || (RadioAnswer4.Checked))
{
        if (RadioAnswer1.Checked)
        {
                Question[i].selectedAnswer = 1;
        }
        else if (RadioAnswer2.Checked)
        {
                Question[i].selectedAnswer = 2;
        }
        else if (RadioAnswer3.Checked)
        {
                Question[i].selectedAnswer = 3; 
        }
        else if (RadioAnswer4.Checked)
        {
                Question[i].selectedAnswer = 4;
        }
        else
        {
                MessageBox.Show("You have not not answered the question. \n Please answer the question before proceeding");
        }
}

Alright, now here we are first taking the users the answer and storing it, before moving onto the next question. This works well to, because you could put a check in here, where if the user doesn't answer the question, the program won't move on (something like a if statement that works off a boolean which is set true in all the if statements except the final 'else' it's set false)

Now we have stored the users answer, we can move onto the next question, so here we increment, with your "i++" and then update the questions with the following

i++;

QuestionLabel.Text = Question[i].question; //print the question in that array index

RadioAnswer1.Text = Question[i].answers[0]; //prints the first option for an answer
RadioAnswer2.Text = Question[i].answers[1]; //prints the second option for an answer
RadioAnswer3.Text = Question[i].answers[2]; //prints the third option for an answer
RadioAnswer4.Text = Question[i].answers[3]; //prints the fourth option for an answer

Note I included the "i++;" for you in there.

Here we then move onto the next question. So really you just need to reorganize those few pieces of code so it will look something like (Also note, I added an "if" statement at the end, that way we don't try to display a question that is out of the array limits

private void Next_Click(object sender, EventArgs e)
{
        Previous.Enabled = true;//Enables the previous button

        RadioAnswer1.Visible = true;
        RadioAnswer2.Visible = true; //Quiz answers now visible to user
        RadioAnswer3.Visible = true;
        RadioAnswer4.Visible = true;

        A1.Visible = true; //answer numbers now visible to the user
        A2.Visible = true;
        A3.Visible = true;
        A4.Visible = true;

        if ((RadioAnswer1.Checked) || (RadioAnswer2.Checked) || (RadioAnswer3.Checked) || (RadioAnswer4.Checked))
        {
                if (RadioAnswer1.Checked)
                {
                        Question[i].selectedAnswer = 1;
                }
                else if (RadioAnswer2.Checked)
                {
                        Question[i].selectedAnswer = 2;
                }
                else if (RadioAnswer3.Checked)
                {
                        Question[i].selectedAnswer = 3; 
                }
                else if (RadioAnswer4.Checked)
                {
                        Question[i].selectedAnswer = 4;
                }
                else
                {
                        MessageBox.Show("You have not not answered the question. \n Please answer the question before proceeding");
                }
        }

        i++;

        if (i == NumQuestions)
        {
                Finish.Visible = true;
                StartQuiz.Enabled = false;
        }
        else
        {
            QuestionLabel.Text = Question[i].question; //print the question in that array index

            RadioAnswer1.Text = Question[i].answers[0]; //prints the first option for an answer
            RadioAnswer2.Text = Question[i].answers[1]; //prints the second option for an answer
            RadioAnswer3.Text = Question[i].answers[2]; //prints the third option for an answer
            RadioAnswer4.Text = Question[i].answers[3]; //prints the fourth option for an answer
        }

} //end of Next_Click

Edited 3 Years Ago by JOSheaIV

This is something i learned from a friend on other forum.
Before i write 1 line of code, i need to plan out at least some functionalities of the solution i'm gonna write.

You start with a whiteboard and plan. What is it you need? For starters:

  • You know you need questions and their corresponding answers.
  • You know you're going to need some means of loading these from files off the drive.
  • You know you're going to need some logic code for selecting questions and tracking scores.
  • You don't know it yet, but you probably want to get in the habit of create some type of logging system so your application can log what it is doing so you can hunt down bugs users report.
  • None of that involves the GUI.

Knowing this would lead you to creating some basic classes for these needs.

Class Question
{
   string QuestionText { get; set;}
   List<string> Answers {get; set;}
   int CorrectAnswer;
}

Next you need to decide on a file structure. How are your quizes going to be laid out within the file on the hard drive? Since it is such a simple need, I would start with a .txt file because anyone can sit down and type up a quiz file for your program using Notepad.

Q: Four score and ____ Years ago, our forefathers...
A: five
A: six
A: seven
A: eight
C: 3
Q: Fourteen hundred and ninety _____ , Columbus sailed the ocean blue...
A: one
A: two
A: three
A: four
C: 2

As you can see this text file format is pretty straight forward. You can show it to anyone and the can see what is expected of them. Q: Signifies the question. A: signifies an answer. C: signifies the correct answer from the list above.

So we need a game engine. A class that can read the questions, randomly select them, track scores etc. The engine is not a GUI thing. It should receive a string as the file path for reading the question file and have methods and events that the GUI reacts to. Something like:

class GameEngine
{
    string QuestionFilePath {get; set;}
    List<Question> Quiz;
    List<int> AvailableQuestions; // When a question is randomly selected we take it's index out of this list so we don't choose it again

    public static EventHandler<QuestionEventArgs> NewQuestion;

    public void SelectNewQuestion()
    {
       int newQindex = SelectRandomQuestionIndex();
       QuestionTemp = Quiz[(newQindex)];
       RaiseNewQuestionEvent(QuestionTemp);
       AvailableQuestions.RemoveAt(newQindex);
    }

    void LoadQuestions(string path)
    {
    }

    int SelectRandomQuestionIndex()
    {
    }

    void RaiseNewQuestionEvent(Question question)
    {
       EventHandler<QuestionEventArgs> handler = NewQuestion;
       if (handler != null) handler(this,  question);
    }
}

Once you have an engine for your game you can create a GUI because it has something to interact with.

How it looks is up to you. More importantly you can change the GUI designs pretty quickly until you find what you like because the game logic is not in the Form's code. You can make it a single form... multiple forms... you can upgrade from WinForms to WPF...

You can also upgrade the game engine completely separate from the GUI. You and your team can now break up the development of the game: One can work on the engine while another the GUI.

Taken from: DreamInCode.net

Comments
Well done!

Thanks for your psot Michael, and the link. I can say that this is not for homework this is for personal development for work, and i need to know C# by the end of next year as i have to teach the basics of it. Which i cant do if i dont know it myself which is why i am starting now.

Hi

I have created my quiz qns,all button are working fine except one button NEXT flag.
Does any body can assist how to impelement please.

I have a Pseudocode to share but I am unable to implement.
" NEXT FLAG QUESTION" CODE

Populate first flag
If not Populate next flag
Do while
++
Click next flag Check previous flag answer
(same as next button)
Populate next flag
if flagged Else
Do while ++
If I =10 end

Since this is a different question you should really start your own. A word of advice, use real code. Pseudocode doesn't tell anyone about what kind of structure you're using or how any classes are set up, etc..

This is what I have done on Next Flag question,but it seems not working. a

private void btnNextFlagQuestion_Click(object sender, EventArgs e)
        { 
          for(i = 0; i<10;i++) 

              if(i<10) 
           MessageBox.Show ("messagebox you have finished" );
            } 
            if (User_Answer[] != Correct_Answer[])
                {
                    textBox1.Text = (i + 1) + ". " + Questions[i];
                    radioButton1.Text = option1[i];
                    radioButton2.Text = option2[i];
                    radioButton3.Text = option3[i];
                    radioButton4.Text = option4[i];
                    btnFinish.Enabled = true;
                    i++;
    }

        if ((radioButton1.Checked) || (radioButton2.Checked) || (radioButton3.Checked) || (radioButton4.Checked) || (chkFlagQuestion.Checked))




        }
    }
}
}
}
This article has been dead for over six months. Start a new discussion instead.