while for loop with scanf

Asked

Viewed 159 times

1

I’m trying to make a report in c, and when I type the first character the program ends.

#include<stdio.h>
#include<string.h>
#include <ctype.h>
#include<conio.h>
#include <ctype.h>

int main()
{

    int a=0, b=0, c=0, d=0, x, e;
        
        do
        {
            printf( "Escolha entre \n\n a, b, c, d, e(fechar) \n\n");
            scanf(" %d", &x);
        
            if(scanf(" %d", &x) == a) 
            { 
                a++;
            }
            
            if(scanf(" %d", &x) == b)
            {
                b++;
            }
            
            if(scanf(" %d", &x) == c)
            { 
                c++; 
            }
            
            if(scanf(" %d", &x) == d)
            { 
                d++; 
            }
        }
        while (scanf(" %d", &x) != e);
        
        printf("\nTotal vezes de a: %d", a);
        printf("\nTotal vezes de b: %d", b);
        printf("\nTotal vezes de c: %d", c);
        printf("\nTotal vezes de d: %d", d);
}```
  • 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.

  • off the fact that you are using scanf atoa every loop

1 answer

0


  • 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.

Browser other questions tagged

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