How to read string with space in a repeat structure?

0

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.

    
asked by anonymous 14.06.2017 / 23:07

2 answers

0

Your problem when using scansets (the name of those "%[^\n]" structures) is that, as you imagine, by the time it picks up the first item it reads until the first carriage return , but does not read the carriage return . Then, in the second, he tries to read until the next carriage return and, what a coincidence !, the first character he reads is a carriage return. So do not put anything in the second variable.

What you need to do is put the carriage return after your scanset there in the format string:

for (i = 0; i < qtdlivros; i ++) {
    scanf("%99[^\n]\n",livros[i].nome);
    scanf("%99[^\n]\n",livros[i].autor);
    scanf("%f",&livros[i].preco);
}

But as you will not put prompts asking for each value, you can economize by concatenating all format strings:

for (i = 0; i < qtdlivros; i ++) {
    scanf(
        "%99[^\n]\n%99[^\n]\n%f",
        livros[i].nome,
        livros[i].autor,
        &livros[i].preco
    );
}

Note the 99 that I put between % and [ : it is a subspecifier of maximum read length. This prevents scanf() from trying to write more than 99 characters in autor or nome (you must save a character for the null byte). This type of detail is very important to avoid the class of errors called buffer overrun , which is a recurring theme in C programming.

    
15.06.2017 / 00:32
0

You can quickly solve your problem by consuming the entire buffer, so that the \ n "\ n" do not disturb the other readings.

A method that works very well is this:

void flush_in(){ 
int ch;
while( (ch = fgetc(stdin)) != EOF && ch != '\n' ){} 
}

This way you could leave your code in the same format:

int main(){
char nome[100],autor[100];
float preco;

while(1){
    scanf("%[^\n]",nome);
    flush_in();
    scanf("%[^\n]",autor);
    flush_in();
    scanf ("%f",&preco);

    printf("\n%s %s %f\n",nome,autor,preco);
}

return 0;
}
    
31.10.2018 / 19:30