Here's the code:
using System;
using manjan_in_csharp.Classes;
namespace manjan_in_csharp
{
class Program
{
static void Main()
{
Console.WriteLine("What Do You Want ? \nPress");
Console.WriteLine("'s' to use sort");
var wish =(char)Console.Read(); //explicitly converting to char (char)
// Console.WriteLine(wish.GetType());
switch (wish)
{
case 's':
Console.WriteLine("Enter the size of unsort list");
Sort sort = new Sort();
var size = int.Parse(Console.ReadLine());
sort.CallingSort(size);
break;
default:
Console.WriteLine("Invalid Operation");
break;
}
}
}
}
Now, I only enter s when the line appears "Press s to use sort" and I get this error:
I have no idea what's causing the problem. I mean, I enter s and then it doesn't let me do anything and the program crashes and I get that error.
Btw, here's the Sort class
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
namespace manjan_in_csharp.Classes
{
public class Sort
{
public void CallingSort(int size)
{
var datetimenow = DateTime.Now;
Console.WriteLine("|| Sorting Progra ||\t\t\t DATE/TIME: " + datetimenow.ToString("dd-mm-yyyy hh-mm"));
Console.WriteLine("Now Which Sort Method you want to apply");
Console.WriteLine("'b' for BubbleSort");
var method = (char)Console.Read();
switch (method)
{
case 'b':
Bubblesort(ListInitilizer(size));
break;
default:
Console.WriteLine("There is no sort related to that");
break;
}
}
public List<int> ListInitilizer(int size)
{
List<int> unsortlist = new List<int>();
Random random = new Random();
for (int i = 0; i < size; i++)
{
unsortlist.Insert(i, random.Next(0, size));
}
Console.WriteLine("Now the unsort list is :");
for (int i = 0; i < size; i++)
{
Console.Write(unsortlist[i] + " ");
}
Console.WriteLine();
return unsortlist;
}
public void Bubblesort(List<int> unsortlist)
{
var start = DateTime.Now;
for (int i = 0; i < unsortlist.Count; i++)
{
for (int j = i; j < unsortlist.Count; j++)
{
if (unsortlist[j] < unsortlist[i])
{
int temp = unsortlist[i];
unsortlist[i] = unsortlist[j];
unsortlist[j] = temp;
}
}
}
Console.WriteLine("Applying Bubblesort");
Console.WriteLine("Items in Sorted List : "+unsortlist.Count);
for (int i = 0; i < unsortlist.Count; i++)
{
Console.Write(unsortlist[i] + " ");
}
var end = DateTime.Now;
Console.WriteLine("\nTotal Duration is : " + (end - start) + " Seconds");
}
}
}