Dam ci odpowiedź którą pisałem do twojego poprzedniego tematu (w momencie gdy go zamykałeś).
Możesz użyć dodatkowej zmiennej do sterowania przepływem w programie.
W przygotowanym przykładzie program posiada pętlę główną która (na podstawie stanu) decyduje które menu wyświetlić (lub jaką funkcję wykonać).
#include <iostream>
using namespace std;
enum class StanMenu {
MenuGlowne,
Menu1,
Menu2,
Koniec
};
StanMenu menuGlowne() {
cout << "===Menu===" << endl;
cout << "Wybierz opcje" << endl;
cout << "1. Menu 1" << endl;
cout << "2. Menu 2" << endl;
cout << "3. Koniec" << endl;
int opcja;
cin >> opcja;
switch (opcja) {
case 1:
return StanMenu::Menu1;
case 2:
return StanMenu::Menu2;
case 3:
return StanMenu::Koniec;
default:
cout << "Zla opcja!" << endl;
}
}
StanMenu menu1() {
cout << "===Menu1===" << endl;
cout << "Wybierz opcje" << endl;
cout << "1. Menu 2" << endl;
cout << "2. Wroc" << endl;
int opcja;
cin >> opcja;
switch (opcja) {
case 1:
return StanMenu::Menu2;
case 2:
return StanMenu::MenuGlowne;
default:
cout << "Zla opcja!" << endl;
}
}
StanMenu menu2() {
cout << "===Menu2===" << endl;
cout << "Wybierz opcje" << endl;
cout << "1. Menu 1" << endl;
cout << "2. Wroc" << endl;
int opcja;
cin >> opcja;
switch (opcja) {
case 1:
return StanMenu::Menu1;
case 2:
return StanMenu::MenuGlowne;
default:
cout << "Zla opcja!" << endl;
}
}
int main()
{
StanMenu stanMenu = StanMenu::MenuGlowne;
while (stanMenu != StanMenu::Koniec) {
switch (stanMenu) {
case StanMenu::MenuGlowne:
stanMenu = menuGlowne();
break;
case StanMenu::Menu1:
stanMenu = menu1();
break;
case StanMenu::Menu2:
stanMenu = menu2();
break;
default:
cout << "Błąd! Koniec Programu!" << endl;
stanMenu = StanMenu::Koniec;
}
}
return 0;
}
Przeanalizuj ten kod. Zastanów się jakbyś mógł pociąć swój kod na mniejsze funkcje aby zastosować podobny schemat.