Problem beim Menu



  • Hallo,

    Ich steh mal wieder total auf dem Schlauch. Das Problem ist folgendes, das, wenn ich bei der menu Funktion '5' drücke sich das Programm nur dann beendet, wenn ich es direkt nach dem Programmstart mache. Allerdings geht es nicht, wenn ich z.b vorher in die options Funktion gehe, wieder zurück zur menu Funktion, wieder rein in die option Funktion, wieder raus...usw. Denn wenn ich dann '5' drücke kommt "Invalid choice, try again". Woran kann das liegen?

    int main(int argc, char **argv)
    {
    ...
    	if(menu() == -1)
    	{
    		endwin();
    		return 0;
    	}
    
    	return 0;
    }
    
    int menu(void)
    {
    ...
    for(;;)
    {
         switch(input=getch())
         {
             case '1' : item.severity = 1; start(0,0); break;
             case '2' : item.severity = 2; start(0,0); break;
             case '3' : item.severity = 3; start(0,0); break;
             case '4' : options(); break;
             case '5' : return -1; break;
    
             default: printf("Invalid choice, try again!\n"); break;
         }
    }
    ...
    

    MfG

    podok



  • Wahrscheinlich liegt es daran, dass das Return noch im Puffer ist?! Vermute ich mal einfach.....



  • break; weglassen bei der 5



  • miller_m:

    nein bringt leider nichts, wenn ich dann 5 drücke erscheint noch ein teil aus der options Funktion in der menu Funktion.



  • Hast du dir schonmal den Wert von input ausgeben lassen?

    PS: Was macht deine Funktion options()?



  • AJ: Ja, der wert stimmt... 5, so ist er in input drinn und das auch nach dem Funktions gewechsel.

    Das wäre ein bischen viel Code hier, ich paste mal das "wichigste":

    int options()
    {
    	unsigned short int y = 8;
    
    	clear();
    	echo();
    	keypad(stdscr, TRUE);
             ....
    	for(;;)
    	{
    		switch(getch())
    		{
                            ...
                            case ESCAPE: hangman_game_menu(); break;
    
                            default: break;
    	         }
              }
    

    Naja grob gefasst, dort befindet sich auch eine art menu drinn, wo man verschiedene Einstellungen vornehmen kann. Damit das besser aussieht habe ich da einen "Auswahl Pfeil" gebastelt, den ich dann in der original menu Funktion sehe.



  • versuch mal dein problem einzugrenzen. nimm doch mal case 1 - 4 raus.



  • Nun, das Problem besteht weiterhin. Auch bei den anderen Funktionen klappt das "quiten" nicht, wenn ich das "hin und her gewechsel" mache. Ich komme zwar immer wieder in die menu Funktion rein, kann dann auch in ander Funktionen wechseln, nur halt nicht das Programm beenden.



  • also so funzt es in groben zügen.

    #include <stdio.h>
    #include <conio.h>
    
    void option()
    {
    	for(;;) 
    	{ 
    		switch(getch()) 
    		{ 
    			case '1': printf("option 1\n"); break; 
    			case '2': printf("option 2\n"); break; 
    			case '3': printf("option 3\n"); break; 
    			case '4': printf("option 4\n"); break; 
    			case '5': printf("option 5\n"); return; 
    
    			default: printf("Invalid choice, try again!\n"); break; 
    		} 
    	}
    }
    
    int menu()
    {
    	for(;;) 
    	{ 
    		switch(getch()) 
    		{ 
    			case '1': printf("menu 1\n"); break; 
    			case '2': printf("menu 2\n"); break; 
    			case '3': printf("menu 3\n"); break; 
    			case '4': printf("menu 4\n"); option(); break; 
    			case '5': printf("menu 5\n"); return -1; 
    
    			default: printf("Invalid choice, try again!\n"); break; 
    		} 
    	}
    }
    
    int main(int argc, char *argv[])
    {
    
        if(menu() == -1) 
        {
        	printf("ende mit taste\n");
        	getch();
    		return 0; 
        } 
    	return 0;
    }
    

    zeig doch mal mehr code



  • Nagut, sieht aber ziemlich wüst aus.
    Unten seht ihr nun die Funktionen mal ausgeschrieben. Einmal die main(), menu() und die options()

    int hangman_game_options()
    {
    	unsigned short int y = 8;
    
    	clear();
    	echo();
    	keypad(stdscr, TRUE);
    
    	hangman_image_box();
    
    	attron(A_BOLD);
    	mvprintw(6,6, "Hangman - Settings");
    	attroff(A_BOLD);
    
    	attron(A_BOLD | COLOR_PAIR(1));
    	mvhline(7,6, ACS_HLINE, 62);
    	mvhline(15,6, ACS_HLINE, 62);
    	attroff(A_BOLD | COLOR_PAIR(1));
    
    	mvprintw(8,6, "->");
    	mvprintw(8,9, "Select or change your nickname   : %s", stat_item.name);
        mvprintw(9,9, "Select your own dictonary        :");
    
        if(opt_item.dict[0] == '\0')
    	{
             mvprintw(9, 44, "<path>");
        }
        else
        {
             mvprintw(9, 44, "%s", opt_item.dict);
        }
    
    	mvprintw(10,9,"Delete the Hangman record table  : <yes/no>");
    
    	for(;;)
    	{
    		switch(getch())
    		{
    			case KEY_UP:
    				if(y > 8)
                    {
                         mvprintw(y, 6, "  ");
                         y--; /* inside borders */
                    }
    				else if(y == 8)
                    {
                         mvprintw(y, 6, "  ");
                         y = 10; /* upper borders */
                    }
    			break;
    
    			case KEY_DOWN:
    				if(y < 10)
                    {
                         mvprintw(y, 6, "  ");
                         y++; /* upper borders */
    
                    }
                    else if(y == 10)
                    {
                         mvprintw(y, 6, "  ");
                         y = 8; /* inside borders */
                    }
    			break;
    
                case KEY_ENTER:
    			case '\r':
    			case '\n':
    			    if(y == 10)
    			    {
                         /* quick and dirty */
                         make_color_chars(y, 44, 23, ' ');
    			         attron(A_BOLD | COLOR_PAIR(2));
                         mvgetnstr(y, 44, opt_item.drec, YNO_SIZE);
                         attroff(A_BOLD | COLOR_PAIR(2));
    
    			         if(strcmp(opt_item.drec, "yes") == 0)
    			         {            
    			                     delete_status_bar(y, 44, 24, ' ');
                                     attron(A_BOLD | COLOR_PAIR(3));
                                     mvprintw(y, 44, "%s", opt_item.drec);
                                     attroff(A_BOLD | COLOR_PAIR(3));
    
                                     if(remove("../stats/records.hgn") == -1)
                                     {
                                              attron(A_BOLD);
                                              delete_status_bar(16, 6, 50, ' ');
    			                              mvprintw(16, 6, "Can't delete Record-Table");
    		                                  attron(A_BOLD);
    			                     }
    
                                     attron(A_BOLD);
                                     delete_status_bar(16, 6, 50, ' ');
    			                     mvprintw(16, 6, "Record-Table was deleted!");
    			                     attron(A_BOLD);
                         }
                         else if(strcmp(opt_item.drec, "no") == 0)
                         {
                                     delete_status_bar(y, 44, 24, ' ');
                                     attron(A_BOLD | COLOR_PAIR(3));
                                     mvprintw(y, 44, "%s", opt_item.drec);
                                     attroff(A_BOLD | COLOR_PAIR(3));
    
                                     attron(A_BOLD);
                                     delete_status_bar(16, 6, 50, ' ');
                                     mvprintw(16, 6, "Record-Table was not changed!");
                                     attron(A_BOLD);
                         }
                         else
                         {           
                                     delete_status_bar(y, 44, 24, ' ');
                                     mvprintw(y, 44, "<yes/no>");
                                     attron(A_BOLD);
                                     delete_status_bar(16, 6, 50, ' ');
                                     mvprintw(16, 6, "This was not a valid input!");
                                     attroff(A_BOLD);
                         }          
                    }
        			else if(y == 9)
                    {
                         /* quick and dirty */
                         make_color_chars(y, 44, 23, ' ');
    			         attron(A_BOLD | COLOR_PAIR(2));
                         mvgetnstr(y, 44, opt_item.dict, PATH_SIZE);
                         attroff(A_BOLD | COLOR_PAIR(2));
    
                         delete_status_bar(y, 44, 24, ' ');
    
                         attron(A_BOLD | COLOR_PAIR(3));
                         mvprintw(y, 44, "%s",opt_item.dict);
                         attroff(A_BOLD | COLOR_PAIR(3));
    
                         attron(A_BOLD);
                         delete_status_bar(16, 6, 50, ' ');
                         mvprintw(16, 6, "New dictonary was selected!");
                         attron(A_BOLD);
                    }
                    else if(y == 8)
                    {    /* quick and dirty */
                         make_color_chars(y, 44, 23, ' ');
                         attron(A_BOLD | COLOR_PAIR(2));
    
                         mvgetnstr(y, 44, stat_item.name, NAME_SIZE);
    
                         if(stat_item.name[0] == '\0')
                         {
                               attron(A_BOLD);
                               delete_status_bar(16, 6, 50, ' ');
                               mvprintw(16, 6, "No name was defined!");
                               attron(A_BOLD);
                         }
    
                         attroff(A_BOLD | COLOR_PAIR(2));
    
                         delete_status_bar(y, 44, 24, ' ');
    
                         attron(A_BOLD | COLOR_PAIR(3));
                         mvprintw(y, 44, "%s",stat_item.name);
                         attroff(A_BOLD | COLOR_PAIR(3));
    
                         attron(A_BOLD);
                         delete_status_bar(16, 6, 50, ' ');
                         mvprintw(16, 6, "Your name was changed!");
                         attron(A_BOLD);
                    }
    			break;
    
    			case ESCAPE: hangman_game_menu();
    		}
    		attron(A_BOLD | COLOR_PAIR(3));
            mvprintw(y,6,   "->");
            attroff(A_BOLD | COLOR_PAIR(3));
    	}
    }
    
    int hangman_game_menu(void)
    {
        unsigned short int i = 1, y;
        char input;
    
        noecho();
     	clear();
    
    	hangman_image_box();
    
    	mvprintw(6,6,  "Select your severity:");
    	mvprintw(8,6,  "< > Normal User");
    	mvprintw(9,6,  "< > Average User");
    	mvprintw(10,6, "< > Expert User");
    	mvprintw(11,6, "< > Options");
    	mvprintw(12,6, "< > Records");
    	mvprintw(13,6, "< > Quit");
    
    	attron(A_BOLD | COLOR_PAIR(1));
    	mvhline(15, 6, ACS_HLINE, 63);
    	attroff(A_BOLD | COLOR_PAIR(1));
    
    	for(y = 8; y <= 13; y++)
    	{
            attron(A_BOLD);
    	    mvprintw(y, 7, "%d", i++);
            attroff(A_BOLD);
        }
    
    	for(;;)
    	{
    		switch(input=getch())
    		{
    			case '1' : stat_item.severity = 1; hangman_game_start(0,0); break;
    			case '2' : stat_item.severity = 2; hangman_game_start(0,0); break;
    			case '3' : stat_item.severity = 3; hangman_game_start(0,0); break;
    			case '4' : hangman_game_options(); break;
     			case '5' : hangman_game_records(); break;
     			case '6' : return -1;
    
     			default:
                           delete_status_bar(16, 6, 50, ' ');
                           attron(A_BOLD); 
                           mvprintw(16, 6, "Invalid choice, try again!");
                           attroff(A_BOLD);
                break;
     		}
     	}
    }
    
    int main(int argc, char **argv)
    {
    	initscr();
    
    	if((has_colors()) == FALSE)
    	{
    		endwin();
    		fprintf(stdout, "Your terminal doesn't support colors\n");
    		return -1;
    	}
    	start_color();
    
        init_pair(1, COLOR_CYAN, COLOR_BLACK);
        init_pair(2, COLOR_WHITE, COLOR_CYAN);
        init_pair(3, COLOR_WHITE, COLOR_BLACK);
    
    	clear();
    	curs_set(0);
    
        hangman_image_intro();
    	hangman_image_box();
    
        getch();
    
    	hangman_setting_name();
    
    	if(hangman_game_menu() == -1)
    	{
    		endwin();
    		exit(0);
    	}
    
    	return 0;
    }
    

    Für jede Verbesserung bin ich offen. 🙂



  • Dacht ich mirs doch 😃

    Da liegt dein Fehler:

    ...
    case ESCAPE: hangman_game_menu();
    ...
    

    Wenn du zigtausendmal den Wechsel machst, erfährst du vom OS sogar den Grund, warum es nicht funktioniert. Es wird dir etwas ähnliches liefern wie: "Die maximale Anzahl an Rekursionen wurde erreicht".

    Soll heißen:

    Du rufst in main() die Funktion menu() auf, aus dieser rufst du dann options() auf, allerdings springst du von options() nicht zurück sondern rufst wiederum menu() auf. Das heißt du verschachtelst das immer weiter und weiter.

    Also was musst du in der oben gezeigten Codezeile ändern??



  • case ESCAPE: return;
    

    Hab das jetzt mal im menu ein bischen aufgeteilt, und funktioniert soweit jetzt ganz gut. Danke nochmal.


Anmelden zum Antworten