#A little bit of problem with switch
11 messages · Page 1 of 1 (latest)
void menuObsługa(listaPotraw* listaPotraw, listaSkładników* listaPosiadaneSkładniki)
{
std::cout << "Witaj użytkowniku" << std::endl << std::endl;
char obsługaProgramu = menuGłówne();
while (true) {
switch (obsługaProgramu) {
case 'M':
obsługaProgramu = menuGłówne();
case 'P':
obsługaProgramu = wyświetlListęPotraw(listaPotraw);
case 'S':
obsługaProgramu = wyświetlListęSkładników(listaPosiadaneSkładniki);
case 'Z':
obsługaProgramu = zapiszListyPlik(listaPotraw, listaPosiadaneSkładniki);
case 'W':
case 'E':
exit(1);
}
}
}
char menuGłówne()
{
char komenda = NULL;
std::cout << "Menu główne projektu:" << std::endl;
std::cout << "P. Pokaż listę potraw" << std::endl;
std::cout << "S. Pokaż listę składników" << std::endl;
std::cout << "Z. Zapisz listy" << std::endl;
std::cout << "W. Wczytaj zapisane listy" << std::endl;
std::cout << "E. Zakończ program" << std::endl;
std::cout << "Wprowadź odpowiednią literę: ";
std::cin >> komenda;
std::cout << std::endl;
komenda = toupper(komenda);
while (komenda != 'P' && komenda != 'S' && komenda != 'Z' && komenda != 'W' && komenda != 'E')
{
std::cout << "Wprowadź ODPOWIEDNIĄ literę: ";
std::cin >> komenda;
komenda = toupper(komenda);
}
return komenda;
}
char wyświetlListęPotraw(listaPotraw* listaPotraw)
{
char komenda = NULL;
int id_wejsc = 0;
std::cout << "Oto lista potraw" << std::endl << std::endl;
listaPotraw->wyświetlListę();
std::cout << "Menu obsługi listy potraw:" << std::endl;
std::cout << "D. Dodaj potrawę" << std::endl;
std::cout << "U. Usuń potrawę" << std::endl;
std::cout << "W. Wyświetl składniki potrawy" << std::endl;
std::cout << "M. Powróć do menu głównego" << std::endl;
std::cout << "Wprowadź odpowiednią literę: ";
std::cin >> komenda;
std::cout << std::endl;
komenda = toupper(komenda);
while (true)
{
if (komenda != 'M' && komenda != 'D' && komenda != 'U' && komenda != 'W')
{
std::cout << "Wprowadź ODPOWIEDNIĄ literę (D/U/W/M): ";
std::cin >> komenda;
komenda = toupper(komenda);
}
else
{
switch (komenda) {
case 'M':
return komenda;
case 'D':
listaPotraw->dodajPotrawę();
std::cout << "Nowa lista:" << std::endl;
listaPotraw->wyświetlListę();
break;
case 'W':
std::cout << "Wprowadź numer potrawy:";
std::cin >> id_wejsc;
listaPotraw->wyświetlSkładnikiPotrawy(id_wejsc);
break;
case 'U':
std::cout << "Wprowadź numer potrawy:";
std::cin >> id_wejsc;
listaPotraw->usuńPotrawę(id_wejsc);
std::cout << "Nowa lista:" << std::endl;
listaPotraw->wyświetlListę();
break;
}
}
std::cout << "Wprowadź odpowiednią literę (D/U/W/M): ";
std::cin >> komenda;
std::cout << std::endl;
komenda = toupper(komenda);
}
}
Basically what happens if I go from main menu to any function and I return the char 'M' which should make main menu appear again, the program stops
Sorry if my code is a mess
everything else works fine, it's just the return to main menu that is not working
you need a break after every single case statement, unless you return, otherwise every other case statement gets executed
void menuObsługa(listaPotraw* listaPotraw, listaSkładników* listaPosiadaneSkładniki)
{
std::cout << "Witaj użytkowniku" << std::endl << std::endl;
char obsługaProgramu = menuGłówne();
while (true) {
switch (obsługaProgramu) {
case 'M':
obsługaProgramu = menuGłówne();
break; // here for example
case 'P':
obsługaProgramu = wyświetlListęPotraw(listaPotraw);
case 'S':
obsługaProgramu = wyświetlListęSkładników(listaPosiadaneSkładniki);
case 'Z':
obsługaProgramu = zapiszListyPlik(listaPotraw, listaPosiadaneSkładniki);
case 'W':
case 'E':
exit(1);
}
}
}