Hey, I'm pretty new at this and I have this maze game I'm making on C++, and every time I try to run it whilst debugging, the same 3 problems come up with the enemies I've implemented.
The error message states; 'Run-Time Check Failure #3 - The variable 'Enemy1' is being used without being initialized.'
it does the same for Enemy2 and Enemy3.
i was wondering if anyone could tell me what I'm doing wrong?
the code is as follows;
#include <iostream>
#include <cstdlib>
//#include <ctime>
using namespace std;
if (Map[Enemy3SpawnPointX][Enemy3SpawnPointY] != '#') {
if (Map[Enemy3SpawnPointX][Enemy3SpawnPointY] != 'O') {
Map[Enemy3SpawnPointY][Enemy3SpawnPointX] = enemy3;
}
}
return 0;
}
int PlayerDirection() {
for (;;) {
cin >> playerMovement;
switch (playerMovement) {
case 'w':
if (Map[playerPositionX - 1][playerPositionY] != '#') {
if (Map[playerPositionX - 1][playerPositionY] != enemy2) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionX--;
}
}
if (Map[playerPositionX][playerPositionY] == enemy3) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionX = 1;
playerPositionY = 1;
}
if (Map[playerPositionX][playerPositionY] == enemy1) {
Map[playerPositionX][playerPositionY] = ' ';
for (;;) {
cout << "\n You Lose" << endl;
}
}
break;
case 's':
if (Map[playerPositionX + 1][playerPositionY] != '#') {
if (Map[playerPositionX + 1][playerPositionY] != enemy2) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionX++;
}
}
if (Map[playerPositionX][playerPositionY] == enemy3) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionX = 1;
playerPositionY = 1;
}
if (Map[playerPositionX][playerPositionY] == enemy3) {
Map[playerPositionX][playerPositionY] = ' ';
for (;;) {
cout << "\n You Lose" << endl;
}
}
break;
case 'a':
if (Map[playerPositionX][playerPositionY - 1] != '#') {
if (Map[playerPositionX][playerPositionY - 1] != enemy2) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionY--;
}
}
if (Map[playerPositionX][playerPositionY] == enemy3) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionX = 1;
playerPositionY = 1;
}
if (Map[playerPositionX][playerPositionY] == enemy1) {
Map[playerPositionX][playerPositionY] = ' ';
for (;;) {
cout << "You Lose" << endl;
}
}
break;
case 'd':
if (Map[playerPositionX][playerPositionY - 1] != '#') {
if (Map[playerPositionX][playerPositionY - 1] != enemy2) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionY--;
}
}
if (Map[playerPositionX][playerPositionY] == enemy3) {
Map[playerPositionX][playerPositionY] = ' ';
playerPositionX = 1;
playerPositionX = 1;
}
if (Map[playerPositionX][playerPositionY] == enemy1) {
Map[playerPositionX][playerPositionY] = ' ';
for (;;) {
cout << "\n You Lose" << endl;
}
}
break;
default:
cout << "You Pressed a Wrong Button, Try again" << endl;
break;
}
cout << SpawnEnemy1();
cout << SpawnEnemy2();
cout << SpawnEnemy3();
refreshMap();
}
return 0;
}
int main() {
cout << "\n Welcome to Treasure Hunt, where you must navigate a maze to find the 'X' Marking the burried treasure!" << endl;
cout << "the controls are are follows;" << endl;
cout << " W = UP\n A = LEFT\n D = RIGHT\n S = DOWN" << endl;
cout << "Watch out for enemy tiles. There are three different types you need to avoid:" << endl;
cout << "(B) Who will block your path" << endl;
cout << "(K) Who will kill you and the game will end" << endl;
cout << "(R) Who will send you back to the beginning" << endl;
cout << "Press any directional key to start (W,A,S,D)" << endl;
main.cpp(58): warning C4700: uninitialized local variable 'Enemy1' used
main.cpp(77): warning C4700: uninitialized local variable 'Enemy2' used
main.cpp(96): warning C4700: uninitialized local variable 'Enemy3' used
It means that the variables have a random value. Random values can lead to problems that are difficult to find.