Pass values by reference C

Asked

Viewed 288 times

0

I have to make a program with options menu that allows me to read a student’s information, write their name and phone after entering their student number, and determine how many students are older than the user’s age. The program runs, but does not save the data. I wondered if you can help me in this problem!!

#include <stdio.h>

struct aluno{
    int numero;
    char nome[100];
    char morada[100];
    int idade;
    int telefone;
};

void infoPerson(struct aluno *Turma, int *i) {


    for (int i = 0; i < 50; i++) {
        printf("Introduza o número \n");
        scanf(" %d", &Turma->numero);
        printf("Introduza a idade \n");
        scanf(" %d", &Turma->idade);
        printf("Introduza o telefone \n");
        scanf(" %d", &Turma->telefone);
        printf("Introduza o nome \n");
        scanf(" %s", &Turma->nome[100]);
        printf("Introduza a morada \n");
        scanf(" %s", &Turma->morada[100]);
    }
}

int main() {
    int opc;
    int i = 0;
    struct aluno *Turma[2];
    do{
        do{
            printf("1 - Introduzir alunos\n");
            printf("2 - Teste\n");
            printf("3 - Teste\n");
            scanf("%d", &opc);
        } while (opc < 0 || opc > 3);
        switch (opc){
            case 1:
                infoPerson(Turma, &i);
                break;
            case 2:
                printf("\n");
                break;
            case 3:
                printf("\n");
                break;
            default:
                printf("Teste\n");
        }
    } while (opc < 0 || opc > 3);
}
  • This code does not even compile into a properly configured compiler.

  • In my IDE (Clion) it runs with some errors.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

The code has several errors, I think that’s what you want:

#include <stdio.h>

typedef struct {
    int numero;
    char nome[100];
    char morada[100];
    int idade;
    int telefone; //o tipo está errado
} Aluno;

void infoPerson(Aluno *turma, int limite) {
    for (int i = 0; i < limite; i++) {
        printf("Introduza o número \n");
        scanf(" %d", &turma[i].numero);
        printf("Introduza a idade \n");
        scanf(" %d", &turma[i].idade);
        printf("Introduza o telefone \n");
        scanf(" %d", &turma[i].telefone);
        printf("Introduza o nome \n");
        scanf(" %99s", turma[i].nome);
        printf("Introduza a morada \n");
        scanf(" %99s", turma[i].morada);
    }
}

int main() {
    int opc;
    Aluno turma[2];
    do {
        printf("1 - Introduzir alunos\n");
        printf("2 - Teste\n");
        printf("3 - Teste\n");
        scanf("%d", &opc);
        switch (opc) {
            case 1:
                infoPerson(turma, 2);
                break;
            case 2:
                printf("\n");
                break;
            case 3:
                printf("\n");
                break;
        }
    } while (opc < 0 || opc > 3);
    printf("%s - %d\n", turma[0].nome, turma[0].idade);
}

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

Then I detail the errors. The code is still not good.

  • Yes, the phone has to be an int unsigned. What does "%99s" mean? Thanks for the help!!

  • No, he has to be a char[12], almost nothing should be uint. Input formatting only allows 99 characters.

  • Okay, thank you. With this code so now I think I can do the rest I needed

Browser other questions tagged

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