1

I have to create a quiz using a string array and a running score will be displayed, there will be one point added for each correct answer and once point subtracted for each wrong answer. I've got the first question working fine I just don't know how to do it for the next ones. I only have one submit button so all the code for the first question is connected to that button. How do I make it so when you submit your second answer it tells you its correct then moves on? I have been told a for loop would work well with this but I don't know how to implement it.

int score = 0;
int i = -1;
int a = 0;

string[] questions = new string[] { 
    "What is 9 cubed?", 
    "What is 6+3?", 
    "What type of animal is tuna sandwiches made from?", 
    "What is 18 backwards?" };

string[] answers = new string[] { 
"9", "81", "729", "2", "4", "2", 
"9", "1", "zebra", "aardvark", 
"fish", "gnu", "31", 
"81", "91", "88" };

private void btnStart_Click(object sender, EventArgs e)
{
    if (i < questions.Length)
    i++;
    //txtScore.Text = score;

    lblQuestion.Text = questions[i];

    radA.Text = answers[a];
    a++;
    radB.Text = answers[a];
    a++;
    radC.Text = answers[a];
    a++;
    radD.Text = answers[a];
    a++;

    btnStart.Visible = false;
    btnStart.Enabled = false;
    btnSubmit.Visible = true;
    btnSubmit.Enabled = true;

}

private void btnSubmit_Click(object sender, EventArgs e)
{
    {
        if (i == 0 && radB.Checked)
        {
            MessageBox.Show("Correct");
            score++;
            txtScore.Text = Convert.ToString(score);
            btnSubmit.Enabled = false;
            btnSubmit.Visible = false;
            btnStart.Visible = true;
            btnStart.Enabled = true;
            btnStart.Text = "Next";
        }

        else
        {
            MessageBox.Show("Incorrect");
            score--;
        }
7
  • did you want the score to be updated as the user inputs the answer? or only when they click submit? Commented Nov 11, 2013 at 3:47
  • if the user gives one wrong answer he should not be allowed further right? Commented Nov 11, 2013 at 3:54
  • @Catalyst it's radio buttons so the way the user can input an answer is by clicking the submit button :) Commented Nov 11, 2013 at 3:55
  • @Sudhakar No, they can still continue to the rest of the quiz I suppose but they loss a point for every incorrect submission Commented Nov 11, 2013 at 3:57
  • what should happen when user press submit? Commented Nov 11, 2013 at 3:59

1 Answer 1

2

problems : here you have hardcoded answer with radio button b value as below:

if (i == 0 && radB.Checked)

it will only check answer with radio button b and it will only work for first question.

you are not continuing this process for rest of the Questions.

solution: i have added one strng array which contain all quiz answers for your questions. so when user press submit button it will verify with respective answer and continue the same process till the end.

code as below:

int score = 0;
int i = -1;
int a = 0;

string[] questions = new string[]
{
    "What is 9 cubed?", "What is 6+3?", 
    "What type of animal is tuna sandwiches made from?",
    "What is 18 backwards?"
};

string[] answers = new string[] {
   "9", "81", "729", "2", 
   "4", "2", "9", "1", 
   "zebra", "aardvark", "fish", "gnu", 
   "31", "81", "91", "88"
};

string [] quizAnswers=new string[]{"729","9","aardvark","81"};
private     void btnStart_Click(object sender, EventArgs e)
{
    if (i < questions.Length)
    i++;
    //txtScore.Text = score;

    lblQuestion.Text = questions[i];

    radA.Text = answers[a];
    a++;
    radB.Text = answers[a];
    a++;
    radC.Text = answers[a];
    a++;
    radD.Text = answers[a];
    a++;

    btnStart.Visible = false;
    btnStart.Enabled = false;
    btnSubmit.Visible = true;
    btnSubmit.Enabled = true;

}

private void btnSubmit_Click(object sender, EventArgs e){


    if(getSelectedAnswer().Equals(quizAnswers[i]))
    {
        MessageBox.Show("Correct");
        score++;
        txtScore.Text = Convert.ToString(score);
        btnSubmit.Enabled = false;
        btnSubmit.Visible = false;
        btnStart.Visible = true;
        btnStart.Enabled = true;
        btnStart.Text = "Next";
    }

    else
    {
        MessageBox.Show("Incorrect");
        score--;
        txtScore.Text = Convert.ToString(score);
        btnSubmit.Enabled = false;
        btnSubmit.Visible = false;
        btnStart.Visible = true;
        btnStart.Enabled = true;
        btnStart.Text = "Next";
    }
}
string getSelectedAnswer()
{
    if (radA.Checked)
    return radA.Text.ToString();
    if (radB.Checked)
    return radB.Text.ToString();
    if (radC.Checked)
    return radC.Text.ToString();
    if (radD.Checked)
    return radD.Text.ToString();
    return "";
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome :) , my suggestion is make textbox readonly so that user can not modify it, and run the run this loop till the length of questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.