Alternative zu goto???
-
Hallo,
Gibt es eine möglichkeit in meinem Code die zwei gotos zu ersetzen, ohne dabei Funktionen zu benutzen?
#include <iostream> using namespace std; int main() { again: cout<<"Bitte geben sie 2 Zahlen ein."<<endl; int zahl1, zahl2; cin>>zahl1; cin>>zahl2; cout<<zahl1<<" + "<<zahl2<<" = "<<zahl1 + zahl2<<endl; error: cout<<"Wollen sie das Programm neustarten?"<<endl; cout<<"1=Ja Nein=2)"<<endl; int wahl; cin>>wahl; if (wahl == 1) { goto again; } else if (wahl == 2) { return EXIT_SUCCES; } else { cout<<"Fehlerhafte Eingabe!"<<endl; goto error; } }
-
z.B. 'ne do while endlos Schleife mit continue und break.
-
#include <iostream> using namespace std; int main() { do { cout<<"Bitte geben sie 2 Zahlen ein."<<endl; int zahl1, zahl2; cin>>zahl1; cin>>zahl2; cout<<zahl1<<" + "<<zahl2<<" = "<<zahl1 + zahl2<<endl; do { cout<<"Wollen sie das Programm neustarten?"<<endl; cout<<"1=Ja Nein=2)"<<endl; int wahl; cin>>wahl; if(wahl != 1 and wahl != 2) { cout<<"Fehlerhafte Eingabe!"<<endl; continue; } break; } while (true); } while (wahl == 1); return EXIT_SUCCES; }Mit Schleifen lässt sich eigentlich jedes goto ersetzen.
Wenn du Funktionen verwendest wird das Ganze auch noch etwas schöner.
-
Wenn du eine Fehlerbehebung machen willst, dann nimm Exceptions her.
-
Danke!
Ich denke nach so etwas habe ich gesucht