C#C
C#3y ago
Joriku

❔ Simple blog page (Homework)

Hi!
(Forgot to mention, this is console app)
got into a block in my head, trying a try catch phase to prevent a crash for wrong input of a blog title, now I am stuck..

on case1
Trying to:
Tell the user to write a blog title
Get the user input, store it and send it of to list above or an vector but blank in my head for the moment..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestKörning
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Simple blogg page by Anthony Persson.
            // A string list with unlimited input.
            List<string> list = new List<string>();

            // Variable for userInput with no default value.
            int userInput;

            // User selection menu, cleaner code.
            Console.WriteLine("\t[1] Add a post\n\t[2] Search for a post\n\t[3] Showcase all blogs\n\t[4] Exit");

            // Prevent a crash upon wrong userInput, non int.
            if (int.TryParse(Console.ReadLine(), out userInput))
            {
                switch(userInput)
                {
                    // Switch menu of userInput
                    case 1:
                        // Allow user to add a post
                        Console.Clear();
                        Console.WriteLine("Write the title of your blog:");
                        Thread.Sleep(1000);
                        string userBlogInput = "";
                        try
                        {
                            Console.ReadLine(userBlogInput);
                        }
                        catch
                        {
                            Console.WriteLine("Sorry, something went wrong..");
                            Thread.Sleep(2500);
                            break;
                        }
                        Console.WriteLine("Success! your title is: " + userBlogInput);
                        break;
                    case 2:
                        // Allow a user to search a post
                        break;
                    case 3:
                        // Allow a user to showcase all current existing blogs
                        break;
                    case 4:
                        // Exit
                        Console.Clear();
                        Console.WriteLine("Thank you for this time!");
                        Thread.Sleep(2500);
                        break;
                    default:
                        // Wrong input
                        break;
                }
            }
            else
            {
                Console.Clear();
                Console.WriteLine("Wrong input, please. Try again.");
                Thread.Sleep(750);
                Console.WriteLine("-------------------------------");
            }
            // Prevent shutdown, remove later
            Console.ReadLine();
        }
    }
}
Was this page helpful?