Click here to Skip to main content
14,265,815 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am a student so apoligies for noob questions.

I have a text file with the following formatting:
dog - I took my dog for a walk
door - I opened the front door of my house
choose - you need to choose an item

I want my program to read every line within the file and store the first word within an array and store the sentence containing the word within a separate array.

I have only managed to do it manually which is not very efficient and makes it so the program will not load the sentences and words randomly. I want the program to load the lines randomly as it is a game and needs to have some challenge.

What I have tried:

string[] linesText = System.IO.File.ReadAllLines(@"/FILEPATH/file.txt");

string question = linesText[1];
string[] wordsText = question.Split(' ');

Console.WriteLine(wordsText[2] + wordsText[3] + wordsText[4] + wordsText[5] + "___"); 
//This is the manually constructed line for "I took my dog for a walk". I want the program to know which word to remove from the sentence and output it constructed correctly.

string userAnswer = Console.ReadLine();

if (userAnswer == wordsText[1])
{
Console.WriteLine("CORRECT!");
userScore++
}
else
{
Console.WriteLine("INCORRECT!");
}


How it looks when compiled:

I took my ___ for a walk

//User then enters the answer.

I want it to look like this for every line without having to manually construct every sentence. I want the program to randomly select a sentence stored in the linesText[] array and know the answer which is always the first word of every line.
Posted
Updated 3 days ago
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

int userScore = 0;

            string[] linesText = System.IO.File.ReadAllLines(@"C:\Users\srikaran.sarma\Documents\txt.txt");
            string question = linesText[1]; // Answer before '-'
            string[] wordsText = question.Split('-');   // The question part
            
            System.Console.WriteLine(wordsText[1].Replace(wordsText[0], "____ ")); //Use the Replace() function 
            
            string userAnswer = System.Console.ReadLine();

            if (userAnswer.Equals(wordsText[0].Trim()))
            {
                System.Console.WriteLine("CORRECT!");
                userScore++;
            }
            else
            {
                System.Console.WriteLine("INCORRECT!");
            }

Note: Use the array indexes correctly.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

How about we ditch splitting it on the space, and instead split it on the hyphen:
// string[] wordsText = question.Split(' ');
string[] lineText = question.Split('-');
/*
given "dog - I took my dog for a walk"

lineText[0].Trim() should be "dog"
lineText[1].Trim() should be "I took my dog for a walk"
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

I think you're better off using you own "markup"; avoids certain problems. e.g.

I took my dog for a walk and ate a hot dog.

I took my $#dog#$ for a walk and ate a hot dog.

I took my ____ for a walk and ate a hot dog.

Split on "$" and your keyword is #dog# (trim the ends). #...# is also your substitution variable for "____".
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

alistairb147[^] wrote:

Console.WriteLine(wordsText[2] + wordsText[3] + wordsText[4] + wordsText[5] + "___"); 
//This is the manually constructed line for "I took my dog for a walk". I want the program to know which word to remove from the sentence and output it constructed correctly.


If you would like to replace 'dog' with '___', you need to use string.Replace[^] method. See:

string[] sentences = {
    "dog - I took my dog for a walk",
    "door - I opened the front door of my house",
    "choose - you need to choose an item"
    };

for(int i=0; i<sentences.Length; i++)
{
    string sentence = sentences[i];
    string word = sentence.Split(new string[]{" - "}, StringSplitOptions.RemoveEmptyEntries)[0];
    sentence = sentence.Split(new string[]{" - "}, StringSplitOptions.RemoveEmptyEntries)[1].Replace(word, new string('_', word.Length));
    Console.WriteLine(sentence);
    string answer = Console.ReadLine();
    if(answer.ToLower()==word.ToLower())
        Console.WriteLine("Correct!");
    else
        Console.WriteLine("Incorrect!");
}


Result:
I took my ___ for a walk
Correct!
I opened the front ____ of my house
Correct!
you need to ______ an item
Incorrect!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100