COYG
COYG

Reputation: 1598

Getting streamreader to read specific line in text file, then write to console c#

I have a struct that holds 4 variable types, the data of this struct is held in a text file. The struct is made up of question number, question level, question, and question answer. I am trying to print only the question to the console window but atm the program insists on writing first the question number then the level then finally the question then the question answer. I only want to print the question to the console screen. Here's what I have so far:

static void quiz(QuestionStruct[] _quiz)
{
    bool asked = true;
    int score = 0;
    int AmountAsked = 0;

    string level = "1";
            string ans;
int pos = 0;

var pathToFile = @"..\..\..\Files\questions.txt";
using (StreamReader sr = new StreamReader(pathToFile, true))
{      
    while (AmountAsked < 20 || score >= 50)
    {
        Console.Clear();
        //int pos = 0;
        if (level == "1")
        {

            AmountAsked++;
            questions[pos].Question = sr.ReadLine();
            Console.Write(questions[pos].Question);
            ans = Console.ReadLine();
            if (ans == questions[pos].answer)
            {
                level = "2";
                score = score + 1;
                while (questions[pos].Level != "2")
                {
                    pos++;
                }
            }
        }
    }
}

Upvotes: 0

Views: 965

Answers (2)

Ishtiaq
Ishtiaq

Reputation: 1058

If there are individual lines for Question number, leve, question and answer, then you have to perform 4 sr.ReadLine() in a sequence to assign each correct line to the member of strcut.

static void quiz(QuestionStruct[] _quiz)
{
    bool asked = true;
    int score = 0;
    int AmountAsked = 0;

    string level = "1";
    string ans;
    int pos = 0;

    var pathToFile = @"..\..\..\Files\questions.txt";
    using (StreamReader sr = new StreamReader(pathToFile, true))
    {


        while (AmountAsked < 20 || score >= 50)
        {
            Console.Clear();
            //int pos = 0;
           while(!sr.EndOfStream)
            {
            if (level == "1")
            {

                AmountAsked++;
                // Remember the order in which you are storing the values to each member of struch
                questions[pos].number = sr.ReadLine();
                questions[pos].level = sr.ReadLine();
                questions[pos].Question = sr.ReadLine();
                questions[pos].answer = sr.ReadLine();

                Console.Write(questions[pos].Question);
                ans = Console.ReadLine();
                if (ans == questions[pos].answer)
                {
                    level = "2";
                    score = score + 1;
                    while (questions[pos].Level != "2")
                    {
                        pos++;
                    }
                }
            }
          } 

Upvotes: -1

Mike Hixson
Mike Hixson

Reputation: 5189

It looks like you are not reading your file in the correct order. You are storing the first line of your file in the Question member, and the second line in the Answer member. Then you never read a line for question number of level. This dosen't match the file layout.

Also its hard to see the problems in the code because it is doing too many things. You should refactor the code so that reading the file is one method, then performing the quiz is another method. This is a software design principal called separation of concerns http://en.wikipedia.org/wiki/Separation_of_concerns.

Here is the code refactored. Had you skipped reading line, or read lines out of order with this code, it would have been easier to spot.

public void Quiz()
{
    List<QuestionStruct> questions = GetQuestions(@"..\..\..\Files\questions.txt");

    RunQuiz(questions);
}


// My job is to parse the file
public List<QuestionStruct> GetQuestions(string pathToFile)
{
    List<QuestionStruct> questions = new List<QuestionStruct>();

    using (StreamReader sr = new StreamReader(pathToFile, true))
    {
        while (!sr.EndOfStream)
        {
            QuestionStruct q = new QuestionStruct();
            q.Number = sr.ReadLine();
            q.Level = sr.ReadLine();
            q.Question = sr.ReadLine();
            q.Answer = sr.ReadLine();

            questions.Add(q);
        }
    }

    return questions;
}


// My job is to run the quiz
public void RunQuiz(List<QuestionStruct> questions)
{

    bool asked = true;
    int score = 0;
    int AmountAsked = 0;

    string level = "1";
    string ans;
    int pos = 0;

    foreach (QuestionStruct question in questions)
    {
        if (!(AmountAsked < 20 || score >= 50))
            break;

        Console.Clear();
        //int pos = 0;
        if (level == "1")
        {
            AmountAsked++;

            Console.Write(question.Question);
            ans = Console.ReadLine();

            if (ans == question.Answer)
            {
                level = "2";
                score = score + 1;
                while (question.Level != "2")
                {
                    pos++;
                }
            }
        }
    }
}

Upvotes: 0

Related Questions