Originally Posted by
tabstop
Like all the scanf modes, the
%[ leaves the new-line in the buffer; unlike many of the scanf modes,
%[ will
not skip over that whitespace when it is used again (
%f is fine, for instance, because it will explicitly throw away any new-lines or other white space before the number). So what I should have recommended was
Code:
scanf(" %49[^\n]", wagen1.modell);
because the explicit space in the format string will then match any (or none) of that extra guff that's already there.
That was it! I had already seen that, but did something wrong. Now it is going right. Thanks!
I have it tested with three cars - I hope the comparisons are correct.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX 50
//New data type: AUTO
typedef struct
{
char firma[MAX];
char modell[MAX];
float ps;
float preis;
}AUTO;
//Structures as an argument of a function
int vergleich(AUTO *zgr_wagen1, AUTO *zgr_wagen2, AUTO *zgr_wagen3);
int main(void)
{
//Structur variables
AUTO wagen1, wagen2, wagen3;
//Pointers for an access of structures
AUTO *zgr_w1, *zgr_w2, *zgr_w3;
//Allacation of the adresses of both structures
//Zuweisung der Adressen der beiden Strukturen
zgr_w1 = &wagen1;
zgr_w2 = &wagen2;
zgr_w3 = &wagen3;
int vergleich_ergebnis;
//Daten einlesen - Input data
printf("\nFirma : ");
scanf(" %49[^\n]", wagen1.firma);
printf("Modell : ");
scanf(" %49[^\n]", wagen1.modell);
printf("Anzahl PS: ");
scanf(" %f", &wagen1.ps);
printf("Preis : ");
scanf(" %f", &wagen1.preis);
fflush(stdin);
printf("\nFirma : ");
scanf(" %49[^\n]", wagen2.firma);
printf("Modell : ");
scanf(" %49[^\n]", wagen2.modell);
printf("Anzahl PS: ");
scanf(" %f", &wagen2.ps);
printf("Preis : ");
scanf(" %f", &wagen2.preis);
fflush(stdin);
printf("\nFirma : ");
scanf(" %49[^\n]", wagen3.firma);
printf("Modell : ");
scanf(" %49[^\n]", wagen3.modell);
printf("Anzahl PS: ");
scanf(" %f", &wagen3.ps);
printf("Preis : ");
scanf(" %f", &wagen3.preis);
fflush(stdin);
vergleich_ergebnis = vergleich(zgr_w1, zgr_w2, zgr_w3);
printf("\nVergleich von Kaufpreis zu PS\n\n");
if(vergleich_ergebnis == 0)
{ printf("Das Verhaeltnis von Kaufpreis zur Anzahl PS ist gleich.\n"); }
else if(vergleich_ergebnis == 1)
{ printf("Der %s hat das bessere Preis- Leistungsverhaeltnis.\n", wagen1.modell); }
else if(vergleich_ergebnis == 2)
{ printf("Der %s hat das bessere Preis- Leistungsverhaeltnis.\n", wagen2.modell); }
else
{ printf("Der %s hat das bessere Preis- Leistungsverhaeltnis.\n", wagen3.modell); }
printf("\n\nGIMMICK:\n");
printf("%5.2f PS sind %5.2f KW\n", zgr_w1->ps, (zgr_w1->ps / 1.36));
printf("%5.2f PS sind %5.2f KW\n\n", zgr_w2->ps, (zgr_w2->ps / 1.36));
return(0);
}
int vergleich(AUTO *zgr_wagen1, AUTO *zgr_wagen2, AUTO *zgr_wagen3)
{
float wagen1_ps, wagen2_ps, wagen3_ps;
float wagen1_preis, wagen2_preis, wagen3_preis;
float preis_leistung1;
float preis_leistung2;
float preis_leistung3;
//'->' arrow operator - is equal with '(*zgr_wagen1).ps'
wagen1_ps = zgr_wagen1->ps;
wagen1_preis = zgr_wagen1->preis;
wagen2_ps = zgr_wagen2->ps;
wagen2_preis = zgr_wagen2->preis;
wagen3_ps = zgr_wagen3->ps;
wagen3_preis = zgr_wagen3->preis;
//Price pro PS/KW
preis_leistung1 = (wagen1_preis / wagen1_ps);
preis_leistung2 = (wagen2_preis / wagen2_ps);
preis_leistung3 = (wagen3_preis / wagen3_ps);
//Compare purchase price to PS
//How many Dollar/Euro for one PS/KW
if(preis_leistung1 == preis_leistung2 && preis_leistung1 == preis_leistung3)
{
return(0);
}
else if(preis_leistung1 < preis_leistung2 && preis_leistung1 < preis_leistung3)
{
return(1);
}
else if(preis_leistung2 < preis_leistung1 && preis_leistung2 < preis_leistung3)
{
return(2);
}
else { return(3); }
}