I want to write a couple of strings (names) into a pointer array. I have written a code that I think logically would do this, but for some reason, it only prints the last string. What could be the solution for this?
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
int jatekosell(char* s) {
int n, ok = 1;
n = atoi(s);
if(n<3 || n > 15)
{
printf("Nem jo! 3 es 15 koze kell essen!");
getline(s, MAX);
ok = 0;
}
return ok;
}
int getline(char *s, int n);
void main() {
char amount[MAX];
char name[MAX];
char *names[MAX];
int number, i, j;
printf("Number of players between 3 and 15: ");
getline(amount, MAX);
while(!jatekosell(amount));
number = atoi(amount);
for(i = 0; i < number; i++)
{
printf("Type the %d. name: ", i+1);
getline(name, MAX);
names[i] = name;
}
for(i = 0; i < number; i++)
{
printf("The names: %s\n", names[i]);
}
getchar();
}
int getline(char *s, int n){
int c;
char *t=s;
while(n-- > 0 && (c=getchar())!=EOF&&c!='\n') *t++ = c;
*t='\0';
while(c!=EOF&&c!='\n') c=getchar();
return(t-s);
}