so my friend just got into programming and I've been helping him a little bit, he has to do the console game as a school "project" (first grade), well, everything was fine untill we went into more "detailed" versions of options:
1. you enter your name (works fine)
2. you are greeted with message for 4 sec (works fine)
3. game tells you in what room you are, now you have 2 options, go to next room? (Y/N), this WORKS if you use y/Y, n/N, it even gives you error when you enter anything else, BUT when you enter MORE characters, e.g. instead of "y" you do "yes", then it will run the code 3 times but it WILL accept it as a "y" because it has "y" as the first element, so when you say something like "qwe" it will give you error like you entered invalid choice, therefore it gets the first char and works with it, but the console will go thru the code as many times as you entered characters
4. YES option - now it will list you the rooms that you can go to, if you will select ID of EXISTING room, that you ARE ABLE to go to, it will just move you there, if you will enter ID of nonexisting room or the room you're not allowed to go, it will give you error saying that you can't go there, this works fine, HOWEVER there's SAME problem, as with the "y/n" option, therefore when you enter a char (instead of int in this case) it will go to the infinite loop without asking you questions again
so again, if you enter correct values (only 1 char for y/n option, INT for a room option), it works fine, it's getting terribly bugged with array of chars for y/n and any char for room option, and since I personally kinda hate chars (I'm just using strings all the time because of problems like this), I really have no idea what to do in here, and my friend doesn't want to use strings because he kinda knew only if/else yesterday and today he has fully dynamic room creation with room entries and stuff (basically I skipped like 1 month of lectures of basic c++ and included it everything in 1 day)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
while(true) // loop of the functions
{
system("cls");
if(cRoom == 0)
cout << "You can't go to that room!" << endl;
if(cValue == 0)
cout << "Please enter correct value!" << endl;
cout << "You are in room: " << sRoomName[iCurrRoom] << endl; // error message for invalid input
if(iCurrRoom == ROOM_DRAGON) // several structions for room with dragon
ExecuteFight(ROOM_DRAGON);
else if(iCurrRoom == ROOM_KING) // function in room king
ExecuteFight(ROOM_KING);
else if(iCurrRoom >= ROOM_TREASURE1 && iCurrRoom <= ROOM_TREASURE4) // function for random loot
RandomLoot();
char cGoToOther;
cout << "Do you want to go to other room ? (Y/N)\n"; // function for changing rooms
cin >> cGoToOther;
if(cGoToOther == 'y' || cGoToOther == 'Y')
{
system("cls");
ShowPossibleRooms(iCurrRoom);
int iNextRoom;
cin >> iNextRoom;
if(iNextRoom < 0 || iNextRoom >= ROOM_MAX)
{
cRoom = 0;
cValue = 1;
}
else
{
cRoom = GoToRoom(iNextRoom);
cValue = 1;
}
}
else if (cGoToOther == 'n' || cGoToOther == 'N')
return 0;
else
{
cValue = 0;
cRoom = 1;
}
}
|