Originally Posted by
laserlight
Kernelpanic: gets is inherently vulnerable to buffer overflow. You must never use it. Consider alternatives like fgets and scanf with %s and a field width instead.
Also, note that fflush(stdin) technically results in undefined behaviour. Look into a loop to read and discard characters until the first new line character instead.
With fgets it is well, "scanf" with %s and a field width I still don't understand. Instead of fflush(stdin) now with getchar() but in the output the text line will wrap.
Code:
//Daten einlesen - Input data
printf("\nFirma : ");
fgets(wagen1.firma, MAX, stdin);
printf("Modell : ");
fgets(wagen1.modell, MAX, stdin);
printf("Anzahl PS: ");
do {scanf("%f", &wagen1.ps);} while(getchar() != '\n' && getchar() != EOF);
printf("Preis : ");
do {scanf("%f", &wagen1.preis);} while(getchar() != '\n' && getchar() != EOF);
printf("\nFirma : ");
fgets(wagen2.firma, MAX, stdin);
printf("Modell : ");
fgets(wagen2.modell, MAX, stdin);
printf("Anzahl PS: ");
do {scanf("%f", &wagen2.ps);} while(getchar() != '\n' && getchar() != EOF);
printf("Preis : ");
do {scanf("%f", &wagen1.preis);} while(getchar() != '\n' && getchar() != EOF);
And this loop works once only.
Code:
//Daten einlesen
printf("\nFirma : ");
fgets(wagen1.firma, MAX, stdin);
printf("Modell : ");
fgets(wagen1.modell, MAX, stdin);
printf("Anzahl PS: ");
scanf("%f", &wagen1.ps);
printf("Preis : ");
scanf("%f", &wagen1.preis);
int c;
do
c = getchar();
while(c != '\n' && c != EOF);
With loop:
Line wrap: