printf being duplicated after the first loop

Asked

Viewed 156 times

-2

I have this program and in the first loop of while it runs normally, print one single time each thing, but in the second loop it printa "Type S to vote, N to see the vote count and exit." twice before reading. Can anyone tell me why?

#include "pch.h"
#include <iostream>
#include <locale.h>

int main() {

    char resp;
    int presidente[15], governador[12], votop = 0, votog = 0, i = 0, x=2;



    while (x != 0) {

        printf ("Digite S para votar, N para ver a apuracao dos votos e sair. ");
        fflush(stdin);
        resp = getchar();

        if (resp == 's' || resp == 'S') {

            printf("\n\n\n Digite o numero para presidente: ");
            fflush(stdin);
            scanf_s("%d", &votop);

            i = votop;

            presidente[i] = presidente[i] + 1;

            printf("\n\nAgora digite o numero para gorvernador: ");
            fflush(stdin);
            scanf_s("%d", &votog);

            i = votog;

            governador[i] = governador[i] + 1;

            printf("\n\n\nObrigado por votar.\n\n\n");

        }
        if (resp == 'n' || resp == 'N') {
            x = 0;
        }
    }




    printf("\n\n\n");
    system("pause");
    return 0;
}
  • I could not reproduce the error, but I had to change a few things to test on the ideone that does not accept C11, and I took advantage and improved several things to make the code more readable: https://ideone.com/f3RzXt

  • The only modification I noticed regarding the problem was a char before the variable name in getchar, even adding the same error still occurs.

  • No, I just put it somewhere else and showed that the mistake doesn’t happen.

1 answer

-1

C data entry with scanf is a confusing subject. The use of fflush(stdin) is controversial, it doesn’t always work the way we expect. When reading a character, instead of using getchar it is more guaranteed to use scanf(" %c", &ch);, which ensures that the newline character (corresponds to the ENTER or RETURN of the keyboard) is ignored.

Below is a version of the program that works correctly, tested on Linux and Windows.

#include <stdio.h>

int main()
{
  char resp;
  int presidente[15], governador[12], votop = 0, votog = 0, i = 0, x=2;

  while (x != 0)
  {
    printf ("\n");
    printf ("Digite S para votar, N para ver a apuracao dos votos e sair: ");
    scanf(" %c", &resp); // <--- veja o espaço antes do %c

    if (resp == 's' || resp == 'S')
    {
      printf("\nDigite o numero para presidente: ");
      scanf("%d", &votop);

      i = votop;

      presidente[i] = presidente[i] + 1;

      printf("\nAgora digite o numero para gorvernador: ");
      scanf("%d", &votog);

      i = votog;

      governador[i] = governador[i] + 1;

      printf("\nObrigado por votar\n");
    }

    if (resp == 'n' || resp == 'N')
    {
      x = 0;
    }
  }

}

ADDENDUM:

Test on Windows 10, compiled with Visual Studio 2005

D:\projects\misc
$ so339544.exe

Digite S para votar, N para ver a apuracao dos votos e sair: X

Digite S para votar, N para ver a apuracao dos votos e sair: X

Digite S para votar, N para ver a apuracao dos votos e sair: X

Digite S para votar, N para ver a apuracao dos votos e sair: N

D:\projects\misc
$ so339544.exe

Digite S para votar, N para ver a apuracao dos votos e sair: s

Digite o numero para presidente: 13

Agora digite o numero para gorvernador: 40

Obrigado por votar

Digite S para votar, N para ver a apuracao dos votos e sair: n

D:\projects\misc
$
  • Thanks for the help, but when I use the scanf the way the program told me It totally uga and begins to descend letters infinitely on the screen.

Browser other questions tagged

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