Problem mit kopfgesteuerter while Schleife



  • Moin Leute!
    Ich bin Neuling, was C-Programmierung angeht und mir unterlaufen immer wieder solche winzig kleinen Fehler.
    Wieder mal ist es so weit, ich weis nicht weiter. Daher hier meine Frage:
    Warum spuckt der Compiler eine Fehlermeldung aus?
    (der Fehler tritt ab der 2. in jeder while-schleife auf. Meldung: "syntax error befor "}" token")

    //Unterprogramm zur Ausgabe des Wechselgeldes
    int Wechselgeld() {
    //Variablen initialisieren
    float g;
    int Anzahl1=0, Anzahl2=0, Anzahl3=0, Anzahl4=0, Anzahl5=0, Anzahl6=0;
    //Schleife zur Bestimmung der Münzusammensetzung
    while (g>0){scanf("%lf",
    while (g>=2){g=g-2; Anzahl1 = Anzahl1++ }
    while (g>=1){g=g-1; Anzahl2 = Anzahl2++}
    while (g>=0.5){g=g-0.5; Anzahl3 = Anzahl3++}
    while (g>=0.2){g=g-0.2; Anzahl4 = Anzahl4++}
    while (g>=0.1){g=g-0.1; Anzahl5 = Anzahl5++}
    while (g>=0.05){g=g-0.05; Anzahl6 = Anzahl6++}
    printf("Das Wechselgeld betraegt: &lf \ndas sind in Münzen\n %i x 2Euro\n %i x 1Euro\n %i x 50Cent\n %i x 20Cent\n %i x 10Cent\n %i x 5Cent ",g,Anzahl1, Anzahl2, Anzahl3, Anzahl4, Anzahl5, Anzahl6);

    Ich hoffe, es ist nicht zu unübersichtlich!
    Vielen Dank schonmal für Eure Hilfe!

    Gruß,
    San



  • Moin moin, auch schon aufgestanden? 😃
    Du kannst keine while Schleife als Parameter an eine Funktion übergeben.

    while (g>0){scanf("%lf",
    while (g>=2){g=g-2; Anzahl1 = Anzahl1++ } // <---- Fehler, while Anweisung anstelle eines gültigen Parameters für scanf
    

    Benutze Codetags, dann sieht dein Post so aus:

    int Wechselgeld() {
    //Variablen initialisieren
    float g;
    int Anzahl1=0, Anzahl2=0, Anzahl3=0, Anzahl4=0, Anzahl5=0, Anzahl6=0;
    //Schleife zur Bestimmung der Münzusammensetzung
    while (g>0){scanf("%lf",
    while (g>=2){g=g-2; Anzahl1 = Anzahl1++ }
    while (g>=1){g=g-1; Anzahl2 = Anzahl2++}
    while (g>=0.5){g=g-0.5; Anzahl3 = Anzahl3++}
    while (g>=0.2){g=g-0.2; Anzahl4 = Anzahl4++}
    while (g>=0.1){g=g-0.1; Anzahl5 = Anzahl5++}
    while (g>=0.05){g=g-0.05; Anzahl6 = Anzahl6++}
    printf("Das Wechselgeld betraegt: &lf \ndas sind in Münzen\n %i x 2Euro\n %i x 1Euro\n %i x 50Cent\n %i x 20Cent\n %i x 10Cent\n %i x 5Cent ",g,Anzahl1, Anzahl2, Anzahl3, Anzahl4, Anzahl5, Anzahl6);
    

    Wie kommst du bloß auf die Idee die ganzen while Schleifen derart zu verschachteln? Nimm lieber ne if Anweisung.



  • Legastheniker?



  • Sanbooy schrieb:

    while (g>0){scanf("%lf",

    Das ist der Hauptfehler. Da fehlt so einiges.

    So würde es funktionieren:

    #include <stdio.h>
    #include <stdlib.h>
    
    //Unterprogramm zur Ausgabe des Wechselgeldes
    int Wechselgeld()
    {
        //Variablen initialisieren
        float g, g1;
        int Anzahl1=0, Anzahl2=0, Anzahl3=0, Anzahl4=0, Anzahl5=0, Anzahl6=0;
        scanf("%f", &g);
        g1 = g;     // Da sich g verändert, muss das Wechselgeld nochmal gespeichert werden
        //Schleife zur Bestimmung der Münzzusammensetzung
        while (g>=2){g-=2; ++Anzahl1; } // So würde es besser aussehen.
        while (g>=1){g=g-1; Anzahl2 = Anzahl2++; }
        while (g>=0.5){g=g-0.5; Anzahl3 = Anzahl3++; }
        while (g>=0.2){g=g-0.2; Anzahl4 = Anzahl4++; }
        while (g>=0.1){g=g-0.1; Anzahl5 = Anzahl5++; }
        while (g>=0.05){g=g-0.05; Anzahl6 = Anzahl6++; }
        printf("Das Wechselgeld betraegt: %.2f \ndas sind in Münzen\n %i x 2Euro\n %i x 1Euro\n %i x 50Cent\n %i x 20Cent\n %i x 10Cent\n %i x 5Cent ",g1,Anzahl1, Anzahl2, Anzahl3, Anzahl4, Anzahl5, Anzahl6);
        return g1;
    }
    
    int main( void )
    {
        Wechselgeld ();
    
        return 0;
    }
    

    viele grüße
    ralph



  • Wutz schrieb:

    Legastheniker?

    Hääää?!
    😕



  • CJosef schrieb:

    Wie kommst du bloß auf die Idee die ganzen while Schleifen derart zu verschachteln? Nimm lieber ne if Anweisung.

    Die sind doch gar nicht verschachtelt. Die sind brav hintereinander.



  • Okay, quasi verschachtelt durch die g Variable 😃 🤡
    Aaach egal jetzt 😃


Anmelden zum Antworten