-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
– Maniero
The only modification I noticed regarding the problem was a char before the variable name in getchar, even adding the same error still occurs.
– davi michetti
No, I just put it somewhere else and showed that the mistake doesn’t happen.
– Maniero