+ 1

Problem with String input

Hello. I'm writing a program and I need the user to input a line like this (1, Stelios, 1.81, Greece). First thing I thought was use gets() because I want to accept space inputs . After a few tries compiling it without success I read that it was removed from C 11 because of dangers. Next thing I did was use scanf() and fgets () but it skips input like they were never there and nothing seems to work. char line[50]; gets(line); fgets(line,50,stdin); scanf ("%[^\n]",line); scanf ("%[^\n]%*c",line); Thats everything I tried.

8th Nov 2019, 6:56 PM
Stelios Toump
Stelios Toump - avatar
2 odpowiedzi
+ 2
You can try using getchar. //create your own function void get(char *buffer, const int nb) { int n = 0; char c = getchar(); while (c!='\n' && n < nb) { buffer[n] = c; n++; c = getchar(); } }
8th Nov 2019, 9:33 PM
Théophile
Théophile - avatar