I am trying to write an unlimited long string input until new line in C. I do not want to waste memory, so I want to dynamically allocate memory for the string. I have a working code, but as soon as I try to put it into a switch case statement it does not work well. I want to loop it and read 5 strings from the user, then put it into a switch case statement, but if I do so, the first iteration of the loop is being skipped. I would appreciate any help or tips.
char* dinput(void){
size_t m=1;
char *line=(char*) malloc(m*sizeof(char));
getline(&line,&m,stdin);
return line;
}
void new(void) {
FILE *fp = fopen(file, "a");
for (int i = 0; i < 5; i++) {
char *p = dinput();
fprintf(fp, "%s", p);
free(p);
}
fclose(fp);
}
void menu(void){
int nav;
do{
scanf("%d",&nav);
switch (nav) {
case 0:
break;
case 1:
new();
break;
}
}while nav!=0;
}