Space in a String C

1

I'm starting to learn C, and I came across the following doubt:

When I ask the user to inform me a song or artist, he ends up informing me of a song with spaces, type "AS I AM" , but the program skips the part of the song and the artist, how to solve it?

printf("MUSICA: "); 
scanf("%s", novo->nome);
printf("ARTISTA: "); 
scanf("%s", novo->artista);
printf("ANO: "); 
scanf("%d", &novo->ano);
    
asked by anonymous 27.02.2016 / 01:13

1 answer

2

scanf accepts several different structures to input values, and to capture an entire line, you must use a structure that reads up to a specific character.

char line[500];
scanf("%[^\n]",line);

The brackets "[" and "]" indicate that you will have a condition, "^" indicates that you must collect everything typed until you reach "\ n".

    
27.02.2016 / 01:29