C
C#16mo ago
Hussain

❔ Urgent help - Multiple Choice Quiz Project with Csv File - Windows form C#

I have two forms, Menu form, and Quiz Form. What to Know: - user can select 1 of 2 topics : Cell Biology or Organisation - Questions are passed through via Csv file - Excel - Im not sure how i can filter the questions displayed based on the users topic selected. - i created a ID for each question so for if topic A selected questions 1-15 displayed, if Topic B selected 16 - 30 - I need 10 questions picked at random and displayed from the 15 questions for each topic - the total score is displaying weird out of 30 after only 10 questions answered. Issues: - Questions not filtered based on user selected topic - MAIN PROBLEM - Right Answers tracker not working properly - score board not working please guys im in desperate need of help ive provided screenshots and source code https://paste.mod.gg/ditwjlzstnfa/0
BlazeBin - ditwjlzstnfa
A tool for sharing your source code with the world!
33 Replies
mtreit
mtreit16mo ago
Let's start with "the total score is displaying weird out of 30 after only 10 questions answered."
MessageBox.Show($"Your score: {rightAnswers} out of {QuestionsList.Count}");
MessageBox.Show($"Your score: {rightAnswers} out of {QuestionsList.Count}");
What do you expect this line of code to do?
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Hussain
HussainOP15mo ago
Ive fixed that now to: MessageBox.Show($"Your score: {rightAnswers} out of 10");
mtreit
mtreit15mo ago
Hard coding '10' rather than dynamically computing how many questions were actually asked seems like going in the wrong direction.
Hussain
HussainOP15mo ago
im aware 30 questions were loaded in and that was counted
mtreit
mtreit15mo ago
That doesn't change the fact that hard-coding the string '10' is not the right way to do this kind of thing.
Hussain
HussainOP15mo ago
understood, do you know how i can only read in only questions that have the topic the user selected from the menu form
mtreit
mtreit15mo ago
What have you tried?
Hussain
HussainOP15mo ago
I was thinking I could do an if statement , if selected topic from the combo box equals oneQuestion.Topic then read it it in But I’m not sure how to do it
mtreit
mtreit15mo ago
while ((line = QuestionFile.ReadLine()) != null)
{
row = line.Split(',');
oneQuestion.ID = Convert.ToInt32(row[0]);
oneQuestion.Topic = row[1];
oneQuestion.Question = row[2];
oneQuestion.Answer = row[3];
oneQuestion.Option2 = row[4];
oneQuestion.Option3 = row[5];
oneQuestion.Option4 = row[6];

// TODO: check if the question matches the selected topic.
// If not, skip it.
QuestionsList.Add(oneQuestion);

}
while ((line = QuestionFile.ReadLine()) != null)
{
row = line.Split(',');
oneQuestion.ID = Convert.ToInt32(row[0]);
oneQuestion.Topic = row[1];
oneQuestion.Question = row[2];
oneQuestion.Answer = row[3];
oneQuestion.Option2 = row[4];
oneQuestion.Option3 = row[5];
oneQuestion.Option4 = row[6];

// TODO: check if the question matches the selected topic.
// If not, skip it.
QuestionsList.Add(oneQuestion);

}
Here, I put in a comment. You just need to replace the comment with some code.
Hussain
HussainOP15mo ago
I was able to display whatever they selected to the Label
mtreit
mtreit15mo ago
Did you write most of this program yourself? Or did you copy / paste a lot of the code from somewhere?
Hussain
HussainOP15mo ago
I wrote majority myself, then use helped from chatgpt
mtreit
mtreit15mo ago
Because if you aren't sure how to use an if statement to make sure the topic matches it feels like you are missing some fundamentals.
Hussain
HussainOP15mo ago
Ik how is statemetnts work but it’s just I don’t know how I would write it , using the topic slected from the user from the Menu Form to the Quiz Form @ If u know what I mean Honestly I appreciate you helping
mtreit
mtreit15mo ago
You already have a field called selectedTopic is there a reason you can't just compare against that?
Hussain
HussainOP15mo ago
It gives syntax error
mtreit
mtreit15mo ago
Show the code you wrote and what the error says
Hussain
HussainOP15mo ago
Apologies I’m AFK, using my phone rn I’ll be home in 20 mins theres a problem
Hussain
HussainOP15mo ago
when I run the program and choose "Organisation" , it works
Hussain
HussainOP15mo ago
heres what csv file looks like for reference
Hussain
HussainOP15mo ago
Hussain
HussainOP15mo ago
when I run the program and choose "Cell-Biology" , error
Hussain
HussainOP15mo ago
mtreit
mtreit15mo ago
That error message is telling you that the file is already opened for exclusive access by something else (possibly your code) When you open a file you should use a using scope to ensure it always get disposed deterministically.
using var QuestionFile = new StreamReader(filename);
using var QuestionFile = new StreamReader(filename);
Something like that Also you probably should just read the file in once when your program starts - put the contents into a List or a Dictionary and then pick the questions from that rather than re-reading the file every time.
Hussain
HussainOP15mo ago
this happens when its run , when i choose the "cell-bioloyg topic"
Hussain
HussainOP15mo ago
Hussain
HussainOP15mo ago
right
Hussain
HussainOP15mo ago
it doesnt seem to read in "Cell-Biology":
Hussain
HussainOP15mo ago
I had the csv file open .
SinFluxx
SinFluxx15mo ago
Hussain
HussainOP15mo ago
Apologies
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server