0
I have to make a program to read the name, the author and the price of an n quantity of books (from structures). I made a struct for the books that way :
typedef struct{
char nome[100];
char autor[100];
float preco;
}dadoslivros;
and created a variable databooks to be able to read the information of the different books.
dadoslivros livros[100];
however when trying to read strings name or author I cannot read with spaces. I have tried:
for (i = 0; i < qtdlivros;i++){
scanf("%s",livros[i].nome);
scanf("%s",livros[i].autor);
scanf("%f",&livros[i].preco);
}
but this way I read only names or authors without space, if you put a space in the name that comes after the space goes to "authors" and if you put space in the author, what comes after the space goes to "price", then I tried this way :
for (i = 0; i < qtdlivros;i++){
scanf ("%[^\n],livros[i].nome);
scanf ("%[^\n],livros[i].autor);
scanf ("%f",&preco);
}
But that way it doesn’t even enter the reading. I would like to know why this happens and how to read the name and the author with space.
See if it helps you: https://answall.com/q/177409/101, https://answall.com/q/42981/101 and https://answall.com/q/109966/101
– Maniero
Thanks, but unfortunately none helped me because due to the limitations of the exercise I can not use the library "string. h". And this was the only way q vi to solve my problem (using fgets() ) from the links q sent me
– Mat.E
I can’t imagine what one has to do with the other.
– Maniero
The only way I could solve my problem from the links you sent me was using the function fgets(), which for this exercise does not serve because it is a college exercise and I can not use functions like fgets ().
– Mat.E
Oh so I don’t even waste time, these weird requirements don’t serve to teach anything useful, unless it was time to read all raw anyway, and even scanf could use, because the goal would be to train the data entry algorithm
– Maniero