Why can not I read a string in the console?

1

I'm trying to read a string typed in the console, which is halfway through the code, but what happens is that it passes straight through and displays the final information.

using System;

namespace Viernes
{
    class Program
    {
        static void Main(string[] args)
        {
            int xDig = -1, yDig = -1;
            char pCardeal = 'N';
           //Verificando a validade

            while (xDig < 0 && yDig < 0)
            {
                Console.Write("Digite as coordenadas \nx: ");
                xDig = Convert.ToInt32(Console.ReadLine());

                Console.Write("y: ");
                yDig = Convert.ToInt32(Console.ReadLine());
            }
            Console.Write("Cardeal 'N' 'S' 'L' 'O': ");
            pCardeal = Convert.ToChar(Console.Read());
            pCardeal = Char.ToUpper(pCardeal);

            //Comandos
            Console.WriteLine("Digite alguns comandos, 'E'squerda, 'D'ireita, 'A'vançar.");
            string str = Console.ReadLine();

            str = str.ToUpper();
            char[] Comands = str.ToCharArray();

            //Obj
            SpaceCar sp = new SpaceCar(xDig, yDig, pCardeal);

            foreach (var separaLetras in Comands)
            {
                switch (separaLetras)
                {
                    case 'E':
                        sp.girarEsquerda();
                    break;
                    case 'D':
                        sp.girarDireita();
                    break;
                    case 'A':
                        sp.avancarBloco(xDig, yDig);
                    break;
                }
            }
            //Imprimindo Destino
            Console.Write("Coordenadas: {0}{1}{2}", sp.PosicaoX,sp.PosicaoY, sp.PosicaoCardial);
            Console.WriteLine("Press Any Key to Exit");
            Console.ReadKey();
        }
    }
}

Pastebin Code

Complement of class SpaceCar in another PasteBin .

    
asked by anonymous 23.06.2014 / 17:49

2 answers

1

Your code has some problems, but I'll just answer your question.

A minimal, complete, and verifiable example code could have been written as the example below demonstrating the solution.

using static System.Console;
using static System.Convert;

public class Program {
    public static void Main() {
        Write("Cardeal 'N' 'S' 'L' 'O': ");
        var pCardeal = char.ToUpper(ToChar(ReadKey(false).Key.ToString()));
        WriteLine($"PosicaoCardinal {pCardeal}");
    }
}

See Running on the Coding Ground . Also put it on GitHub for future reference .

The problem I could understand is that I was using the

23.06.2014 / 19:18
0

Use Console.ReadLine() and not Console.ReadKey()

    
24.06.2014 / 16:15