I have to do a program to read the name, author and price of a quantity n of books (from structures). I made a struct for the books this way:
typedef struct{
char nome[100];
char autor[100];
float preco;
}dadoslivros;
and created a variable databook to be able to read information from different books.
dadoslivros livros[100];
but when trying to read the strings name or author I can not read with spaces. Already tried:
for (i = 0; i < qtdlivros;i++){
scanf("%s",livros[i].nome);
scanf("%s",livros[i].autor);
scanf("%f",&livros[i].preco);
}
But in this way I read only names or authors without space, if I put a space in the name what comes after the space goes to "authors" and if I put space in the author, what comes after the space goes to "price", hence I tried like this:
for (i = 0; i < qtdlivros;i++){
scanf ("%[^\n],livros[i].nome);
scanf ("%[^\n],livros[i].autor);
scanf ("%f",&preco);
}
But this way neither enters the reading. I would like to know why this happens and how to read the name and author with space.