Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C#
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 13-Aug-19 21:15pm
v2

C#
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.
 
Share this answer
 
v2
How about we ditch splitting it on the space, and instead split it on the hyphen:
C#
// 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"
 
Share this answer
 
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 "____".
 
Share this answer
 
v2
alistairb147[^] wrote:

C#
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:

C#
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900