[Novize] char* Problematik *gelöst*
-
Hallo Leute!
Bin mal wieder bereit aus meinen Fehlern zu lernen. Leider hat 2 Tage Kopf zerbrechen und herumprobieren nicht das gewünschte Ergebnis gebracht
Das macht aber nichts da ich hier immer Top Hilfe erhalte!
Es geht um folgenden Code (Bitte bei der Frage nur auf die Problemstellen reagieren - so schlimm mein Code auch aussieht - Ich lerne
und sobald Fehler auftauchen werd ich diese lösen müssen)
#include <stdio.h> #include <stdlib.h> #include <time.h> void getMovementDir(char); void askMovementKey(char); void Inventory(char*); // void putItemInventory(char*); int randomNumber(); int ITEMCHANCEBLOCK = 0; char USERINPUT; int x = 0; int y = 0; int main() { int ItemChance; int Menu = 0; char* checkitem; printf("\nWelcome to 'Humanity Lost'!\n"); printf("\nPlease choose an option below\n\n"); printf(":: Start Game :: Type '1'\n"); printf(":: Options :: Type '2'\n"); printf(":: Help :: Type '3'\n\n"); do { printf("\nplease Choose: "); scanf("%d",&Menu); }while( Menu != 1 && Menu != 2 && Menu != 3); if (Menu == 1) { do { askMovementKey(USERINPUT); ItemChance = randomNumber(); if (ITEMCHANCEBLOCK == 0) { switch(ItemChance) { case 1: { printf("You found an apple!\n"); checkitem = "apple"; Inventory(checkitem); } case 2: { printf("You found a bread!\n"); checkitem = "bread"; Inventory(checkitem); } } } } while(y <= 50 && y >= -50 && x <= 50 && x >= -50); } else if (Menu == 2) { printf("Options"); } else if (Menu == 3) { printf("Help"); } else { printf("You entered a wrong number"); } return 0; } //************************************************************** int randomNumber() { int random; srand(time(NULL)); random = rand() % 10; return random; } void askMovementKey(char USERINPUT) { printf("\nIn which direction do you want to move now ? "); scanf(" %c)",&USERINPUT); getMovementDir(USERINPUT); // printf("Debug Test Char: %c \n",USERINPUT); return; } void getMovementDir(char movekey) { switch(movekey) { case 'w': if(y < 50) { printf("\nYou wandered 1 mile to the north\n"); y++; ITEMCHANCEBLOCK = 0; } else { printf("You reached a unbreakable wall in the north. Try to move south, west or east.\n"); ITEMCHANCEBLOCK = 1; } break; case 's': if(y > -50) { printf("\nYou wandered 1 mile to the south\n"); y--; ITEMCHANCEBLOCK = 0; } else { printf("You reached a unbreakable wall in the south. Try to move north, west or east.\n"); ITEMCHANCEBLOCK = 1; } break; case 'a': if(x > -50) { printf("\nYou wandered 1 mile to the west\n"); x--; ITEMCHANCEBLOCK = 0; } else { printf("You reached a unbreakable wall in the west. Try to move south, north or east.\n"); ITEMCHANCEBLOCK = 1; } break; case 'd': if(x < 50) { printf("\nYou wandered 1 mile to the east\n"); x++; ITEMCHANCEBLOCK = 0; } else { printf("You reached a unbreakable wall in the east. Try to move south, west or north.\n"); ITEMCHANCEBLOCK = 1; } break; default: printf("Please enter one of the movement keys (w,s,a,d)\n"); } return; } void Inventory(char* item) { static int inventory[8][3]; printf("Debug Items %s\n",item); if( item == "apple") { inventory[0][0] += 1; } else if( item == "bread") { inventory[1][0] += 1; } printf("Apples:%d\n", inventory[0][0]); printf("Bread:%d\n", inventory[1][0]); return; } /* void putItemInventory(char* item) { if(!strcmp(item,"Apfel")) { printf("Apfel + 1 Inventory"); } else { printf("Nothing"); } return; } */
Problembereich:
if (Menu == 1) { do { askMovementKey(USERINPUT); ItemChance = randomNumber(); if (ITEMCHANCEBLOCK == 0) { switch(ItemChance) { case 1: { printf("You found an apple!\n"); checkitem = "apple"; Inventory(checkitem); } case 2: { printf("You found a bread!\n"); checkitem = "bread"; Inventory(checkitem); } } } } while(y <= 50 && y >= -50 && x <= 50 && x >= -50); } else if (Menu == 2) { printf("Options"); } else if (Menu == 3) { printf("Help"); } else { printf("You entered a wrong number"); }
... vvv deklarierte Funktion vvv ...
void Inventory(char* item) { static int inventory[8][3]; printf("Debug Items %s\n",item); if( item == "apple") { inventory[0][0] += 1; } else if( item == "bread") { inventory[1][0] += 1; } printf("Apples:%d\n", inventory[0][0]); printf("Bread:%d\n", inventory[1][0]); return; }
Problem:
Bei der Ausführung funktioniert alles soweit stabil. Ich starte ein neues Spiel und gebe den Buchstaben "w" z.b. ein um nach Norden zu wandern (im Spiel). durch random finde ich per Zufall diverse Gegenstände auf dem Weg. Darunter ein Apfel und ein Brot - "apple" & "bread".
Bei Fund wird der Gegenstand imchar* checkitem
gelagert.
Im Anschluss ruft meine FunktionInventory(checkitem);
den Gegenstand ab und verarbeitet ihn im IF Statement beim Funktionsteil in
Line 6
Anschließend wird im Inventar Array jeweils +1 dazugegeben. Die Rows geben jeden Gegenstand ein
inventory[1][0]
ist hier das "bread" und
inventory[0][0]
ein "apple".
Sobald die Zufallszahl
2
auftritt funktioniert alles. Die Switch Funktion im Problembereich setzt
checkitem = "bread";
und
Inventory(checkitem);
wird ausgeführt.
Nun sieht das Ganze anders aus sobald ein "apple" gefunden wird. Und zwar wie folgt (compiled):
Welcome to 'Humanity Lost'! Please choose an option below :: Start Game :: Type '1' :: Options :: Type '2' :: Help :: Type '3' please Choose: 1 In which direction do you want to move now ? w You wandered 1 mile to the north You found an apple! Debug Items apple Apples:1 Bread:0 You found a bread! Debug Items bread Apples:1 Bread:1 In which direction do you want to move now ?
Wie man sieht wurde Zufallszahl "1" genertiert. Das bedeutet das ein Apfel gefunden wurde. jedoch wird im gleichen Moment auch ein Brot gefunden. Normalerweise dachte ich es liegt daran dass das Brot im Eingabe Stream steckenbleibt. Das ist aber nicht der Fall beim compile da der Apfel zuerst gefunden wurde somit gab es vorher ja kein Brot bzw. es gab keine Zufallszahl "2".
Da es aber keine "2" gab - verstehe ich nicht wie mein Case 2: ausgeführt wurde. Denn dieser erwartet ja eigentlich eine "2" bei switch(ItemChance).Ich weiss ich schreibe sehhhrr viel Zeug - wobei das Problem vielleicht total winzig ist. Ich möchte aber so ausführlich wie nötig sein um möglichst viele Fragen zum Code vorweg beantworten zu haben.
Gruß Charlie!
-
In den case-Zweigen hast du das break vergessen. So wird der erste case-Fall betreten und danach anschließend direkt in den zweiten gewandert (da break fehlt).
-
Werirgend schrieb:
In den case-Zweigen hast du das break vergessen. So wird der erste case-Fall betreten und danach anschließend direkt in den zweiten gewandert (da break fehlt).
hahahha alter ich hau mich gleich um
danke Dir