C
C#2y 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();
}
}
}
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();
}
}
}
122 Replies
Haze.
Haze.2y ago
isnt it supposed to be userBlogInput = Console.ReadLine() instead of Console.ReadLine(userBlogInput)?
Haze.
Haze.2y ago
Joriku
JorikuOP2y ago
Thank you, tried a little with this. Getting an issue that it goes onto catches, not saving the title into index 0
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.
// List for blog inputs
List<string> myBlog = 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))
{
bool isRunning = true;
while(isRunning)
{
switch (userInput)
{
// Switch menu of userInput
case 1:
// Allow user to add a post
Console.Clear();
Thread.Sleep(1000);
try
{
for (int i = 0; i < 1;)
{
// Name the blogpage, save in list on idex 0
Console.WriteLine("Write the title of your blog:");
myBlog[i] = Console.ReadLine();
Console.WriteLine("-----------------");
Console.WriteLine("Title has been saved on index: " + i); // For test purposes, leaving index
Console.WriteLine("-----------------");
}
}
catch
{
Console.WriteLine("Sorry, something went wrong..");
Thread.Sleep(2500);
Console.Clear();
}
Console.WriteLine("Success! your title is: " + myBlog[0]);
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();
}
}
}
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.
// List for blog inputs
List<string> myBlog = 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))
{
bool isRunning = true;
while(isRunning)
{
switch (userInput)
{
// Switch menu of userInput
case 1:
// Allow user to add a post
Console.Clear();
Thread.Sleep(1000);
try
{
for (int i = 0; i < 1;)
{
// Name the blogpage, save in list on idex 0
Console.WriteLine("Write the title of your blog:");
myBlog[i] = Console.ReadLine();
Console.WriteLine("-----------------");
Console.WriteLine("Title has been saved on index: " + i); // For test purposes, leaving index
Console.WriteLine("-----------------");
}
}
catch
{
Console.WriteLine("Sorry, something went wrong..");
Thread.Sleep(2500);
Console.Clear();
}
Console.WriteLine("Success! your title is: " + myBlog[0]);
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();
}
}
}
Haze.
Haze.2y ago
that could be because the list myBlog doesnt actually have a value at index 0
Haze.
Haze.2y ago
Haze.
Haze.2y ago
change myBlog[i] = Console.ReadLine(); to myBlog.Add(Console.ReadLine()); and also you probably want for (int i = 0; i < 1; i++) instead of for (int i = 0; i < 1;) and also isRunning is always true so that means you will only be able to enter the same case you initially entered
Joriku
JorikuOP2y ago
I moved it up, then would you recommend using a for loop for this if I want to add more posts without overwriting the old one? Changed it to this instead of a for loop
case 1:
// Allow user to add a post
Console.Clear();
Thread.Sleep(1000);
string[] blogTitle = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogTitle[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogTitle[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogTitle[0] + ". Text: " + blogTitle[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogTitle[0] + " and your blog text is: " + blogTitle[1]);
break;
case 1:
// Allow user to add a post
Console.Clear();
Thread.Sleep(1000);
string[] blogTitle = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogTitle[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogTitle[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogTitle[0] + ". Text: " + blogTitle[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogTitle[0] + " and your blog text is: " + blogTitle[1]);
break;
This allowed me to store the values on index positions + changed the list to this:
// String vector list
List<string[]> myBlogVectorList = new List<string[]>();
// String vector list
List<string[]> myBlogVectorList = new List<string[]>();
Haze.
Haze.2y ago
looks like it should work although just to nitpick, i would probably change blogTitle to something like blogPost
Joriku
JorikuOP2y ago
Then I saw that you mentioned .add, so using: myBlogVectorList.Add(blogTitle);
Haze.
Haze.2y ago
yeah since the list initially doesnt have any values youll have to do that
Joriku
JorikuOP2y ago
Quick question regarding the .add, do I need it right after the user's input or only at the end once? Will it then add both inputs if I use it at the end?
Pobiega
Pobiega2y ago
Ah. Now I know why this felt so similar. Is this an NTI assignment?
Haze.
Haze.2y ago
oh i forgot this was homework shouldve structured it as learning
Pobiega
Pobiega2y ago
and you are supposed to implement a binary search later on?
Joriku
JorikuOP2y ago
Yes!
Pobiega
Pobiega2y ago
Yeah, we had a fellow student of yours ask for help with this very assignment a week or two ago.
Haze.
Haze.2y ago
oh please dont make me remember that
Joriku
JorikuOP2y ago
Well, not sure. I find it very helpful. You just told me to change the title due to the name I chose. Made me think of why and keep in mind to name them more appealing Would love to see that post, if you'd know where to find it
Haze.
Haze.2y ago
probably archived now
Haze.
Haze.2y ago
ah yes
Pobiega
Pobiega2y ago
but honestly, don't spend too much time digging in them
Haze.
Haze.2y ago
yeah id recommend trying to implement it yourself then if it doesnt work, ask
Pobiega
Pobiega2y ago
your program has a few issues as is already 🙂
// Prevent a crash upon wrong userInput, non int.
if (int.TryParse(Console.ReadLine(), out userInput))
{
bool isRunning = true;
while(isRunning)
{
// Prevent a crash upon wrong userInput, non int.
if (int.TryParse(Console.ReadLine(), out userInput))
{
bool isRunning = true;
while(isRunning)
{
this strikes me as the wrong order of things you parse the input, once, and if it was valid, you enter a loop? usually you have a loop surrounding your read, so that a failed read is "retried" so to speak but bravo for int.TryParse(Console.ReadLine(), out userInput), thats crispy clean
Joriku
JorikuOP2y ago
My bad, It's updated. Noticed it while testing it out
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.
// String vector list
List<string[]> myBlogVectorList = new List<string[]>();

// Variable for userInput with no default value.
int userInput;
bool isRunning = true;

while (isRunning)
{
// 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();
Thread.Sleep(1000);
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
myBlogVectorList.Add(blogPost);
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
Console.WriteLine("Test: " + myBlogVectorList[0]); // Test to check if saved to vectorList, issue
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);
isRunning = false;
break;
default:
// Wrong input
Console.WriteLine("Wrong Input.");
break;
}
}
else
{
Console.Clear();
Console.WriteLine("Wrong input, please. Try again.");
Thread.Sleep(750);
Console.WriteLine("-------------------------------");
}
}
}
}
}
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.
// String vector list
List<string[]> myBlogVectorList = new List<string[]>();

// Variable for userInput with no default value.
int userInput;
bool isRunning = true;

while (isRunning)
{
// 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();
Thread.Sleep(1000);
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
myBlogVectorList.Add(blogPost);
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
Console.WriteLine("Test: " + myBlogVectorList[0]); // Test to check if saved to vectorList, issue
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);
isRunning = false;
break;
default:
// Wrong input
Console.WriteLine("Wrong Input.");
break;
}
}
else
{
Console.Clear();
Console.WriteLine("Wrong input, please. Try again.");
Thread.Sleep(750);
Console.WriteLine("-------------------------------");
}
}
}
}
}
(Prob should have cut this down)..
Pobiega
Pobiega2y ago
Have you covered methods yet?
Joriku
JorikuOP2y ago
Only a little, I played around with them yesterday after watching: C# Tutorial - Full Course for Beginners by: freeCodeCamp.org
Pobiega
Pobiega2y ago
its fine if not, but this code would be so much nicer if each of your cases were split up a bit, as this will get nasty when you implement case 2 and 3 otherwise
Haze.
Haze.2y ago
its worth learning at least the basics for it takes a lot of code out of main
Joriku
JorikuOP2y ago
Alright, give me a few. Will try it out Alright, stuck
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 string saveBlogPost(string saveBlog)
{
// Test method
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
return saveBlog;
}

static void Main(string[] args)
{
// Simple blogg page by Anthony Persson.
// String vector list
List<string[]> myBlogVectorList = new List<string[]>();

// Variable for userInput with no default value.
int userInput;
bool isRunning = true;

while (isRunning)
{
// 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();
Thread.Sleep(1000);
saveBlogPost(Console.ReadLine());
myBlogVectorList.Add(saveBlogPost); // Uhm??
Console.WriteLine("Test: " + myBlogVectorList[0]); // Test to check if saved to vectorList, issue
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);
isRunning = false;
break;
default:
// Wrong input
Console.WriteLine("Wrong Input.");
break;
}
}
else
{
Console.Clear();
Console.WriteLine("Wrong input, please. Try again.");
Thread.Sleep(750);
Console.WriteLine("-------------------------------");
}
}
}
}
}
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 string saveBlogPost(string saveBlog)
{
// Test method
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
return saveBlog;
}

static void Main(string[] args)
{
// Simple blogg page by Anthony Persson.
// String vector list
List<string[]> myBlogVectorList = new List<string[]>();

// Variable for userInput with no default value.
int userInput;
bool isRunning = true;

while (isRunning)
{
// 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();
Thread.Sleep(1000);
saveBlogPost(Console.ReadLine());
myBlogVectorList.Add(saveBlogPost); // Uhm??
Console.WriteLine("Test: " + myBlogVectorList[0]); // Test to check if saved to vectorList, issue
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);
isRunning = false;
break;
default:
// Wrong input
Console.WriteLine("Wrong Input.");
break;
}
}
else
{
Console.Clear();
Console.WriteLine("Wrong input, please. Try again.");
Thread.Sleep(750);
Console.WriteLine("-------------------------------");
}
}
}
}
}
Pobiega
Pobiega2y ago
Okay so lets look only on your save method first
static string saveBlogPost(string saveBlog)
{
// Test method
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
return saveBlog;
}
static string saveBlogPost(string saveBlog)
{
// Test method
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
return saveBlog;
}
this method takes a string in, that is... never interacted with at all, and then returned you do however build a string[] blogPost that has values added to it, and then left to die alone So my questions are as follows: 1. what is the purpose of string saveBlog? 2. is saveBlog the correct return value here?
Joriku
JorikuOP2y ago
1. To use the return statement, I think I've misunderstood that. Changed it to this:
static string[] saveBlogPost()
{
// Test method
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
return blogPost;
}
static string[] saveBlogPost()
{
// Test method
string[] blogPost = new string[2];
Thread.Sleep(1000);
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine();
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine();
Console.WriteLine("Blog title: " + blogPost[0] + ". Text: " + blogPost[1]);
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]);
return blogPost;
}
Since we're trying to return blogPost's values/indexes 2. No, blogPost is the return we want
Pobiega
Pobiega2y ago
much better!
Joriku
JorikuOP2y ago
Not going to lie and take it as a W, this was a quick solution from Visual studio which helped me realize that I misunderstood the explanation of how to use a return statement! Good progress though! Alright, the statement should be alright. Changed into this, this should allow us to enter the method and prompt from there to then return once finished.
saveBlogPost();
saveBlogPost();
Then, how would I use the add function to add it into the vectorList?
Pobiega
Pobiega2y ago
show me your entire case 1: now
Joriku
JorikuOP2y ago
case 1:
// Allow user to add a post
Console.Clear();
Thread.Sleep(1000);
saveBlogPost();
myBlogVectorList.Add(); // Uhm??
Console.WriteLine("Test: " + myBlogVectorList[0]); // Test to check if saved to vectorList, issue
break;
case 1:
// Allow user to add a post
Console.Clear();
Thread.Sleep(1000);
saveBlogPost();
myBlogVectorList.Add(); // Uhm??
Console.WriteLine("Test: " + myBlogVectorList[0]); // Test to check if saved to vectorList, issue
break;
Pobiega
Pobiega2y ago
saveBlogPost returns something we need to use that returned value
Joriku
JorikuOP2y ago
It returns blogPost, right?
Pobiega
Pobiega2y ago
it returns a string[] thats all you know at that point but you are not using the return value you are discarding it, throwing it away
Joriku
JorikuOP2y ago
Alright So, then to grab that value. I'd simply have to:
myBlogVectorList.Add(saveBlogPost());
myBlogVectorList.Add(saveBlogPost());
Pobiega
Pobiega2y ago
yup
Joriku
JorikuOP2y ago
Not sure, it seems that it takes me into the method once more
Joriku
JorikuOP2y ago
Joriku
JorikuOP2y ago
Joriku
JorikuOP2y ago
Right, just remove the upper one and keep it as .add(saveBlogPost()); This should add the values, right?
Pobiega
Pobiega2y ago
yup
Joriku
JorikuOP2y ago
If i'd like to take the value on index 0 out just to check if it's stored, how would I do that?
Pobiega
Pobiega2y ago
myBlogVectorList[0] would give you an array
string[] blogPost = myBlogVectorList[0];
Console.WriteLine(blogPost[0]);
string[] blogPost = myBlogVectorList[0];
Console.WriteLine(blogPost[0]);
you need to then index that array too
Joriku
JorikuOP2y ago
Alright, why is this needed?
string[] blogPost = myBlogVectorList[0];
string[] blogPost = myBlogVectorList[0];
Pobiega
Pobiega2y ago
its not, strictly speaking
Haze.
Haze.2y ago
its to make it easier to read
Pobiega
Pobiega2y ago
if you just want the blog title of the first post, myBlogVectorList[0][0] works but its not very nice to read
Haze.
Haze.2y ago
and then if you want the text as well youd have myBlogVectorList[0][1]
Joriku
JorikuOP2y ago
Running that gives the last text only,
Pobiega
Pobiega2y ago
ofc thats literally what haze said the first index is accessing the List, the second index is for accessing what is stored there (the string[])
Haze.
Haze.2y ago
i suppose i couldve worded it better
Joriku
JorikuOP2y ago
Oh!
Haze.
Haze.2y ago
if you wanted only the text youd have myBlogVectorList[0][1]
Joriku
JorikuOP2y ago
Keeping the string inside, it does make it easier to harold
Pobiega
Pobiega2y ago
yup
Joriku
JorikuOP2y ago
Case 3
case 3:
// Allow a user to showcase all current existing blogs
bool isEmpty = !myBlogVectorList.Any();
if (isEmpty)
{
// If vectorList is empty
Console.Clear();
Console.WriteLine("There are currently no blogs.");
Console.WriteLine("-----------------------------");
}
else
{
Console.WriteLine("Current blog(s) existing:");
Console.WriteLine("-------------------------");
foreach (string[] item in myBlogVectorList)
{
Console.WriteLine("Title: " + item[0] + " / Text: " + item[1]);
Console.WriteLine("-------------------------");
}
}
break;
case 3:
// Allow a user to showcase all current existing blogs
bool isEmpty = !myBlogVectorList.Any();
if (isEmpty)
{
// If vectorList is empty
Console.Clear();
Console.WriteLine("There are currently no blogs.");
Console.WriteLine("-----------------------------");
}
else
{
Console.WriteLine("Current blog(s) existing:");
Console.WriteLine("-------------------------");
foreach (string[] item in myBlogVectorList)
{
Console.WriteLine("Title: " + item[0] + " / Text: " + item[1]);
Console.WriteLine("-------------------------");
}
}
break;
Haze.
Haze.2y ago
i dont see the need for an extra variable here, but it doesnt really matter also another really nitpicky thing would be to possible rename item to blog, since you are looping through blogs but that really doesnt matter
Joriku
JorikuOP2y ago
Thanks a lot so far, it's extremely helpful!
The king of kings
That's right. Luckily, I completed the project, submitted it 😆 @Jorikuit seems like you're taking the same course as I do or apparently different course, but same assignment!
Joriku
JorikuOP2y ago
Oh no way, hope you getting good grades! I got some extra assignments to do for it for an A but don't think time exists for it. Got case 2 legt
The king of kings
Well! My grades looks good so far. I got some assignments too that I'm working on currently and they're getting harder and harder.
Joriku
JorikuOP2y ago
Yeah, so far only had A's up till last assignment where I started to struggle. They're getting extreme harold
The king of kings
Ok! Same thing happened with me too. It's like playing a game and the level gets harder. Are you from Brazilian?
Joriku
JorikuOP2y ago
Yeah, short explained. It just takes up your skill level animesoft I am Swedish, prob shouldn't keep this help section into a conversation Laugh Will return with Case 2 in a little
The king of kings
Aha! Ok! I'll dm you then.
Joriku
JorikuOP2y ago
Alright, stuck again
case 2:
// Allow a user to search a post
Console.WriteLine("Enter the name of a post:");
string[] searchWord = Console.ReadLine();

if (!myBlogVectorList.Any()) // If vectorList .any(element) is false.
{
// If vectorList is empty
Console.Clear();
Console.WriteLine("There are currently no blogs.");
Console.WriteLine("-----------------------------");
} else {
Console.WriteLine("Current blog existing:");
Console.WriteLine("-------------------------");
for (int i = 0; i < myBlogVectorList.Count; i++)
{
if(searchWord == myBlogVectorList[i])
{
Console.WriteLine("Exists on idenx: " + myBlogVectorList[0]);
//foreach (string[] blog in myBlogVectorList) // For each, string[in list] blog in vectorList
//{
// Console.WriteLine("Title: " + blog[0] + " / Text: " + blog[1]);
// Console.WriteLine("-------------------------");
//}
}
}
}
break;
case 2:
// Allow a user to search a post
Console.WriteLine("Enter the name of a post:");
string[] searchWord = Console.ReadLine();

if (!myBlogVectorList.Any()) // If vectorList .any(element) is false.
{
// If vectorList is empty
Console.Clear();
Console.WriteLine("There are currently no blogs.");
Console.WriteLine("-----------------------------");
} else {
Console.WriteLine("Current blog existing:");
Console.WriteLine("-------------------------");
for (int i = 0; i < myBlogVectorList.Count; i++)
{
if(searchWord == myBlogVectorList[i])
{
Console.WriteLine("Exists on idenx: " + myBlogVectorList[0]);
//foreach (string[] blog in myBlogVectorList) // For each, string[in list] blog in vectorList
//{
// Console.WriteLine("Title: " + blog[0] + " / Text: " + blog[1]);
// Console.WriteLine("-------------------------");
//}
}
}
}
break;
Joriku
JorikuOP2y ago
Using:
string[] searchWord = Console.ReadLine();
string[] searchWord = Console.ReadLine();
Haze.
Haze.2y ago
Well yes What does Console.ReadLine() return?
Joriku
JorikuOP2y ago
The user input as a string, sorry. Went to gym and took some food.
Haze.
Haze.2y ago
and what are you trying to store it as?
Joriku
JorikuOP2y ago
I think I am trying to store it as the same as the vectorList..
Haze.
Haze.2y ago
so you are trying to store a string as a string array?
Joriku
JorikuOP2y ago
Trying to compare it, is what I am after at least
Haze.
Haze.2y ago
this line string[] searchWord = Console.ReadLine(); what is it trying to store in searchWord?
Joriku
JorikuOP2y ago
a string list
Haze.
Haze.2y ago
but what does Console.ReadLine() return?
Joriku
JorikuOP2y ago
a simple string
Haze.
Haze.2y ago
there you go then you cannot store a string as a string array
Joriku
JorikuOP2y ago
Then what options do I have here?
Haze.
Haze.2y ago
well why are you trying to store it as a string array? nowhere in your code uses indexes of the variable
Joriku
JorikuOP2y ago
Here's where my head collapsed
Haze.
Haze.2y ago
what does string[] mean to you?
Joriku
JorikuOP2y ago
a list of strings, a place where you can store multiple strings
Haze.
Haze.2y ago
ok, but Console.ReadLine() returns only one string so how can you declare a variable as a list (it's actually an array) when you are only passing in a singular string variable
Joriku
JorikuOP2y ago
This is where my head went into spinning, not sure what I am harold
Haze.
Haze.2y ago
I'm trying to get you to understand what you are actually trying to do So first off, string[] is a string array, not a list, if you do not know the difference I highly recommend you do some research Secondly, reread the error message you receive when trying to run the code and really think about what it means Thirdly, look through your current code and see if you really are using whatever data type you think you are
Joriku
JorikuOP2y ago
This
List<string[]> myBlogVectorList = new List<string[]>();
List<string[]> myBlogVectorList = new List<string[]>();
This is a list, right? A list of strings with no set sequential And an array, set sequentials
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
Or am I misunderstanding it completely
Haze.
Haze.2y ago
Yes, you are correct
Joriku
JorikuOP2y ago
Then if you only use string[], it's an array And now, it's looking a little bit better
case 2:
// Allow a user to search a post
// Allow a user to search a post
Console.Clear();
Console.WriteLine("Enter the name of a post:");
string searchWord = Console.ReadLine();

if (!myBlogVectorList.Any()) // If vectorList .any(element) is false.
{
// If vectorList is empty
Console.Clear();
Console.WriteLine("There are currently no blogs.");
Console.WriteLine("-----------------------------");
}
else
{
for (int i = 0; i < myBlogVectorList.Count; i++)
{
if (searchWord.ToLower() == myBlogVectorList[i][i].ToLower())
{
Console.WriteLine("Current blog existing:");
Console.WriteLine("-------------------------");
Console.WriteLine("Exists on index: " + myBlogVectorList[i][i]);
}
else
{
Console.WriteLine("Blog post does not exist.");
}
}
}
break;
case 2:
// Allow a user to search a post
// Allow a user to search a post
Console.Clear();
Console.WriteLine("Enter the name of a post:");
string searchWord = Console.ReadLine();

if (!myBlogVectorList.Any()) // If vectorList .any(element) is false.
{
// If vectorList is empty
Console.Clear();
Console.WriteLine("There are currently no blogs.");
Console.WriteLine("-----------------------------");
}
else
{
for (int i = 0; i < myBlogVectorList.Count; i++)
{
if (searchWord.ToLower() == myBlogVectorList[i][i].ToLower())
{
Console.WriteLine("Current blog existing:");
Console.WriteLine("-------------------------");
Console.WriteLine("Exists on index: " + myBlogVectorList[i][i]);
}
else
{
Console.WriteLine("Blog post does not exist.");
}
}
}
break;
One question about this, though. While using the double [i]'s, why are what happens here? It has to do with my list, right?
Haze.
Haze.2y ago
You are firstly indexing your list, which returns an array, then you are indexing that array with the second [i] Although I don't know why you are using [i][i] instead of [i][0] since aren't you just trying to search the titles of the blogs?
Joriku
JorikuOP2y ago
So, the second i basically takes the value out of the array? Right, since using 0 is the first Index. Thank you! That just solved my crashes using 0 and 1 instead of i i with i++
for (int i = 0; i < myBlogVectorList.Count; i++)
{
if (searchWord.ToLower() == myBlogVectorList[i][0].ToLower())
{
// If post exists, print out title and text.
Console.Clear();
Console.WriteLine("Current blog is existing:");
Console.WriteLine("-------------------------");
Console.WriteLine("Blog title: " + myBlogVectorList[i][0] + " / Text: " + myBlogVectorList[i][1]);
Console.WriteLine("-------------------------");
Thread.Sleep(2500);
} else {
Console.Clear();
Console.WriteLine("Blog post does not exist.");
}
}
for (int i = 0; i < myBlogVectorList.Count; i++)
{
if (searchWord.ToLower() == myBlogVectorList[i][0].ToLower())
{
// If post exists, print out title and text.
Console.Clear();
Console.WriteLine("Current blog is existing:");
Console.WriteLine("-------------------------");
Console.WriteLine("Blog title: " + myBlogVectorList[i][0] + " / Text: " + myBlogVectorList[i][1]);
Console.WriteLine("-------------------------");
Thread.Sleep(2500);
} else {
Console.Clear();
Console.WriteLine("Blog post does not exist.");
}
}
Console.WriteLine("Blog title: " + myBlogVectorList[i][0] + " / Text: " + myBlogVectorList[i][1]); // [i][i] and [i++][i++] - Gave out only first title and text, but resulted in a crash on next search
Console.WriteLine("Blog title: " + myBlogVectorList[i][0] + " / Text: " + myBlogVectorList[i][1]); // [i][i] and [i++][i++] - Gave out only first title and text, but resulted in a crash on next search
Haze.
Haze.2y ago
So is it all sorted now?
Joriku
JorikuOP2y ago
Yeah, it's all sorted for now. Thank you!
Haze.
Haze.2y ago
great, and i hope you learned something
Joriku
JorikuOP2y ago
Alright, it's not done
Joriku
JorikuOP2y ago
Alright, I can't figure this out.. So, this is what I have so far. Trying to give alternative options based on the first character of user input, if no post exists with the input of the user
Joriku
JorikuOP2y ago
Alright, back for some feedback and checks So far, I updated the code to this
case 2:
// Allow a user to search for stored blog post.
Console.Write("Enter a search word:\n");
string searchWord = Console.ReadLine(); // Creates variable for searchWord
bool search = false; // If search is true or false

// blog instead of item
foreach (string[] blog in myBlogVectorList) // loops through vectorList to find the searchword
{
if (blog[0].IndexOf(searchWord, StringComparison.OrdinalIgnoreCase) >= 0 // StringComparison.OrdinalIgnoreCase, ignores using toUpper or toLower. Words treated as equals.
|| blog[1].IndexOf(searchWord, StringComparison.OrdinalIgnoreCase) >= 0) // || - if true and true, if both are true.
{
// searches for wordmatch in both title and text
Console.WriteLine("Title: " + blog[0] + "\nText: " + blog[1] + "\n-------------------------"); // Writes out title and text with a seperation
search = true;
}

}
if (!search) // if searchword does not exist in vectorList
{
Console.WriteLine("Your search has failed!" + "\nNo blog exists with either character: " + searchWord.Substring(0));
}
break;
case 2:
// Allow a user to search for stored blog post.
Console.Write("Enter a search word:\n");
string searchWord = Console.ReadLine(); // Creates variable for searchWord
bool search = false; // If search is true or false

// blog instead of item
foreach (string[] blog in myBlogVectorList) // loops through vectorList to find the searchword
{
if (blog[0].IndexOf(searchWord, StringComparison.OrdinalIgnoreCase) >= 0 // StringComparison.OrdinalIgnoreCase, ignores using toUpper or toLower. Words treated as equals.
|| blog[1].IndexOf(searchWord, StringComparison.OrdinalIgnoreCase) >= 0) // || - if true and true, if both are true.
{
// searches for wordmatch in both title and text
Console.WriteLine("Title: " + blog[0] + "\nText: " + blog[1] + "\n-------------------------"); // Writes out title and text with a seperation
search = true;
}

}
if (!search) // if searchword does not exist in vectorList
{
Console.WriteLine("Your search has failed!" + "\nNo blog exists with either character: " + searchWord.Substring(0));
}
break;
Questions: What can go wrong here, and is this acceptable as a linear search method? Then, what I think this does to check If I am correct in most parts: It takes the users input, searches loops through the vector list to find the search word. Ignores the ordinal case rule skipping need of toLower/Upper. Checks if both [0, 1] index are true and searches for a word match. And if it doesn't exist, performs code below and informs that no character in the user's search matches a title nor text
Pobiega
Pobiega2y ago
// If search is true or false
// Creates variable for searchWord
// If search is true or false
// Creates variable for searchWord
comments like these are just useless noise. Unless your teachers force you to write them, remove them. and yes, looks okay for a linear search
Joriku
JorikuOP2y ago
Update: Added dates
static string[] saveBlogPost()
{
// Taking care of the add post function, returning with blogPost which can be used in main function.
// Set sequential for 2 temp strings, adds to list inside main function later.
string[] blogPost = new string[3];
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine(); // Adds userInput to index 0
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine(); // Adds userInput to index 1
blogPost[2] = DateTime.Now.ToString(); // Adds current time string to index 2
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]); // Prints out set title and text
Console.WriteLine("Your blog is now saved!");
return blogPost;
}
static string[] saveBlogPost()
{
// Taking care of the add post function, returning with blogPost which can be used in main function.
// Set sequential for 2 temp strings, adds to list inside main function later.
string[] blogPost = new string[3];
Console.WriteLine("Enter a blog title:");
blogPost[0] = Console.ReadLine(); // Adds userInput to index 0
Thread.Sleep(1000);
Console.WriteLine("Enter the text for your blog post:");
blogPost[1] = Console.ReadLine(); // Adds userInput to index 1
blogPost[2] = DateTime.Now.ToString(); // Adds current time string to index 2
Console.Clear();
Console.WriteLine("Success! your title is: " + blogPost[0] + " and your blog text is: " + blogPost[1]); // Prints out set title and text
Console.WriteLine("Your blog is now saved!");
return blogPost;
}
And printing out as blogPost[2]
Haze.
Haze.2y ago
Is it just bad mobile formatting or am I seeing comments inside an if statement?
Joriku
JorikuOP2y ago
Uhm, caugh, caugh. Just bad mobile harold let's see. How would I do to print out all existing blogs. Give them a number by taking 3 indexes for each as 1 then the next 3 indexes for number 2. So Create an ID for the stories? This is to be able to remove a post by index 0 (title) text (index 1) and it's date (index 2) for each an every post. A simple remove function. Then this will come in handy while allowing to edit that specific title or text aswell. Then just update the edit date by .new
Joriku
JorikuOP2y ago
Posting the whole code since a lot has changed since last time.
Joriku
JorikuOP2y ago
Currently:
// Remove a post
int removePostUserInput;
Console.Clear();
Console.WriteLine("Which post do you want to remove?");
// Print out all available posts as an option to select post.
for (int i = 0; i < myBlogVectorList.Count; i++)
{
Console.WriteLine("ID " + (i + 1) + ": " + myBlogVectorList[i][0]);
}
if (int.TryParse(Console.ReadLine(), out removePostUserInput))
{
// Remove 3 indexes based on ID
} else {
Console.WriteLine("Wrong input.");
}
// Remove a post
int removePostUserInput;
Console.Clear();
Console.WriteLine("Which post do you want to remove?");
// Print out all available posts as an option to select post.
for (int i = 0; i < myBlogVectorList.Count; i++)
{
Console.WriteLine("ID " + (i + 1) + ": " + myBlogVectorList[i][0]);
}
if (int.TryParse(Console.ReadLine(), out removePostUserInput))
{
// Remove 3 indexes based on ID
} else {
Console.WriteLine("Wrong input.");
}
And I am done, finished up this task. I am not going for that A. Screw it, too complex xD Thank you both for your help, it's been extremely useful and took hours extra of my already 4 days into this project. Leaving the code public here, in case any one wants to look through it or needs it in the future.
Haze.
Haze.2y ago
How much longer do you have until it's due?
Pobiega
Pobiega2y ago
I'm guessing it was due 23:59 and they posted it at 23:48 😛
Haze.
Haze.2y ago
If they had more time I'd fully recommend at least trying to complete the other parts, since there's no real harm in doing so
Joriku
JorikuOP2y ago
Sorry, didn't see this. It's turned in, had till yesterday 23.59
Haze.
Haze.2y ago
ah okay
Joriku
JorikuOP2y ago
Yeah, I took 4 out of 6 extra parts. Sadly, had not time for the other two so I can't get an A, but at least a C/B
Haze.
Haze.2y ago
even if it doesnt mean anything, id still recommend you at least try the extra tasks
Joriku
JorikuOP2y ago
Yeah, starting a new project to finish up things and get some more repitation for the grounds
Haze.
Haze.2y ago
as long as you dont get into the habit of not doing stuff because its difficult you should be fine for the most part
Joriku
JorikuOP2y ago
Got the finals in 2 days only So, the things left was: - Binear search - A sorting system So, those are the things I'll be working on in the new project
Haze.
Haze.2y ago
thats good
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server