Creating a blog

MartialFC

New member
Joined
Mar 8, 2021
Messages
4
Programming Experience
Beginner
Hello everyone, hope you are all doing well during these though times.

Im a beginner in C#, and im going to create a simple blog where i can add posts and titles in it.
I just want a to save my posts with titles and date, im not able to do it, can you guys help me out.

Im thankful for any help and tips that i can get.

BR
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,892
Location
Chesapeake, VA
Programming Experience
10+
We can help you out. Show us your code. Tell us where you think the problem is at. Tell us what you've already tried or thought about.
 

MartialFC

New member
Joined
Mar 8, 2021
Messages
4
Programming Experience
Beginner
I did insert my code, but it didnt accompany the post, here it comes.

C#:
 {

        public static void Menyn()
        {
            Console.WriteLine("\n\tVälkommen till din blogg!");
            Console.WriteLine("\t[1] Skriv nytt inlägg");
            Console.WriteLine("\t[2] Sök efter ett inlägg");
            Console.WriteLine("\t[3] Skriv ut alla blogginlägg");
            Console.WriteLine("\t[4] Avsluta bloggen");

        }
        



        static void Main(string[] args)
        {

            List<string[]> minBlogg = new List<string[]> { }; //Här skapar vi lista som ska innehålla strängar
            string[] post = new string[2]; //Två strängar närmare bestämt

            post[0] = "";
            post[1] = "";


            

            bool menyVisas = true;

            DateTime datum = DateTime.Now;
            Console.WriteLine("\n" + datum);


            while (menyVisas)
            {
                Menyn();

                int menyVal;
                if (int.TryParse(Console.ReadLine(), out menyVal))


                    switch (menyVal)
                    {
                        case 1:
                            Console.Clear();
                            Console.Write("Skriv din titel: \n"); //Create a titel
                            post[0] = Console.ReadLine(); //for titel
                            Console.Clear();
                            Console.Write("Skriv nytt inlägg: \n");//Create a post
                            post[1] = Console.ReadLine();
                            minBlogg.Add(post);//Save titel and post
                            
                            
                            

                            break;

                        case 2:


                            



                            
                            Console.WriteLine("Sök efter inlägg: \n"); //search after post
                            string sökOrd = Console.ReadLine(); //Search word
                            bool sök = false;
                            Console.Clear();

                            for (int i = 0; i < minBlogg.Count; i++)
                            {
                                if (post[i].ToUpper() == sökOrd.ToUpper())
                                {
                                    sök = true;
                                    Console.WriteLine("Vi hittade: " + post[0] + "\n" + post[1]);
                                    
                                }

                                if(!sök)
                                {
                                    Console.WriteLine("Vi kunde tyvärr inte hitta efter det sökta inlägget");
                                }
                            }
                            
                            
                            
                            
                            break;

                        case 3:

                            
                            
                            foreach (string[] item in minBlogg)
                            {
                                if (item == null) // Something is wrong in this loop, because if there
                                                    //isnt a post, it will show the line below
                                {
                                    Console.WriteLine("Det finns inga inlägg att skriva ut"); //I want it to display this msg, when there isnt a post
                                  
                                }

                                else
                                {
                                    Console.WriteLine("\t Det här är dina inlägg!"); //Line
                                    Console.WriteLine("\n Titel: " + item[0] + "\n Inlägg: " + item[1]);
                                    Console.WriteLine(datum);
                                }
                                
                            }

                                
                            
                                
                            

                            break;

                    }



            }

        }
    }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,892
Location
Chesapeake, VA
Programming Experience
10+
So in the original post, you said your problem was with regards to trying to save the blog titles and messages. But looking at the code you posted in post #3, you seem to having trouble trying to show a message when there are no blog titles and messages posted.

In general, if a list is empty, then the foreach loop will not even be entered. Execution of the code will jump from line 95 to line 112. So your attempt to check for an empty list is not actually doing anything. To fix this, notice that in line 71 you were referencing the Count property of the list. You can do a similar check before your foreach loop. If the list has a Count of zero, then that means the list is empty, so you can print out whatever message you want.
 

MartialFC

New member
Joined
Mar 8, 2021
Messages
4
Programming Experience
Beginner
Ok, thanks for the input, just want to make it clear for myself, do mean like this?
Also, could you help me with making sure that the messages that i want to display is getting displayed, see comments.
How can i make sure that it displays the posts that are in the blog (when there are posts) and displays the message "that there aren't any posts"




C#:
case 3: Console.Clear(); for (int i = 0; i < minBlogg.Count; i++) foreach (string[] item in minBlogg) { if (item == null) { Console.WriteLine("Det finns inga inlägg att skriva ut"); Console.ReadLine(); } else { Console.WriteLine("\t Det här är dina inlägg!"); Console.WriteLine("\n Titel: " + item[0] + "\n Inlägg: " + item[1]); Console.WriteLine(datum); } } break;"] case 3:

                            Console.Clear();
                           
                            for (int i = 0; i < minBlogg.Count; i++) //Like this??
                                                                   
                            foreach (string[] item in minBlogg)
                            {
                                if (item == null)                    //When there isnt a post in my blog, it doesnt show the msg below
                                {
                                    Console.WriteLine("Det finns inga inlägg att skriva ut"); //This
                                    Console.ReadLine();
                                 
                                }

                                else
                                {
                                    Console.WriteLine("\t Det här är dina inlägg!");
                                    Console.WriteLine("\n Titel: " + item[0] + "\n Inlägg: " + item[1]);
                                    Console.WriteLine(datum);
                                }
                               
                            }

                               
                            break;
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,892
Location
Chesapeake, VA
Programming Experience
10+
No, you would want something like this pseudo-code:
C#:
if (list.Count == 0)
{
    print "There are no blog posts."
}
else
{
    foreach(blogPost in list)
    {
        show blogPost
    }
}
 

MartialFC

New member
Joined
Mar 8, 2021
Messages
4
Programming Experience
Beginner
Hi again,

I have tried many times, but it seems that im not making any progress.
In CASE 4, i want to let a user type in the post they wanna delete from the list, but i cannot figure it out.

Can someone give me a clue or something please.

Thanks in advance





C#:
using System;
using System.Collections.Generic;

namespace Bloggen
{
    class Program
    {

        public static void Menyn()
        {
            Console.WriteLine("\n\tVälkommen till din blogg!");
            Console.WriteLine("\t[1] Skriv nytt inlägg");
            Console.WriteLine("\t[2] Sök efter ett inlägg");
            Console.WriteLine("\t[3] Skriv ut alla blogginlägg");
            // Console.WriteLine("\t[4] Ta bort ett inlägg ");
            Console.WriteLine("\t[4] Ta bort inlägg");

            Console.WriteLine("\t[5] Avsluta bloggen");
        }


        public static List<string[]> minBlogg = new List<string[]> { }; //Skapar lista med strängar

        public static void Meny1()
        {
            Console.Clear();
            Console.Write("Skriv din titel: \n");
            string[] innehåll = new string[3];
            DateTime datum = DateTime.Now;
            innehåll = new string[3];
            innehåll[0] = Console.ReadLine(); //För titeln
            Console.Clear();
            Console.Write("Skriv inlägg: \n");
            innehåll[1] = Console.ReadLine(); //För inlägg
            datum = DateTime.Now;
            innehåll[2] = datum.ToString(); //Element 3 ska innehålla datum, då inlägg skapades

            minBlogg.Add(innehåll);

        }




        static void Main(string[] args)
        {
            //List<string[]> minBlogg = new List<string[]> { }; //Skapar lista med strängar












            bool menyVisas = true;







            while (menyVisas)
            {
                Menyn();

                int menyVal;
                if (int.TryParse(Console.ReadLine(), out menyVal))



                    switch (menyVal)
                    {
                        case 1:

                            Meny1(); //Vi anropar vår metod som innehåller Case 1







                            Console.Clear();



                            break;

                        case 2:





                            //Möjligheten att söka med hjälp linjärsökning
                            Console.Clear();
                            Console.WriteLine("Ange titel: \n");
                            string sökOrd = Console.ReadLine();
                            Console.Clear();

                            bool sök = false;





                            for (int i = 0; i < minBlogg.Count; i++)


                            {

                                if (minBlogg[i][0].ToUpper() == sökOrd.ToUpper())


                                {



                                    Console.WriteLine("Vi hittade " + "\nTitel: " + minBlogg[i][0] + "\nInlägg: " + minBlogg[i][1] + "\nDatum: " + minBlogg[i][2]); //Här skriver ut elementen som finns sparade i våra index
                                    sök = true;

                                }



                            }



                            if (sök != true)
                            {
                                Console.WriteLine("Vi kunde tyvärr inte hitta efter det sökta inlägget");

                            }

                            break;





                        case 3:                                                 //Här visas inlägg som har lagts till

                            Console.Clear();






                            if (minBlogg.Count == 0)
                            {
                                Console.WriteLine("Det finns inga inlägg att skriva ut");    //Ifall inga inlägg finns


                            }

                            else

                            {
                                //Annars detta...

                                foreach (string[] item in minBlogg)
                                {

                                    Console.WriteLine("\t Det här är dina inlägg!\n");

                                    Console.WriteLine("\n Titel: " + item[0] + "\n Inlägg: " + item[1] + "\n Datum: " + item[2]);




                                }

                            }





                            break;




                        case 4:                                //I wanna take in an input from user
                            Console.Clear();                // and delete the post they wanna delete in the list       
                            Console.WriteLine("Ange index \n");   
                            string inmattning = Console.ReadLine();
                            Console.Clear();
                            int index = Convert.ToInt32(inmattning);



                            for (int i = 0; i < minBlogg.Count; i++)
                            {

                                if (minBlogg[i] != null)
                                {
                                    
                                    
                                }
                            }
                                    


                                

                            

                            

                            for (int i = 0; i < minBlogg.Count; i++)
                            {
                                Console.WriteLine(minBlogg[i][0]);
                            }
                            Console.WriteLine("Titel borttagen");

                            break;





                        case 5:
                            //Avlsuta menyn
                            menyVisas = false;
                            Console.Clear();

                            break;

                        default:

                            Console.WriteLine("Välj mellan menyval 1-5");

                            Console.ReadLine();

                            break;



                    }



            }

        }
    }
}
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,892
Location
Chesapeake, VA
Programming Experience
10+
Top Bottom