Skip to content

Instantly share code, notes, and snippets.

@bpmct
Created January 30, 2020 03:20
Show Gist options
  • Save bpmct/6df42cb105beec6a7d34fe80cec02d8b to your computer and use it in GitHub Desktop.
Save bpmct/6df42cb105beec6a7d34fe80cec02d8b to your computer and use it in GitHub Desktop.
using System;
using System.IO;
namespace Potter_PE6
{
class Program
{
static void Main(string[] args)
{
//Read text from the mad libs template
StreamReader sr = new StreamReader("c:\\templates\\MadLibsTemplate.txt");
//Count the number of lines in the mad libs
int lineCount = 0;
while (sr.ReadLine() != null)
lineCount++;
//Populate an array with each line
string[] madLibs = new string[lineCount];
string currentLine;
int currentLineNum = 0;
while ((currentLine = sr.ReadLine()) != null)
{
madLibs[currentLineNum] = currentLine;
currentLineNum++;
}
//Collect the user's name
Console.Write("What is your name? ");
string userName = Console.ReadLine();
//Collect a number between 1 and the amount of lines. Display an error if incorrect input
Console.Write($"Pick chose a story between 1 and {lineCount}: ");
int storyNumber;
while (!Int32.TryParse(Console.ReadLine(), out storyNumber) || storyNumber > lineCount || storyNumber < 1)
Console.Write($"Error! Enter a whole number between 1 and {lineCount}: ");
string resultString = madLibs[storyNumber];
//Close the StreamReader once we're done with it
sr.Close();
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment