ReadLine does not ask for data entry

1
 static void Main(string[] args)
    {
        Console.WriteLine("ESCOLHA UMA OPÇÃO");
        Console.WriteLine("-----------------");
        Console.WriteLine("1-Encriptar");
        Console.WriteLine("2-Decriptar");
        Console.WriteLine("-----------------");
        int escolha = Convert.ToInt32(Console.Read());
        Boolean b;
        algoritmo("", "", true);
    }
    private static void algoritmo(String input, String chave, Boolean b)
    {
        Console.WriteLine("Digite a mensagem: ");
        input = Console.ReadLine();
        Console.WriteLine("Digite a chave: ");
        chave = Console.ReadLine();
        input = input.ToUpper();
        chave = chave.ToUpper();

When I say execute, the console asks for the option number and I type 1, then the two messages "type message, type key" appear without me writing message to input and goes right to the key. What do I do?

    
asked by anonymous 19.02.2017 / 00:49

1 answer

2

This code has several technical and conceptual bugs and it runs out of the modern C # regular language.

Prefer to use the type names that the language uses, prefer, int , string , bool .

You can import the static class and simplify the code.

If you type a letter in the menu the application would break. You have to check if the user entered something valid. I will not even check if you typed anything other than 1 and 2, I'll leave it to you.

This method has parameters that are useless, I took. had unused variables.

Avoid converting to uppercase. unless you want to print in capital letters. If it is only to facilitate the comparison and not to deal with low and high mixed box, it has better techniques.

The main problem is that you used a Read() that does not do what you think, you have to use ReadLine() itself. The Read() will only get a key pressed and return a key code, it will not return what you typed. Nor will he wait for typing. When you type 1 you are actually typing the message.

using static System.Console;

public class Program {
 public static void Main() {
        WriteLine("ESCOLHA UMA OPÇÃO");
        WriteLine("-----------------");
        WriteLine("1-Encriptar");
        WriteLine("2-Decriptar");
        WriteLine("-----------------");
        int escolha;
        if (!int.TryParse(ReadLine(), out escolha)) {
            WriteLine("Opção inválida");
            return; //dá pra fazer melhor que isso, mas agora vai simples mesmo
        }
        algoritmo();
    }
    private static void algoritmo() {
        WriteLine("Digite a mensagem: ");
        var input = ReadLine();
        WriteLine("Digite a chave: ");
        var chave = ReadLine();
        //input = input.ToUpper(); //não faça isto se não precisa mesmo, em geral não precisa
        //chave = chave.ToUpper(); //tem tecnicas melhores para facilitar para o usuário não se preocupar com caixa
    }
}

See running on .NET Fiddle . And No Coding Ground . Also put it on GitHub for future reference .

    
19.02.2017 / 01:19