Hi! I have a question about showing my input.
First , I have to ask user how many names want to input
i need to use a (dynamic) array to get all the name
then, show it
However, the program didnt show the name after i input the name
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define merror() {printf("memory allocation problem\n");exit(1);}
int main(void)
{
char *buffer;
char **buf;
int num;
int i, j;
printf(" how many names will be entered : \n");
fflush(stdout);
fgets ( buffer, 10, stdin );
num = atoi (buffer);
if (num <=0)
{
printf("empty input \n");
}
else if (num >10)
{
printf("input too long \n");
}
else if (num >0 && num <= 10)
{
printf("you will enter %d names \n", num);
}
for(i=0; i < num; i++)
{
for(j=0; j<21; j++)
{
buf[j]=fgetc(stdin);
if (buf[j]=='\n') {
buf[j]='\0';
break; /* break j-loop */
}
}/* end j-loop */
if (j==21)
{
printf("input too long\n");
while(fgetc(stdin)!='\n');
continue; /* continue i-loop */
}
}
printf("\nunsorted input:\n");
for(i=0; i< num; i++)
printf("%s\n", buf[i]);
return 0;
}