if Abfragen Problem



  • Hallo immer wenn ich am Anfang die 2 für Beenden drücke springt das Programm hier hin:

    if (eingabe==1)
                  {
                    clrscr();
                    cout<<"Du bist am Fluss\n";
                    cout<<"(4) In Fluss gehen\n";
                    cout<<"(2) Zurueck\n";
                    cout<<"Auswahl";
                    cin>>eingabe;
                  }
    

    Ich weiss einfach nicht was ich da falch mache 😞

    Kompletter Programm Code

    #include <iostream>
    #include <conio.h >
    using namespace std;
    
    int main()
    {
    int start,wohin,eingabe,fisch;
    
    cout<<"(1) Starten,  (2) Beenden";
    cin>>start;
    
            if (start==2)
            {
               cout<<"ende";
               system("PAUSE");
            }
    do
    {
    
            if (start==1)
            {
               clrscr();
               cout<<"(1) Haus,  (2) Wald\n";
               cout<<"Auswahl: ";
               cin>>wohin;
            }
    
                    if (wohin==1)
                    {
                       clrscr();
                       cout<<"haus"
                       <<"\n";
                       cout<<"(3) Gegenstand nehmen\n";
                       cout<<"(2) Zurueck\n";
                       cout<<"Auswahl ";
                       cin>>eingabe;
                    }
    
                    if (wohin==2)
                    {
                       clrscr();
                       cout<<"Wald\n"
                       <<"\n";
                       cout<<"(1) Fluss\n";
                       cout<<"(2) Zurueck\n";
                       cout<<"Auswahl: ";
                       cin>>eingabe;
                    }
    
                  if (eingabe==3)
                  {
                    clrscr();
                    cout<<"Gegenstang genommen\n";
                    cout<<"(2) Zurueck\n";
                    cout<<"Auswahl";
                    cin>>eingabe;
                  }
    
                  if (eingabe==1)
                  {
                    clrscr();
                    cout<<"Du bist am Fluss\n";
                    cout<<"(4) In Fluss gehen\n";
                    cout<<"(2) Zurueck\n";
                    cout<<"Auswahl";
                    cin>>eingabe;
                  }
    
                    if (eingabe==4)
                    {
                      clrscr();
                      cout<<"(2) Zurueck \n";
                      cout<<"(6) Fisch fangen\n";
                      cout<<"Auswahl: ";
                      cin>>eingabe;
                    }
    
    }
    while (eingabe==2);
    
    system("PAUSE");
    }
    //---------------------------------------------------------------------------
    


  • derDA schrieb:

    Ich weiss einfach nicht was ich da falch mache 😞

    1. Grauenhaft formartierter und strukturierter Code.
    2. Du landest automatisch in der Schleife, unabhängig von deiner Eingabe, da nachdem die if-Behandlung abgehandelt ist, das Programm an der Stelle weiterläuft.

    ...
        if (start==2)
        {
            cout<<"ende";
            system("PAUSE");
        } // <-- Am ende der if-Anweisung läuft das Programm weiter...
        do
        { //... und hier rein
            if (start==1) // wobei ich bezweifel das bei einer 2 diese if ausgeführt wird
            {
    ...
    

    cu André



  • if (start==2)
    {
        cout<<"ende";
        system("PAUSE");
        return 0;
    }
    


  • Ah okay danke.



  • und ich würde mal die Variablen sinnvoll initialisieren


Anmelden zum Antworten