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
Post a Comment