Readline does not ask for data typing

Asked

Viewed 96 times

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 have run, the console asks for the option number and I type 1, right after the two messages "type message, type key" appear without me writing message to input and goes straight to the key. What I do?

1 answer

2


This code has several technical and conceptual errors and escapes from the modern regular language of C#.

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

You can import static class and simplify code.

If you typed a letter in the menu the application would break. You have to check if the user typed something valid. I won’t even check if you typed anything other than 1 and 2, I’ll leave that to you.

This method has parameters that do not serve for nothing, I took. had variables without use.

Avoid converting to uppercase. unless you want to print in uppercase. If it’s just easy to compare and not dealing with low and high box mixed, have better techniques.

The main problem is that you used a Read()that does not do what you imagine, has to use the ReadLine() same. The Read() will only take a pressed key and return a key code, will not return what you typed. Nor will you 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
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Thank you very much. The goal to use. Toupper() was to restrict to the values of the range [65;90] of the ascii table, to try to encrypt using the Vigenere cipher.

  • @Yanpodkorytoff now you can vote on every site as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.