0
Good evening guys! and the following.. I need to do this exercise here while in C:
e) In a presidential election there are 2 candidates. Votes are informed through codes. For candidate 1 the code is A for candidate 2 the code is B. The algorithm can only receive code A or B, any other character typed must be ignored and an invalid code message must be issued. Develop an algorithm that calculates and writes at the end the total number of votes of candidate A and the total number of votes of candidate B. The completion of the algorithm will take place when the user enters code F.
Virtually every exercise I’ve done so far has either been with float or with int, ie, I don’t know how to use char, so far as I know it stores a symbol only, and need to put it between ' ', but I don’t know how to write this code, probably what I wrote is wrong, Can anyone give a hint? I made a note there in the code, if you could take a look at the //
#include <stdio.h>
#include <stdlib.h>
char escolha;
int valorA=0, valorB=0;
main()
{
do{
printf("Digite A para candidato 1, ou B para candidato 2 (F para terminar): ");
scanf(" %c", escolha);
if((escolha!='A')||(escolha!='B')||(escolha!='F')){
printf("Codigo invalido\n");
}else if(escolha == 'A'){
valorA = valorA + 1;
}else if(escolha == 'B'){
valorB = valorB + 1;
}
}while(escolha == 'F'); //talvez oq esta em cima nao esteja TOTALMENTE errado, mas eu acho que o maior erro ta aqui no while,
//eu quero encerrar o loop escrevendo F..
printf("A quantidade de Pessoas que votaram no candidato 1 foi: %i e do candidato 2: %i", valorA, valorB);
Here:
scanf(" %c", escolha);
you have to inform the address:scanf(" %c", &escolha);
. Maybe yourif
should be:if((escolha!='A') && (escolha!='B') && (escolha!='F')){
(to be considered invalid when different from these 3) and indo ... while
the condition should be:(escolha != 'F');
(to remain in the loop until informed 'F').– anonimo