How to validate data entry in C # console

0

I have a doubt in the validation of data entry in C #, for example, in an exercise of calcuclar a sum in C # console mode, as it prevents the execution from hanging and let it run when inserting letters.

For example with this code that takes the average of even and odd numbers.

int cont;
int num;
float contP = 0, acumP = 0;
float contI = 0, acumI = 0;

Console.WriteLine("Digite los numeros que quiera");
cont = int.Parse(Console.ReadLine());

for (int i = 0; i < cont; i++)
{
    Console.WriteLine("Digite el numero {0}", (i + 1));
    num = int.Parse(Console.ReadLine());

    if (num % 2 == 0) {
        contP++;
        acumP += num;
    } else {
        contI++;
        acumI += num;
    }
}

float promP = acumP / contP;
float promI = acumI / contI;

Console.WriteLine("El promedio de los numeros pares es: " + promP);
Console.WriteLine("El promedio de los numeros impares es: " + promI);

What validation do I have to do at the time the user inserts a letter saying "You use a letter" ?. I searched but it does not come out, I found it but for Form nothing more and not in console

    
asked by nixon molina 22.05.2018 в 02:23
source

2 answers

0

You already have a good idea of how to work with the console. When validating the console, to be more exact, with int.Parse ( ) you have to assign the result in a flag and your cont as an output variable.

bool EsEntero = Int32.TryParse(Console.ReadLine(), out cont);

if(!EsEntero)
    Console.WriteLine("El dato ingresado no es un entero");

Example with your code, adjust what you need

    
answered by 22.05.2018 в 02:37
0

You could loop to keep the flow until a valid integer is entered:

Console.WriteLine("Digite los numeros que quiera");
while (!Int32.TryParse(Console.ReadLine(), out cont))
{
    Console.WriteLine("El dato ingresado no es un entero");
    Console.WriteLine("Digite los numeros que quiera.");
}

And if you want to reuse the logic you can extract it to a method:

static int IngresarEntero(string mensaje)
{
    int valor;
    Console.WriteLine(mensaje);
    while (!Int32.TryParse(Console.ReadLine(), out valor))
    {
        Console.WriteLine("El dato ingresado no es un entero");
        Console.WriteLine(mensaje);
    }

    return valor;
}

and then invoke it:

static void Main(string[] args)
{
    int cont;
    int num;
    float contP = 0, acumP = 0;
    float contI = 0, acumI = 0;

    cont = IngresarEntero("Digite los numeros que quiera");

    for (int i = 0; i < cont; i++)
    {
    ...

You may want to give it an escape option.

static int IngresarEntero(string mensaje)
{
    int value = 0;
    Console.WriteLine(mensaje);
    string valor = Console.ReadLine();
    while (!Int32.TryParse(valor, out value))
    {
        Console.WriteLine("El dato ingresado no es un entero");
        Console.WriteLine($"{mensaje} o (q) para salir");
        valor = Console.ReadLine();
        if (valor.ToLower().Equals("q"))
        {
            Console.WriteLine("Programa terminado. Presione una tecla para continuar.");
            Console.Read();
            Environment.Exit(0);
        }
    }
    return value;
}

This way the program will end if the user enters a 'q'

    
answered by 30.05.2018 в 04:58