problem with infinite loop

1

I need to do a program that asks for the sex and age of a number of people (it is supposed to end by putting 'n' in sex) ... and then says how many women and men attended ... in addition to intervals of age (how many men and women of certain ages were registered) ... The point is that after the first iteration the program asks the sex ... then the age ... but immediately afterwards it asks again the age without going through the sex ... and I can not get out of there unless I force the exit or enter 'n' many times ...

/* Se supone que ingrese el sexo (F/f/M/m) y la edad de cada persona y cuando se
   ingrese 'N' o 'n' en sexo...el programa debe indicar cuantas mujeres
   fueron registradas y cuantas de ellas tenían entre 22 y 25 años...
   en el caso de los hombres también se debe indicar cuantos se registraron
   y decir cuántos de elos tenían entre 18 y 30 años...*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int edad, nvar=0, ndam=0, nv1830=0, nd2225=0;
  char sexo;

  do
  {
    printf("Ingrese sexo (F/M)(0 si no hay nadie): ");
    scanf("%c", &sexo);
    printf("\nIngrese edad (0 si no hay nadie): ");
    scanf("%d", &edad);
    if(sexo=='f' || sexo=='F')
    {
      ndam++;
      if(edad>=22 && edad<=25)
      {
        nd2225++;
      }
    }
    else if(sexo=='m' || sexo=='M')
    {
      nvar++;
      if(edad>= 18 && edad<=30)
      {
        nv1830++;
      }
    }
    else if(sexo=='n' || sexo=='N')
    {
      printf("Asistieron %d damas a la fiesta.\n", ndam);
      printf("Damas con edades entre 22 y 25: %d\n", nd2225);
      printf("\nAsistieron %d varones a la fiesta.\n", nvar);
      printf("Varones con edades entre 18 y 30: %d\n", nv1830);
    }
  }
  while(sexo!='n' || sexo!= 'N');
  return 0;
}
    
asked by Dr. Haus 20.05.2018 в 22:42
source

2 answers

1

The scanf interface is smart up to a point.

If you notice you will see that the execution does not leave scanf until you enter a line break. Well, scanf usually delete this line break on numerous occasions without you being aware of it, as for example in your code:

printf("Ingrese sexo (F/M)(0 si no hay nadie): ");
scanf("%c", &sexo);
printf("\nIngrese edad (0 si no hay nadie): ");
scanf("%d", &edad); // <<---

The second scanf is eliminating the line break that the user has stored in the input buffer when entering the sex and you have not had to do anything specific for this to happen.

However, entering the age in the input buffer leaves another line break and here already if the problems start. scanf will not delete the line break when asked to read a character.

However, we can ignore this line break by adding a space before %c :

scanf(" %c",&sexo);

Another option is to discard the line break after reading the age:

scanf("%d", &edad);
getchar(); // Descartamos el salto de línea
    
answered by 21.05.2018 / 08:40
source
0
printf("Ingrese sexo (F/M)(0 si no hay nadie): ");
scanf("%c", &sexo);
printf("\nIngrese edad (0 si no hay nadie): ");
scanf("%d", &edad);

When you do the above, when the call is for the first time the scanf this wait has to be typed something and doing so is a line break that is stored in a buffer. to justify it you just have to clean it with fflush(stdin); your code would be:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int edad, nvar=0, ndam=0, nv1830=0, nd2225=0;
  char sexo;

  do
  {
    fflush(stdin);
    printf("Ingrese sexo (F/M)(0 si no hay nadie): ");
    scanf("%c", &sexo);
    printf("\nIngrese edad (0 si no hay nadie): ");
    scanf("%d", &edad);
    if(sexo=='f' || sexo=='F')
    {
      ndam++;
      if(edad>=22 && edad<=25)
      {
        nd2225++;
      }
    }
    else if(sexo=='m' || sexo=='M')
    {
      nvar++;
      if(edad>= 18 && edad<=30)
      {
        nv1830++;
      }
    }
    else if(sexo=='n' || sexo=='N')
    {
      printf("Asistieron %d damas a la fiesta.\n", ndam);
      printf("Damas con edades entre 22 y 25: %d\n", nd2225);
      printf("\nAsistieron %d varones a la fiesta.\n", nvar);
      printf("Varones con edades entre 18 y 30: %d\n", nv1830);
    }
  }
  while(sexo!='n' || sexo!= 'N');
  return 0;
}
    
answered by 20.05.2018 в 23:51