当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - stream reader not moving to the next record in the file

问题描述:

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

网友答案:

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);

        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: