0

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:

enter image description here

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");
        }
    }
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • `wish =(char)Console.Read()` => `wish =(char)Console.ReadLine()`? – GSerg Jun 02 '20 at 16:24
  • that gives an error. Cannot convert type string to char – Abdul Mufeez Jun 02 '20 at 16:26
  • Does this answer your question? [Difference between Console.Read() and Console.ReadLine()?](https://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline) – stannius Jun 02 '20 at 16:26
  • 2
    Possible duplicate of [first Console.ReadLine() returns immediately](https://stackoverflow.com/questions/17167132/first-console-readline-returns-immediately) – GSerg Jun 02 '20 at 16:27
  • no I dont see how this answers my question – Abdul Mufeez Jun 02 '20 at 16:27
  • no it isn't a duplicate because the answer in that link that doesn't solve my issue – Abdul Mufeez Jun 02 '20 at 16:28
  • Yes it does @AbdulMufeez. That is exactly what you are doing here. You have `Read()`, and then your `ReadLine()` returns am empty string. – GSerg Jun 02 '20 at 16:29
  • no the console.readline doesnt solve the problem – Abdul Mufeez Jun 02 '20 at 16:30
  • As I said, using control.ReadLine() gives an error of cannot convert type string to char – Abdul Mufeez Jun 02 '20 at 16:30
  • Then fix that error separately, such as by doing `wish = Console.ReadLine()[0]`, or by [inserting a bogus `ReadLine()`](https://stackoverflow.com/a/17167151/11683) like suggested in the linked answer. The fact that you haven't doesn't invalidate the point of doing it. – GSerg Jun 02 '20 at 16:32
  • Thanks, man it solved the problem. btw, could you please explain why we need to write Console.ReadLine()[0]? – Abdul Mufeez Jun 02 '20 at 16:36

1 Answers1

2

The problem is with the combination of these two lines of code:

var wish =(char)Console.Read(); 
...
var size = int.Parse(Console.ReadLine());

The Console.Read() takes only single character from the input stream so for your input 's' followed by carriage-return. The carriage-return is discarded which will then be passed to next read i.e. Console.ReadLine(). You can either use ReadLine() for the first input as well or use ReadKey(). Modify your Main() as such

static void Main()
        {
            Console.WriteLine("What Do You Want ? \nPress");
            Console.WriteLine("'s' to use sort");
            var wish = Console.ReadKey().Key;        //explicitly converting to char (char)
                                                    // Console.WriteLine(wish.GetType());
            switch (wish)
            {
                case ConsoleKey.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;
            }
        }
nizoodxs
  • 141
  • 1
  • 10