- Declaring variable wrong way.
Initially you are making the mistake of trying to read characters with an integer value variable.
int a=0, b=0, c=0, d=0, x, and;
When in fact you should be declaring.
int a = 0, b = 0, c = 0, d = 0;
char x;
Since what is intended in this matter is precisely to count how many times the 4 letters were provided by the user, in a loop that will only be finalized when the letter is read "and", that is, the variable e
which you stated in that situation is not necessary.
In addition, it is also necessary to change the input value of:
scanf(" %d", &x);
To:
scanf (" %c ", &x);
Since previously, as we changed the type of the variable, of int for char we should also change the type of variable reading of scanf.
- Commando
if
is declared incorrectly.
You are misusing the command if
, recommend reviewing a little about. Where you stated in the following way:
if(scanf(" %d", &x) == a)
When in fact the most correct way in this situation would be to declare:
if (x == 'a') /* Verificando se é verdade que a variável "x" possui como valor a letra a */
Remembering that in this type of comparison, it will only be true if the user type a lowercase letter.
- Commando
do while
is declared incorrectly.
In the same way as the command if
, you have declared wrong command do while
. Where you stated in the following way:
while (scanf(" %d", &x) != and);
When in fact the correct way to declare this while
in that situation, that would be:
while (x != 'e'); /* O loop só cessará quando a letra "e" for declarada pelo usuário. */
- Corrected code without many changes.
With these changes quoted above, your code would look something like this:
int main(){
int a = 0, b = 0, c = 0, d = 0;
char x;
printf( "Escolha entre as seguintes letras: \n\n a, b, c, d, e(Para Fechar) \n\n");
do
{
scanf("%c", &x);
if(x == 'a')
{
a++;
}
if(x == 'b')
{
b++;
}
if(x == 'c')
{
c++;
}
if(x == 'd')
{
d++;
}
}
while (x != 'e');
printf("\nTotal de vezes de a: %d", a);
printf("\nTotal de vezes de b: %d", b);
printf("\nTotal de vezes de c: %d", c);
printf("\nTotal de vezes de d: %d", d);
return 0;
Remembering that I kept the original code made by you, even though there are more correct ways to make such a desired algorithm.
A serious confusion between what is a variable and what is a constant. Your message says
Escolha entre \n\n a, b, c, d, e(fechar)
but in your tests you compare what was read with an entire variable, all of them initialized with 0.– anonimo
off the fact that you are using scanf atoa every loop
– FourZeroFive