c - Why can't I type a string with space? -


i'm practicing on struct, , here simple code. i'm having problem here couldn't find answer. code asks me type song's name, artist , duration of song. typed "my lightning speed", word "my" fills song's name. word "lightning" fill artist , speed fills duration. why? how can fix it?

#define _crt_secure_no_warnings #include <string.h> #include <stdio.h> #define size 20  typedef struct {     char name[size];     char artist[size];     int duration; } songname;  songname fillsong();  int main() {     songname songnumb1, songnumb2, songnumb3;      songnumb1 = fillsong();     songnumb2 = fillsong();      return 0; }  songname fillsong() {     songname tempc;      printf("\n");     printf("enter name of song: ");     scanf(" %s", tempc.name);     printf("name: %s\n", tempc.name);      printf("who artist? ");     scanf(" %s", tempc.artist);     printf("artist: %s\n", tempc.artist);      printf("what duration(seconds)? ");     scanf("%d", &tempc.duration);     printf("duration: %d\n", tempc.duration);      return tempc; } 

scanf skips white space (blanks, tabs,newlines, etc.) while reading input. read input format not fixed, best read line @ time.

please read " c programming language" brian w. kernighan , dennis m. ritchie learn more.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -