gets
doesn't work in the function neuePerson
,
it worked when it was in a for
loop, but then I changed it and now the compiler says isn't undefined.
I tried it with fgets
, now there is no warning, but it still ignores fgets
, so I cant write anything in the console.
in the main
function's gets
works. I'm a little bit confused... :o
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "readline.h"
//typedef struct Person {
// char name[50];
// char unit;
// int number;
//} Person;
typedef struct person {
char name[50];
char unit;
int number;
struct person *next;
} Person;
void neuePerson(Person *firstPerson) {
time_t t;
time(&t);
srand((unsigned int)t);
while (firstPerson->next != 0)
firstPerson = firstPerson->next;
printf("Gib einen Namen ein \n");
fgets(firstPerson->name, 50, stdin);
firstPerson->number = rand() % 99 + 1;
firstPerson->unit = rand() % 3 + 65;
firstPerson->next = (Person*)malloc(sizeof(Person));
firstPerson = firstPerson->next;
firstPerson->next = 0;
}
void ausgabe(Person *anfang) {
while (anfang->next != 0) {
printf("Name: %s", anfang->name);
printf(" Abteilung: %c", anfang->unit);
printf(" Tel.Nummer: %i\n", anfang->number);
anfang = anfang->next;
}
}
int main() {
Person* pers1 = (Person*)malloc(sizeof(Person));
//Person* test = (Person*)malloc(sizeof(Person));
//gets(test->name, 50);
//printf("%s", test->name);
pers1->next = 0;
char z = 'n';
while (z != 'e') {
printf("[n]eue Person, [a]usgabe, [e]nde");
z = getchar();
if (z == 'n') neuePerson(pers1);
else if (z == 'a') ausgabe(pers1);
}
}