while is not running from the start

Asked

Viewed 71 times

2

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

main(){

 int parar=1; // Condição para parar.
 int a=0, b=0, c=0; //Elevadores
 int count=0; //Numero de pessoas
 char elevador=""; //Elevador

do{

  printf("Elevador: ");
  scanf("%c", &elevador);

  if(elevador == 'a'){
    a++;
    count++;
  }else if(elevador == 'b'){
    b++;
    count++;
  }else if(elevador == 'c'){
     c++;
     count++;
  }else{
     printf("inválido!");
  }

    printf("\nContinuar respondendo? s=1 n=0");
    scanf("%d", &parar);

  }while(parar != 0);
}

I am created a mini form for research but it is not being executed in the correct way, see how it is returned to me:

SS

2 answers

3


The basic code problem is that you are initiating a character as if it were a string, there is code not read correctly with the information. Use simple quotes to char.

This code can be improved, but it is not worth doing now because it is not even complete.

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

int main() {
    int parar = 1; // Condição para parar.
    int a = 0, b = 0, c = 0; //Elevadores
    int count = 0; //Numero de pessoas
    char elevador=' '; //Elevador
    do {
        printf("Elevador: ");
        scanf("%c", &elevador);
        if (elevador == 'a') {
            a++;
            count++;
        } else if (elevador == 'b') {
            b++;
            count++;
        } else if (elevador == 'c') {
           c++;
           count++;
        } else {
            printf("inválido!");
        }
        printf("\nContinuar respondendo? s=1 n=0");
        scanf("%d", &parar);
    } while (parar != 0);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • It is always necessary to start a char variable?

  • If you want to always work correctly yes, but you can have case that does not need as optimization, then you have to deepen.

-1

Good night. In the code you posted there’s a key you’re not opening

//this one down here where I left it in bold

} while(stop != 0); }

  • Bro, I don’t know what you mean

  • where you are opening this key ( } ) which is in front of the WHILE ?? because if you repair has another Cheve right after the command tbm, probably one of them is closing the DO command and the other is "lost"

  • One is to close the do{} other to close the main(){}

  • @WSS actually i problem is not this. I also did not understand the answer.

  • tried to use code: 'system ("pause"); Return(0);'

  • Put this code after the of and take a test.

Show 1 more comment

Browser other questions tagged

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