Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm currently needing help to implement a search function within my (first ever) program, primarily a Linear Search-algorithm and potentially a Binary-algorithm as well. I've been reading through a ton of guides and tried different sorts of codes but none have really shown any result in my program. I would appreciate any help on where to start and what I need to do because I'm feeling dumb right now after hours (even days) of research and still yet to find a way.

Let me explain a bit of how the program is supposed to work. It's a blog with a menu that contains 5 choices (using switch):

1) Write a new post and save with current date+time, title and free-text

2) Show all saved posts

3) Search for a post with its title

4) Remove all posts

5) Exit

The current code in case 3 (which is the search option) is one of a ton I've tested, but it doesn't show any result once I try to search for a post.

Here's my code: https://pastebin.com/KJkFSnr9
C#
            while (menu)
            {
                Console.ResetColor();
 
                Console.WriteLine("\n\t[1] Write a new post");
                Console.WriteLine("\t[2] Show all your blogposts");
                Console.WriteLine("\t[3] Search for a blogpost");
                Console.WriteLine("\t[4] Remove all your blogposts");
                Console.WriteLine("\t[5] Exit Bloggen");
 
                Console.Write("\n\tChoose: ");
                int menuChoice;
                Int32.TryParse(Console.ReadLine(), out menuChoice);
 
                switch (menuChoice)
                {
                    case 1: //Write a new post
                        Console.Clear();
 
                        string[] newpost = new string[3];
                        newpost[0] = DateTime.Now.ToString("yyyy-MM-dd\tHH:mm");
                        newpost[1] = "Title: ";
                        newpost[2] = "Text: ";
 
                        Console.ForegroundColor = ConsoleColor.Yellow;
 
                        Console.WriteLine("\n\t" + newpost[0]);
 
                        Console.Write("\n\tTitle: ");
                        newpost[1] = Console.ReadLine();
 
                        Console.Write("\tText: ");
                        newpost[2] = Console.ReadLine();
 
                        Bloggen.Add(newpost);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("\n\tYour blogpost is saved!");
                        break;
 
 
                    case 2: //Show all your blogposts
                        Console.Clear();
 
                        if (post.Length > 0)
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("\n\tHBelow is all your saved blogposts:");
 
                            ShowPost(Bloggen);
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.DarkRed;
                            Console.WriteLine("\n\tYou don't have any blogpost yet!");
                        }
                        break;
 
 
                    case 3: //Search for a blogpost
                        Console.Clear();
 
                        Console.Write("\n\tWhat blogpost are you looking for? Write its title: ");
                        string key = Console.ReadLine();
 
                        for (int i = 0; i < post.Length; i++)
                        {
                            if (post[i] == key)
                            {
                                Console.WriteLine("\tYour blogpost is found!" + " " + i);
                            }
                            else
                            {
                                Console.Write("\n\tWe couldn't find the post!");
                            }
                        }
                        break;
 
 
                    case 4: //Remove all your blogposts
                        Bloggen.Clear();
 
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("\n\tAll your blogposts have been removed!");
                        break;
 
 
                    case 5://Exit Bloggen
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("\n\n\tHope you had a great time with Bloggen, {0}!", name);
 
                        Thread.Sleep(3000);
                        menu = false;
                        break;
 
 
                    default:
                        Console.ForegroundColor = ConsoleColor.DarkRed;
                        Console.WriteLine("\n\tPlease choose only between option 1 and 5!");
                        break;
                }
            }
        }
 
        // --------------------------------------------METHOD--------------------------------------------
        static void ShowPost(List<string[]> Bloggen)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            foreach (string[] post in Bloggen)
            {
                Console.WriteLine("\n\t{0}\n\t{1}\n\t{2}\n", post[0], post[1], post[2]);
            }
            Console.ResetColor();
        }
    }
}


What I have tried:

At this point, I've tried so many different codes/methods I can't remember to mention them right now. But one of the codes I've tested is included in my pasted code.
Posted
Updated 24-Aug-21 11:36am

Three comments:

(1) A crucial part of the code isn't shown: when you enter a post. you fill array newpost[] and then call Bloggen.Add(newpost); which is a black box to us.

When you search a post. you basically scan a post[] array, so we must assume that contains all the titles? If that doesn't yield a match when it should, then something is wrong in Bloggen.Add, i.e. it does not put the new title in a new post[] entry.

(2) A better approach would be to define a Post class (might also be a struct, however I prefer a class here), representing a blog post; it would obviously have properties Stamp, Title, Text; probably have a constructor with three parameters; and most likely hold a static List (not an array, but a generic List) of Post instances; furthermore a static method FindExactTitle() searching for a post by Title. That would be the OO-approach.

Note there would not be a post[] array holding copies of all the titles. Duplicate data is to be avoided: it is always a potential source of errors (loss of consistency), and makes debugging harder (when all info is present only once, a mistake would be noticed sooner).

(3) From the user point of view, searching a post should not require an exact title match (do you remember the exact title of the question you posted here?). One could give the Post class a static method to search for a list of Post instances that have a title containing some substring, yielding zero, one, or many matches depending on how precise your search term would be.
And even that search should be user-friendly, probably ignoring case, maybe ignoring punctuation, etc.

:)
 
Share this answer
 
v4
Comments
BillWoodruff 24-Aug-21 20:26pm    
+5
tgviee 25-Aug-21 5:00am    
Thank you for your help leading me to the right path in this! :)
Luc Pattyn 25-Aug-21 7:08am    
you're welcome
Getting it to run not mean your code is right! :laugh:

Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

Going "I've been reading through a ton of guides and tried different sorts of codes but none have really shown any result in my program" and just moving on to another when it doesn't work isn't a solution - you really do need to write your own and then debug it to find out why it doesn't work. Chances are the problem is something common to all your attempts.
 
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