C++ Schleifen/Switch-Case Problem

Discjoggy

Ensign
Registriert
Aug. 2008
Beiträge
185
Hallo,

bin jetzt fast hier am durchdrehen. Es geht um folgendes:
Ich bin jetzt am Ende angelangt mein keines Spielchen zu programmieren. Grundaufbau sieht halt so aus, dass man als erstes in ein Menü kommt und dort "Spiel starten", "Statistiken" und "Ende" auswählen kann (Punkte erklären sich von selbst).
Hier mal ein schnippsel des Aufrufs des Menüs:

Code:
[FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] choice = -1;[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]while[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4]( choice == -1)[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]{[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] choice = game_menu();[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]switch[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] (choice)[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]{[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff] case[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] 0:[/SIZE][/FONT]
[SIZE=4][FONT=Consolas] choice = 1;[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff] break[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4];[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff] case[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] 1:[/SIZE][/FONT]
[SIZE=4][FONT=Consolas] game_main();[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff] break[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4];[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff] case[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] 2:[/SIZE][/FONT]
[SIZE=4][FONT=Consolas] game_statistic();[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff] break[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4];[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]}[/FONT][/SIZE]
[SIZE=4][FONT=Consolas]}[/FONT][/SIZE]
[/SIZE][/FONT]

So jetzt das Kuriose... wie Ihr seht geht er immer erst falls die Bedingung choice=-1 noch erfüllt ist in das game_menu(), dort wird das eigentliche Menü gezeigt und der returnwert ist von 0 bis 2. Wenn ich nun 0 in game_menu() zurückgebe wird ja eigentlich in den case Punkt 0 reingesprungen und choice auf 1 gesetzt, was das Programm doch beenden sollte.

Nun hab ich x mal den Debugger durchlaufen lassen und immer am Ende der While-Schleife ist plötzlich choice wieder -1

Habt ihr eine Antwort?

-edit:
Habe vergessen zu erwähnen das ich mit Allegro programmiert habe, was aber hier ja eh keinen Einfluss genommen hätte...
 
Zuletzt bearbeitet:
Ganz einfach du hast zwei verschiedene Variablen, die beide "choice" heißen. Eine außerhalb der Schleife (bleibt immer -1) und eine innerhalb (der weißt du den Rückgabewert von "game_menu()" zu.

So sollte es gehen:
Code:
int choice = -1;
while( choice == -1)
{
    choice = game_menu(); // Das "int" muss weg!
    switch (choice)
    {
        case 0:
            choice = 1;
            break;
        case 1:
            game_main();
            break;
        case 2:
            game_statistic();
            break;
    }
}
 
Zuletzt bearbeitet: (Format)
Code:
[FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] game_menu()[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]{[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] choice = -1;[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]BITMAP *buffer = create_bitmap(SCREEN_W, SCREEN_H);[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]while[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4](choice == -1)[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]{[/FONT][/SIZE]
[SIZE=4][FONT=Consolas]clear(buffer);[/FONT][/SIZE]
[SIZE=4][FONT=Consolas]paint_button(buffer, 160, 120, 240, 80, makecol(255,0,0), makecol(255,255,255), [/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#a31515][FONT=Consolas][SIZE=4][COLOR=#a31515][FONT=Consolas][SIZE=4][COLOR=#a31515]"Neues Spiel"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4]);[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]paint_button(buffer, 160, 220, 240, 80, makecol(255,0,0), makecol(255,255,255), [/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#a31515][FONT=Consolas][SIZE=4][COLOR=#a31515][FONT=Consolas][SIZE=4][COLOR=#a31515]"Statustik"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4]);[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]paint_button(buffer, 160, 320, 240, 80, makecol(255,0,0), makecol(255,255,255), [/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#a31515][FONT=Consolas][SIZE=4][COLOR=#a31515][FONT=Consolas][SIZE=4][COLOR=#a31515]"Spiel Beenden"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4]);[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]blit(buffer,screen,0,0,0,0,SCREEN_W, SCREEN_H);[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]for[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] i=0; i<3; i++)[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]{[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] (mouse_b & 1 && mouse_x >= 160 && mouse_y >= (i*100)+120 && mouse_x <= 400 && mouse_y <= (i*100)+200)[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]choice = (i+1)%3;[/FONT][/SIZE]
[SIZE=4][FONT=Consolas]}[/FONT][/SIZE]
[SIZE=4][FONT=Consolas]}[/FONT][/SIZE]
[/SIZE][/FONT][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff][FONT=Consolas][SIZE=4][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=4][FONT=Consolas][SIZE=4] choice;[/SIZE][/FONT]
[SIZE=4][FONT=Consolas]}[/FONT][/SIZE]
sorry das Einrücken geht hier schlecht...[/SIZE][/FONT]
Ergänzung ()

TheCadillacMan schrieb:
Ganz einfach du hast zwei verschiedene Variablen, die beide "choice" heißen. Eine außerhalb der Schleife (bleibt immer -1) und eine innerhalb (der weißt du den Rückgabewert von "game_menu()" zu.

So sollte es gehen:
Code:
int choice = -1;
while( choice == -1)
{
choice = game_menu(); // Das "int" muss weg!
switch (choice)
{
 case 0:
 choice = 1;
 break;
 case 1:
 game_main();
 break;
 case 2:
 game_statistic();
 break;
}
}


Oh mein Gott!!!
Du hast recht... wie blind man doch echt manchmal sein kann :D darf ich keinem erzählen ^^
 
Zurück
Oben