-1

I'm currently experimenting with Stacks using C#. I've created this little testing program to push, pop and view the stack:

 Stack st = new Stack();

        Console.WriteLine("Add item to Stack");
        int inp = Convert.ToInt32(Console.ReadLine());
        st.Push(inp);

        ty:
        Console.WriteLine("");
        Console.WriteLine("A to Add");
        Console.WriteLine("D to Pop");
        Console.WriteLine("V to View");
        char act = Console.ReadKey().KeyChar;

        switch (act)
        {
            case 'A':
                Add(st);
                break;
            case 'D':
                Remove(st);
                break;
            case 'V':
                View(st);
                break;

            default:
                Console.WriteLine("Not an valid option");
                break;
        }

        goto ty;

However, when I try to enter a character for the decision part, this instantly leads it being repeated on the same line like this:

Screenshot

The question is, why does this occur; am I using Console.WriteLine incorrectly?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
szmonoloza
  • 1
  • 1
  • 1

2 Answers2

2

The answer to your immediate question is that after your Console.ReadKey() you are not printing an empty line. Therefore whatever the program writes to the Console next will be on the same line.


Having said that, this code has quite a few bad practices.

First of all, I'd get rid of the goto. A good rule of thumb as when to use goto is "never". Seriously, I doubt there will ever really be a situation where you have no option but to use goto, and if you find yourself in such a situation chances are that you've done something wrong to get there. In all honesty, you may be able to justify using it in certain situations, but at least in this case it's not really necessary. Please read more here.

Second, you don't have a way to exit your loop other than force your program to close by Ctrl+C which is not a good way to terminate. Personally, I'd refactor your code so input/display/operations are separated into their own methods, something like this:

static void Main(string[] args)
{
    Stack st = new Stack();
    Add(st);
    StackOperation(st);
}

static void StackOperation(Stack st)
{
    string act = string.Empty;
    while (act != "Q")
    {
        act = DisplayPrompt();
        switch (act)
        {
            case "A":
                Add(st);
                break;
            case "D":
                Remove(st);
                break;
            case "V":
                View(st);
                break;
            case "Q":
                break;
            default:
                Console.WriteLine("Not a valid option");
                break;
        }
    }
}

static string DisplayPrompt()
{
    Console.WriteLine();
    Console.WriteLine("A to Add");
    Console.WriteLine("D to Pop");
    Console.WriteLine("V to View");
    Console.WriteLine("Q to Quit");
    var act = Console.ReadKey().KeyChar;
    Console.WriteLine();
    return act.ToString();
}

static void Add(Stack st)
{
    Console.Write("Input item to add to stack: ");
    int inp = Convert.ToInt32(Console.ReadLine());
    st.Push(inp);
}

static void Remove(Stack st) { Console.WriteLine("Delete item from Stack"); }
static void View(Stack st) { Console.WriteLine("View Stack"); }

Obviously this is just a very rough idea of how to refactor, and you should just take this as a guide rather than the finished product.

Sach
  • 10,091
  • 8
  • 47
  • 84
  • 1
    Never is the proper frequency for "goto" for a novice. There are still a **very** few cases where it's the best answer, AFIAK there's no case where it's substantially better than other answers. I'll use it where appropriate, I don't think I've used one in the last decade. – Loren Pechtel May 31 '18 at 22:59
0

You can change the read operation to look like this:

Console.ReadKey(True)

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    You might want to explain what that does, e.g. that it will then not echo the key pressed to the console. – Dijkgraaf May 31 '18 at 22:48