+ 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.
1 Answer
+ 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();
}
}