user2318228
user2318228

Reputation: 45

Loop issue in C# Comparing two arrays

I am creating a C# GUI application and I am having problems with my loop, the application gets .txt file with student driver exam answers A,B,C or D from each on a septate line and then it compares it with the correct answers in the array in the code. My loop is always giving me that all answers are incorrect regardless of the answers being correct or incorrect.

My interface

enter image description here enter image description here

My Code:

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;
using System.IO;

namespace Mohammad_Saad_Assignment_1_Part_2_Sec_B
{
public partial class Form1 : Form
{
    //Acutal answers
    string[] correctAnswers = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", 
            "C", "D", "A", "D", "C", "C", "B", "D", "A" };

    //Student answers array of size 20
    string[] studentAnswers = new string[20];

    public Form1()
    {

        InitializeComponent();
    }

    private void btnOpen_Click(object sender, EventArgs e)
    {
        listBox2.Items.AddRange(correctAnswers);

        //Opening a new file.
        OpenFileDialog open = new OpenFileDialog();
        //Dismiss user cancelation so application dosnt crash.
        if (open.ShowDialog() == DialogResult.OK)
        {
            path.Text = open.FileName;
        }

        StreamReader sReader = new StreamReader(open.FileName);
        int index = 0;

        while (index < correctAnswers.Length && !sReader.EndOfStream)
        {
            correctAnswers[index] = sReader.ReadLine();
            index++;
        }
        foreach (string str in correctAnswers)
        {
            listBox1.Items.Add(str);
        }

        btnOpen.Enabled = false;

    }

    private void button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        int questionNumber = 1;
        int answersCorrect = 0;
        int answersIncorrect = 0;
        do
        {
            if (studentAnswers[i] == correctAnswers[i])
            {
                answersCorrect++;
            }
            if (studentAnswers[i] != correctAnswers[i])
            {
                listBox3.Items.Add("Question " + questionNumber);
                listBox3.Items.Add("Incorrect");
                answersIncorrect++;
            }
            i++;
            questionNumber++;
        }

        while (i != 20);
        lblCorrect.Text = answersCorrect.ToString();
        lblInCorrect.Text = answersIncorrect.ToString();
        if (answersCorrect >= 15)
        {
            txtResults.Text = "PASS";

        }
        if (answersCorrect < 15)
        {
            txtResults.Text = "FAIL";

        }

        btnMark.Enabled = false;
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        btnOpen.Enabled = true;
        btnMark.Enabled = true;
        listBox1.Items.Clear();
        listBox3.Items.Clear();
        txtResults.Clear();
    }
}
}

The issue is I think is in the Mark button (button 1) Any help is much appreciated.

Upvotes: 0

Views: 1510

Answers (3)

Antonio Pelleriti
Antonio Pelleriti

Reputation: 859

In general, using the debugger, and executing the code step by step will help you.

Upvotes: 2

jomsk1e
jomsk1e

Reputation: 3625

Change this code block:

while (index < correctAnswers.Length && !sReader.EndOfStream)
{
    correctAnswers[index] = sReader.ReadLine();
    index++;
}
foreach (string str in correctAnswers)
{
    listBox1.Items.Add(str);
}

to:

while (index < correctAnswers.Length && !sReader.EndOfStream)
{
    studentAnswers[index] = sReader.ReadLine();
    index++;
}
foreach (string str in studentAnswers)
{
    listBox1.Items.Add(str);
}

Upvotes: 2

Enigmativity
Enigmativity

Reputation: 117064

You have this line when you are reading in the student answers:

correctAnswers[index] = sReader.ReadLine();

It should be:

studentAnswers[index] = sReader.ReadLine();

Upvotes: 2

Related Questions