I'm developing a quiz with 50 questions, the quiz needs 5 question levels with the user moving up and down them based on correct and incorrect answers. For my level 1 questions i have set up a text file containing 10 questions in this format:
I have created a question structure and an array for questions @ level 1:
class Program
{
static Question[] _Questions1 = new Question[10];
static int score = 0;
static int asked = 0;
static int user_input = 0;
static int user_level = 1;
static int pos = 1;
struct Question
{
public string q_no;
public string question;
public string choices;
public string answer;
}
I have written a stream reader to read my text files using my array:
static void Level_1()
{
Console.Clear();
Question _Questions1 = new Question();
string filename = @"Files\question1.txt";
using (StreamReader sreader = new StreamReader(filename, true))
{
asked += 1;
pos = 1;
_Questions1.q_no = sreader.ReadLine();
Console.WriteLine(_Questions1.q_no);
_Questions1.question = sreader.ReadLine();
Console.WriteLine(_Questions1.question);
_Questions1.choices = sreader.ReadLine();
Console.WriteLine(_Questions1.choices);
_Questions1.answer = sreader.ReadLine();
user_input = Convert.ToInt32(Console.ReadLine());
if (user_input == Convert.ToInt32(_Questions1.answer))
{
score += 2;
user_level += 1;
Console.WriteLine("\nCongratulations you have answered correctly, scored 2 points and advanced to level 2!");
Console.WriteLine("Score = {0}, Questions Asked = {1}", score, asked);
}
if (user_input != Convert.ToInt32(_Questions1.answer))
{
Console.WriteLine("\nUnfortunately you are incorrect and remain at level 1, please try again!");
pos++;
asked++;
Level_1();
}
}
this works fine for reading the first question, but whenever i try applying indexing using _Questions1[pos].q_no
so that it moves onto the next question once the user answers incorrectly, i get build errors stating:
Error 1 Cannot apply indexing with [] to an expression of type 'Quiz_Application_Rewritten.Program.Question
I realise that i have asked this exact question on a different account, i have forgotten the sign in details for it and as such have created this one as i am still stuck and i cannot progress to year 2 of my course without having this application completed and having recently motivated myself into the mindset of becoming a programmer, i would greatly appreciate any help you may have to offer.
I tried making this as specific as possible, apologies if it still isn't of any use
The loop i tried earlier:
static void Questions1(Question _Questions1)
{
string[] q_no;
string[] question;
string[] choices;
string[] answer;
q_no = new string[10];
question = new string[10];
choices = new string[10];
answer = new string[10];
string filename = @"Files\question1.txt";
using (StreamReader sreader = new StreamReader(filename, true))
{
for (int x = 0; x < 10; x++)
{
q_no[x] = _Questions1[pos].q_no = sreader.ReadLine();
}
}
with q_no[x] = _Questions1[pos].q_no = sreader.ReadLine();
throwing up the cannot apply indexing error