0

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:

http://imgur.com/Or60Yut

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

craigy g
  • 9
  • 1
  • 3
    You say you have `Question[] _Questions1` and then in the code sample you say you have `Question _Questions1`. Which is it? – Andrew Sep 11 '14 at 17:06
  • 1
    Agreed, you aren't even reading the questions in a loop. You need to rethink this design. You want to read the entire file into memory, and then ask the user the questions and move through the in-memory collection. – BradleyDotNET Sep 11 '14 at 17:12
  • the code sample is for the stream reader, `Question[] _Questions1` refers to earlier in the program when i was actually creating the array. Earlier i tried creating a loop to read my file into created arrays but ran into the same problem with indexing, i will update my question with the loop that i tried and also the code sample to include variables. This program was started a few months back and as such i'm a little fuzzy on the programming within and how to go about re-writing it – craigy g Sep 11 '14 at 17:45
  • Thats some seriously messed up code. You have to keep an *array* of questions. You passed in a single one, so of course indexing won't work. I'm honestly not sure how to help without just writing the whole thing for you, which doesn't really help you in the end. – BradleyDotNET Sep 11 '14 at 17:57
  • don't i know it lol, i think if i knew how to use arrays properly again i would be fine but it was so long ago and i haven't bothered looking at it all summer so now i can barely even read my code let alone fix it so attempts on this site are desperate. is there any possibility of emailing you with a few questions, not regarding the above application but more so advice with coding in general? – craigy g Sep 11 '14 at 18:07

1 Answers1

0

Maybe you are looking for something like this?

    static void Level_1()
    {
        Console.Clear();
        _Questions1 = new List<Question>();
        string filename = @"Files\question1.txt";
        Question question = new Question();

        using (FileStream f = File.Open(filename, FileMode.Open))
        {
            using (StreamReader sr = new StreamReader(f))
            {
                string read = sr.ReadLine();
                int qNum = 1;

                while (read != null)
                {
                    switch (qNum)
                    {
                        case 1:
                            question.q_no = read;
                            break;
                        case 2:
                            question.question = read;
                            break;
                        case 3:
                            question.choices = read;
                            break;
                        case 4:
                            question.answer = read;
                            _Questions1.Add(question);
                            question = new Question();
                            qNum = -1;
                            break;
                    }
                    qNum++;
                }
            }
            int qqNum = 0;
            do
            {
                question = _Questions1[qqNum];
                Console.WriteLine(question.q_no);
                Console.WriteLine(question.question);
                Console.WriteLine(question.choices);

                user_input = Convert.ToInt32(Console.ReadLine());

                if (user_input == Convert.ToInt32(question.answer))
                {
                    score += 2;
                    qqNum++;
                    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(question.answer))
                {
                    Console.WriteLine("\nUnfortunately you are incorrect and remain at level 1, please try again!");
                    pos++;
                    asked++;
                    Level_1();
                }
            } while (qqNum < _Questions1.Count);

        }
    }