0
I have the following code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAM 3
typedef struct{
char nome[50];
char musica[50];
int integrantes;
int ranking;
}Banda[TAM];
void ler(Banda *banda){
int i=0;
for(i=0;i<TAM;i++){
printf("Nome da banda: ");
fgets(banda[i]->nome,50,stdin);
fflush(stdin);
printf("Tipo de musica: ");
fgets(banda[i]->musica,50,stdin);
fflush(stdin);
printf("Numero de integrantes: ");
scanf("%i",&banda[i]->integrantes);
fflush(stdin);
printf("Posicao: ");
scanf("%i",&banda[i]->ranking);
fflush(stdin);
printf("\n\n");
}
}
void mostrar(Banda banda){
int i=0;
for(i=0;i<TAM;i++){
printf("Banda %i\n\n",i+1);
printf("Nome da banda: %s\n",banda[i].nome);
printf("Tipo de musica: %s\n",banda[i].musica);
printf("Integrantes: %i\n",banda[i].integrantes);
printf("Posicao: %i\n",banda[i].ranking);
}
}
int main(){
Banda banda[TAM];
ler(&banda);
mostrar(banda);
return 0;
}
I can read the 3 positions with the function ler()
, but when writing with the function mostrar()
, only the first position is written in the correct way, the other two are not,(random letters and numbers appear).
I appreciate the help.
This code does not work http://ideone.com/v51JKC
– Maniero
i circled before sending... What doesn’t work? don’t compile or any error?
– Brumazzi DB
It’s running there, just look.
– Maniero
I changed the input methods, now it’s working, try to avoid using scanf with gets.
– Brumazzi DB