stays in infinite cycle and can not see why

-1

This is the code:

while (op != 0) {
    op = 0;
    scanf_s("%i", &op);
    system("cls");
    printf("\n\n");


    switch (op) {
    case 1:
        printf("Ingrese el numero de cedula del cliente y presione enter: \n");
        scanf_s("%i", &numCedula);
        if (numCedula <= 99999999 && numCedula > 0) {
            invertirNumero(numCedula);
            resultDigito = sumaDigitos(numCedula);
            printf("La suma de los digitos es: %i \n", resultDigito);
            i++;
            arreglo[i] = resultDigito;
            break;
        }
        else {
            printf("el numero de cedula no puede exceder los 8 digitos \n tiene que ser positivo \n distinto de cero \n y no puede contener letras ni puntos");
            break;
        }
    case 2:
        ordenar(arreglo);
        break;

    }
    if (op) {
        printf("\n\n");
        system("pause");
        system("cls");
    }
}

return 0;

} 

I think it is something related to the scan that is after the while the cycle prints on the screen and then remains in infinite loop, the main problem is when the option chosen is 1 and the scanf_S of numCedula takes a value that is not a integer

    
asked by Gaedi11 09.07.2017 в 20:45
source

1 answer

0
  

The main problem is when the option chosen is 1 and the scanf_S of numCedula takes a value that is not an integer

If a read error occurs (eg try to read a string as an integer), the erroneous characters are not taken out of the input buffer ... if you do not clean the buffer the rest of the readings may be equally incorrect and the program can end, as is your case, with an infinite loop.

To start the functions of the family scanf return an integer that indicates the number of values read. If in a type instruction:

scanf_s("%i", &numCedula);

The user enters a text the function will return 0.

What you have to do is detect that situation and act accordingly (for example, cleaning the input buffer). And how is the input buffer cleaned? Well, there is no standard form, but if we assume that when scanf returns 0 is that the buffer contains at least one character what we can do is discard characters until we find a line break:

if( scanf_s("%i", &numCedula) == 0 )
{
  while ((c = getchar()) != '\n' && c != EOF);
}

As I said there is no standard mechanism to do this and if you redirect the standard input you may have problems, but with keyboard inputs this mechanism should work as a general rule.

By the way, I've talked about C all the time because although the code you expose compiles in C ++, it does not really have anything of C ++. In C ++ the logical thing would be to work with std::cin and std::cout to work with the standard I / O. In that case the code should look like this:

#include <iostream> // std::cin
#include <limits> // std::numeric_limits

if( !std::cin >> numCedula )
{
  // Error
  std::cin.ignore(std::numeric_limits<int>::max(),'\n'); // Descartar caracteres
  std::cin.clear(); // limpiar flags de error
}
    
answered by 10.07.2017 в 10:12