Ashley Brown
Ashley Brown

Reputation: 23

When executing second for loop into listbox program populates no data

so the assignment is to read from the student file into an array and read into the answer key array, compare the two and output a grade based on the array comparison. the issue i'm having is that when i try to load the answer key into it's array it's like its not even getting the data, because all the questions output as wrong. below is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AshleyBrown_CPT185A01S_Chapter7Lab
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //variables
        private const int SIZE = 20; //current # of q's on test
        private int index = 0, count = 1; //counter variables
        private int wrong = 0, right = 0; //grade variables

these are the arrays that are used for the answers:

        //arrays
        private char[] studentAnswers = new char[SIZE];
        private char[] answerKey = new char[SIZE];


        private void calculateBtn_Click(object sender, EventArgs e)
        {
            //prevents any file errors
            try
            {
                ReadStudentFile();
                ReadAnswerKey();
                CompareAnswers();
            }
            catch
            {
                MessageBox.Show("File doesn't exist or has the wrong name.");
            }

        }

        private void clearBtn_Click(object sender, EventArgs e)
        {
            //Clear Form
            studentAListBox.Items.Clear();
            correctAListBox.Items.Clear();
            wrongAListBox.Items.Clear();
            incorrectBox.Text = "";
            correctBox.Text = "";
            percentBox.Text = "";

        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            //close program
            Close();
        }

method for reading the student file that works:

        private void ReadStudentFile()
        {
            //Stream Reader Setup
            StreamReader studentFile;
            studentFile = File.OpenText("C:\\Users\\aabro\\Documents\\_CPT 185\\AshleyBrown_CPT185A01S_Chapter7Lab\\Student File.txt");

            //Read Student Answers into studentAnswers Array
            while (index < studentAnswers.Length && !studentFile.EndOfStream)
            {
                studentAnswers[index] = char.Parse(studentFile.ReadLine());
                index++;
            }

            //Close Student Answer file
            studentFile.Close();

            //Display Student Answers
            foreach (char answer in studentAnswers)
            {
                studentAListBox.Items.Add(count + ". " + answer.ToString());
                count++;
            }
        }

method for reading answer key that populates with no data:

        private void ReadAnswerKey()
        {
            //Stream Reader Setup
            StreamReader answerFile;
            answerFile = File.OpenText("C:\\Users\\aabro\\Documents\\_CPT 185\\AshleyBrown_CPT185A01S_Chapter7Lab\\Answer Key.txt");

            //Read Answer Key in answerKey Array
            while (index < answerKey.Length && !answerFile.EndOfStream)
            {
                answerKey[index] = char.Parse(answerFile.ReadLine());
                index++;
            }

            //Close answer key file
            answerFile.Close();

            //clear count
            count = 1;

            //display answer key in correct answer list box
            foreach (char key in answerKey)
            {
                correctAListBox.Items.Add(count + ". " + key.ToString());
                count++;
            }
        }

        private void CompareAnswers()
        {
            //reset count
            count = 1;

            for (index = 0; index < answerKey.Length; index++)
            {
                //determine if answer is right
                if (studentAnswers[index] != answerKey[index])
                {
                    wrongAListBox.Items.Add(count + ". " + answerKey[index]);

                    wrong++;
                    count++;
                }
            }

            //fail display
            if (wrong > 5)
            {
                MessageBox.Show("Student has failed");
            }

            //calculations
            double pointPerQ = 5;
            double wrongTotal = wrong;
            double wrongPointTotal = wrong * pointPerQ;
            double grade = 100 - wrongPointTotal;

            //output grade information
            incorrectBox.Text = wrong.ToString();
            correctBox.Text = right.ToString();
            percentBox.Text = grade.ToString("p0");
        }
    }    
}

this is the current output from running the program, I did double check the file names and contents as well.

output of current code

Upvotes: 1

Views: 59

Answers (1)

Lazar Đorđević
Lazar Đorđević

Reputation: 533

One issue that I saw in code is using of index in multiple while loops without previously assigning to 0. I suggest using of local variables(variable which exists only in the current block of code).

Also in my opinion it will be good to declare and initialize new variable in for-loop like this:

for(int index = 0; index < answerKey.Length; index++) 
{
  // your code
}

it will be more readable and easier if you want later to separate loops in methods or move in service or helper.

Upvotes: 1

Related Questions