Array returns nothing

Asked

Viewed 116 times

0

I’m trying to get an array to store the typed letter if it’s not a vowel, however, it’s not storing anything, just a few strange symbols appear. Follows the code:

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

int main()
{
    system("cls");
    char letra[10], consoante[10];
    int i, x = 0;

    printf("Faca um Programa que leia um vetor de 10 caracteres, e diga quantas consoantes foram lidas. Imprima as consoantes\n");

    for (i = 0; i < 10; i++)
    {
        printf("Digite a letra: ");
        fflush(stdin);
        scanf("%c", &letra[i]);

        if ((letra[i] == 'a') || (letra[i] == 'e') || (letra[i] == 'i') || letra[i] == 'o' || letra[i] == 'u')
        {
            printf("Caiu aqui");
        }
        else
        {
            consoante[i] == letra[i];
        }
    }

    for (x = 0; x < 10; x++)
    {
       printf("A consoante foi: %c\n", consoante[x]);
    }


    system("pause");
}

I already tested the if and the else and is falling into the right place, but does not store in the array.

  • You are using the same index variable and are therefore leaving positions containing vowels with memory junk in the consonant array.

2 answers

2


There are several problems there. The first is that you don’t need to have two arrays, as described. There is a syntactic/semantic error when trying to assign a value with ==, this is the comparison operator. And the code ends up being more complex than it needs to be and then ends up creating a array full of holes but still access all 10 positions. You have to control the counter of how many consonants there are in the array and only read these, including because the exercise asks to print this total and your code does not. The test you did just landed in the wrong place, the if is inverted.

#include <stdio.h>

int main() {
    char consoante[10];
    int total = 0;
    char c;
    for (int i = 0; i < 10; i++) {
        printf("Digite a letra: ");
        char letra;
        scanf("%c", &letra);
        while ((c = getchar()) != '\n' && c != EOF) { }
        if (letra != 'a' && letra != 'e' && letra != 'i' && letra != 'o' && letra != 'u') consoante[total++] = letra;
    }
    printf("\n%d", total);
    for (int i = 0; i < total; i++) printf("\nA consoante foi: %c", consoante[i]);
}

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

I used a universal method to clean the buffer, what you used only works in certain situations.

0

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

int main()
{
    system("cls");
    char letra[10], consoante[10];
    int i, x = 0, contaNumeroDeConsoantes = 0;;


    printf("Faca um Programa que leia um vetor de 10 caracteres, e diga quantas consoantes foram lidas. Imprima as consoantes\n");

    for (i = 0; i < 10; i++)
    {
        printf("Digite a letra: ");
        fflush(stdin);
        scanf("%c", &letra[i]);

        if ((letra[i] == 'a') || (letra[i] == 'e') || (letra[i] == 'i') || letra[i] == 'o' || letra[i] == 'u')
        {
            printf("Caiu aqui");
        }
        else
        {
            consoante[contaNumeroDeConsoantes ] = letra[i];
            contaNumeroDeConsoantes = contaNumeroDeConsoantes + 1; 
        }
    }

    for (x = 0; x < contaNumeroDeConsoantes ; x++)
    {
       printf("A consoante foi: %c\n", consoante[x]);
    }


    system("pause");
}

EXPLANATION In your code there was an error in the line consoante[i] == letra[i]; to assign a value uses the = and not ==, and these strange symbols that you said you saw, it’s simply because you were trying to access vector indexes consoantes which did not exist, because the for (x = 0; x < 10; x++){printf("A consoante foi: %c\n", consoante[x]);} would always "think" that consoante has 10 values, which would not always happen, because imagine that you pass the values in this order: A,B,C,D,E,I,O,U,A,A, the only values that should be in the variable consoante is consoante[0]->B, consoante[1]->C and consoante[2]-D,namely the for vector display consoante should be only up to 3, not up to 10, so a new variable called contaNumeroDeConsoantes, variable that is incremented every time you enter a consonant.

AND WHAT WERE THE SYMBOLS? When you declare a variable in C, it saves this variable in some part of memory, example: consoante[0] = 1 -> (local da variável poderia ser 215 na memória), and if you try to do consoante[293] this value will be from another place in memory. When you start to see pointers maybe understand better

Browser other questions tagged

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