Joseph

Joseph

  • NA
  • 14
  • 17.7k

StreamWriter

Apr 16 2011 9:23 PM

StreamWriter โ€“ Random Insult Generator

You will create a program that will generate a desired number of random insults by combining a name, a verb, and an object from three arrays of strings. The generated insults will be displayed. Your program will use methods to generate the insults and save the results to a text file.

Main Program

The main program will contain three arrays of strings that are provided with initial values:

An array of names of the people to insult.

An array of verbs to use in the insults.

An array of objects for the insults.

The arrays will be provided initial values that will be used to generate the insults. Main will call the method MakeInsults() which will return a string array of insults. Main will call DisplayInsults() to display the insults on the screen. After the insults have been displayed, ask the user if they wish to run the program again. If the user responds with "yes", then clear the screen and run the program again.

MakeInsults()

The method MakeInsults() will be passed the array of names, verbs, and objects as three string array parameters. It will use GetValue() (from your library created in an earlier ICA) to input the number of insults desired. The minimum number of insults is 5, and the maximum is 100. For each insult generated a random name, verb, and object will be combined into a single insult (as shown in the above example) and stored in an array of insults. After all of the desired insults have been generated, return the array of insults to the main program.

SaveInsults()

The method SaveInsults() will be passed the array of insults, and will save them to a specified text file. This method will input the name of the file to be used to save the insults. Add the extension .txt to the file name that was entered. Ask the user if they wish to append to the file. If they wish to append to the file, open the file with appending. If appending is not desired, open the file without appending. Save the insults to the text file (one insult per line).

I Have This Code and its working...My problem is i dont know how to use the streamwriter and the method SaveInsults().

         static void Main(string[] args)
        {

            string[] sNames = new string[5] { "Bill", "Al", "John", "JD", "Ross" };
            string[] sVerbs = new string[5] { "talks to", "runs", "licks", "flushes", "uses" };
            string[] sObjects = new string[5] { "Safeway carts", "Microsoft", "old mops", "dead cows", "Vista" };
            string[] sInsults;
            string sAgain;

            Console.Title = "StreamWriter โ€“ Random Insult Generator";

            do
            {
                Console.Clear(); // clear console for each run
                Console.WriteLine("{0, 40}", "ICA23 - StreamWriter โ€“ Random Insult Generator");

                sInsults = MakeInsults(sNames, sVerbs, sObjects);
                DisplayInsults(sInsults);

                Console.Write("Run Again? \"yes\" to run again: ");
                sAgain = Console.ReadLine();
            }
            while (sAgain == "yes");
            Console.Clear();
        }
        static public string[] MakeInsults(string[] sNames, string[] sVerbs, string[] sObjects)
        {
            Random random = new Random();
            int iTest = sNames.Length;

            CUtilities.GetValue(out iTest, "\nEnter number of insults to generate : ", 5, 100);
            string[] sInsults = new string[iTest];

            int iFirst, iSecond, iThird;
            for (int i = 0; i < iTest; i++)
            {
                // generate three random numbers
                iFirst = random.Next(0, 4);
                iSecond = random.Next(0, 4);
                iThird = random.Next(0, 4);

                // create random insult
                sInsults[i] = String.Format("{0} {1} {2}", sNames[iFirst], sVerbs[iSecond], sObjects[iThird]);

            }
            return sInsults;
        }
        static public void DisplayInsults(string[] sInsults)
        {
            Console.WriteLine("\nHere are the insults...\n");
            foreach (string sInsult in sInsults) Console.WriteLine(sInsult);
            Console.WriteLine();
        }


Answers (1)