Am besten du speicherst dir die Werte vorher in ein Array ab.
Hier mal eine flüchtige Lösung von mir, ich habe dein Programm aus praktischen Gründen mal etwas umstrukturiert.
Code:
// Getränkeautomat
#include <stdio.h>
#include <stdlib.h>
int main() {
//Getränkeliste
printf("\n Our drinks:");
printf("\n--------------------------");
printf("\n1) Coca Cola 1.85$");
printf("\n2) Ice Tea 1.45$");
printf("\n3) Water 1.20$");
//Getränkeauswahl
int choice;
double costs, payed, payed2;
double dollars[12] = {0.01, 0.05, 0.10, 0.25, 0.50, 1, //Münzen
2, 5, 10, 20, 50, 100} ; //Scheine, keine 1$ Note
printf("\n\n\nPlease choose your drink: ");
scanf("%d", &choice);
switch (choice) {
case 1:
costs = 1.85;
printf("\nYou choose Coca Cola. Please insert 1.85$: ");
break;
case 2:
costs = 1.45;
printf("\nYou choose Ice Tea. Please insert 1.45$: ");
break;
case 3:
costs = 1.20;
printf("\nYou choose Water. Please insert 1.20$: ");
break;
default:
printf("\nYou choose nothing. See you!");
exit(0);
}
scanf("%lf", &payed);
while (payed < costs) {
printf("Please insert more money (%2.2lf$ left): ", costs - payed);
scanf("%lf", &payed2);
payed += payed2;
}
printf("\nYou insert: %2.2lf\nYour change: %.2lf\n", payed, payed - costs);
if (payed > costs) {
printf("Take:\n");
payed -= costs; //in payed steht ab jetzt nur noch das verbleibende Wechselgeld
for (int i = 11; i >= 0; i--) {
if (payed >= dollars[i]) {
printf("%d x %10.02lf$\n", int(payed / dollars[i]), dollars[i]);
payed -= int(payed / dollars[i]) * dollars[i];
}
}
}
return 0;
}
line 41 payed += payed2;
sagt das aus, das der payed und payed2 zusammenlegt?
51 error: 'for' loop initial declarations are only allowed in C99 mode|
51 note: use option -std=c99 or -std=gnu99 to compile your code|
53 error: expected expression before 'int'|
54 error: expected expression before 'int'|
Der möchte das Programm nicht starten. Letzteres versteh ich auch nicht ganz. Das ist doch eine for-Schleife? Was genau passiert dann?