Search code examples
c#linear-search

Linear search after assigning variables


I have been making a program that has been working. But now i want to change the search function of the program to something simpler. This is what I have done: and the error message is use of unassigned local variable logg. To be clear, this is how I want the search function to look. Now I just need to figure out the variable thing.

full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Loggbok
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            DateTime tiden = DateTime.UtcNow; //Skriver ut tiden vid varje inlägg
            bool running = true; //Ger ett booleskt värde till variabeln running för att kunna skapa en loop
            List<string[]> loggbok = new List<string[]>(); //Här skapas listan som innehåller arrayen

            while (running) //Här skapas loopen
            {
                Console.WriteLine("\n************************************");
                Console.WriteLine("\nVälkommen till loggboken!");
                Console.WriteLine("\n************************************");
                Console.WriteLine("\n[1] Skriv nytt inlägg i loggboken");
                Console.WriteLine("[2] Skriv ut alla loggar");
                Console.WriteLine("[3] Sök inlägg i loggboken");
                Console.WriteLine("[4] Radera innehåll i loggboken");
                Console.WriteLine("[5] Avsluta loggboken");
                Console.WriteLine("\n************************************");
                Console.Write("\nVälj: ");

                int option; //Int eftersom valet ska vara ett heltal

                try
                {
                    option = Int32.Parse(Console.ReadLine()); //testar så att inmatningen är av typen Int
                }
                catch
                {
                    Console.WriteLine("Fel, du får bara skriva in nummer"); //Felmeddelande om inmatningen är en bokstav
                    continue;
                }

                switch (option)
                {
                    case 1:
                        string[] logg = new string[2]; //Här deklareras arrayen
                        Console.WriteLine("\n************************************");
                        Console.WriteLine(tiden);
                        Console.WriteLine("Ange en Titel:");
                        logg[0] = Console.ReadLine(); //Här sparas titeln
                        Console.Clear();
                        Console.WriteLine("\n************************************");
                        Console.WriteLine("Skriv inlägg:");
                        logg[1] = String.Format("{0}{1}{2}", Console.ReadLine(), Environment.NewLine,
                            DateTime.Now.ToString(
                                "yyyy-MM-dd HH:mm:ss")); //Här sparas inlägget samt datum och tid, detta är möjligt tack vare formattering 
                        loggbok.Add(logg);
                        break;

                    case 2:
                        foreach (string[] item in loggbok) //För att skriva ut alla items i loggboken
                        {
                            Console.WriteLine("\n--------------------------------------\n ");
                            Console.WriteLine(item[0]); //För att skriva ut titel
                            Console.WriteLine(item[1]); //För att skriva ut inlägg
                            Console.WriteLine("\n--------------------------------------\n ");
                        }

                        Console.ReadLine();
                        break;

                    case 3:
                        Console.WriteLine("\n************************************");
                        Console.WriteLine("Skriv in ett ord du vill söka efter i loggboken:");
                        string nyckelord = Console.ReadLine(); //Här sparas inmatningen av nyckelordet
                        for (int i = 0; i < logg.Length; i++)
                        {
                            if (logg[i] == nyckelord)
                            {
                                Console.WriteLine(logg[0]);
                                Console.WriteLine(logg[1]);
                            }
                            else
                            {
                                Console.WriteLine("Finns ej");
                            }
                        }
                        break;

                    case 4:
                        Console.WriteLine("\n************************************");
                        Console.WriteLine("Skriv titeln på det inlägg du vill ta bort:");
                        string title = Console.ReadLine(); //Sparar titeln på inlägget användaren vill radera

                        for (int x = 0; x < loggbok.Count; x++) //Loopa igenom varje titel
                        {
                            if (String.Equals(loggbok[x][0], title, StringComparison.OrdinalIgnoreCase)
                            ) //Icke skiftlägeskänslig matchning av titeln.
                            {
                                loggbok.RemoveAt(x); //Matchning funnen.
                            }
                            else
                            {
                                Console.WriteLine("Titeln finns inte, återgår till huvudmenyn");
                            }
                        }
                        break; //Avsluta loopen.

                    case 5:
                        running = false; //Avslutar loopen och därmed programmet
                        break;

                    default:
                        Console.WriteLine(
                            "Nu blev det fel, välj mellan [1] [2] [3] [4] [5]"); //Felmeddelande om valet är någon annan siffra än de som menyn innehåller
                        break;
                }
            }
        }
    }
}

Solution

  • This is what I have done: and the error message is use of unassigned local variable logg

    all local variables must be assigned before control leaves the containing method.

    It is declared in case 1

    remove the string[] logg = new string[2]; from case 1 and insert it before the opening try block otherwise you won't be able to use the array throughout the other cases.

    string[] logg = new string[2];//Här deklareras arrayen
    try
    {
       option = Int32.Parse(Console.ReadLine());//testar så att inmatningen är av typen Int
    }
    ...
    ...
    ...