While or Do While exercise using CHAR variable

Asked

Viewed 603 times

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 your if should be: if((escolha!='A') && (escolha!='B') && (escolha!='F')){ (to be considered invalid when different from these 3) and in do ... while the condition should be: (escolha != 'F'); (to remain in the loop until informed 'F').

2 answers

1

As stated in the exercise there is no contraindication to use the structure switch-case, I took the liberty of improving your code, I believe with this structure it will be more readable.

Do the following:

do{
        printf("Digite A para candidato 1, ou B para candidato 2 (F para terminar): ");
        scanf(" %c", &escolha);

        switch (escolha){
        case 'A':
        valorA = valorA + 1;
        break;

        case 'B':
        valorB = valorB + 1;
        break;

        case 'F':
        break;

        default:
        printf("Codigo invalido\n");
        }
    }while(escolha != 'F');

There are improvements to make in your code, however I let you make them. An improvement would be to convert the input to uppercase, avoiding possible future problems.

I recommend reading:toupper

  • 1

    Thanks for the tip! I thought at first to use the switch, but I thought it was only possible to use it with numbers.. case type 1, case 2.. etc. it was good to know that, thank you.

1


The char type is an integral type as well as the int, it can contain a value between -127 to 128 if signed or 0 to 255 without sign. However, it is mostly used to store a character that ultimately will be a numerical value.

In your while you compare whether the choice is equal to F, when you have to compare if the choice is different from F so the loop keeps happening.

If you only want to add 1 more to one variable at a time, you can increment it with the operator ++.

Here an implementation:

#include <stdio.h>

int main() {
  int valorA = 0;
  int valorB = 0;

  char escolha = 0;

  do {
    printf("Digite A para candidato 1, ou B para candidato 2 (F para terminar): ");
    scanf(" %c", &escolha);

    if(escolha == 'A') {
      ++valorA;
    }else if(escolha == 'B') {
      ++valorB;
    }else if(escolha != 'F') {
      printf("Código inválido!\n");
    }
  }while(escolha != 'F');

  printf("Votos para o candidato 1: %d\n", valorA);
  printf("Votos para o candidato 2: %d\n", valorB);

  return 0;
}
  • 1

    Ah, I get it! I wasn’t getting the logic with char, I don’t know, for me when is a variable with numbers gets mt easier, until pq I started programming a little while, must be why haha, thanks guy!

Browser other questions tagged

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