Need Help w/Arrays
Apr 1, 2013 at 5:33pm UTC
Can someone explain to me how arrays are supposed to work? I put them down but all I get is zero when I compile the program. How do I get the actual array produce the numbers and such?
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;
const int ROOMS = 30;
const int MAX_ARROWS = 7;
const int MAX_BATS = 5;
const int MAX_PITS = 5;
/*
game array
[ number of arrows ][ player ][ wumpus ]
*/
int gameArray[3];
/*
rooms array
*/
int roomArray[ROOMS][ROOMS];
/*
bats array
*/
int bats[MAX_BATS];
/*
pits array
*/
int pits[MAX_PITS];
// function prototypes
void instructions();
void menu();
void printMemory();
void setup();
int getRandomRoom();
void createMaze();
void placeWumpus();
void placePlayer();
void placeHazards();
bool checkBats(int );
bool checkNearBats(int );
bool checkPits(int );
bool checkNearPits(int );
bool checkWumpus(int );
bool checkNearWumpus(int );
bool checkPlayer(int );
int main()
{
int choice;
const int INSTRUCTIONS = 1,
BEGIN = 2,
EXIT = 3;
do
{
menu();
cin >> choice;
if (choice == INSTRUCTIONS)
{
instructions();
}
else if (choice == BEGIN)
{
printMemory();
}
else if (choice == EXIT)
{
return 0;
}
else
{
cout << "You MUST choose an option between 1 and 3." << endl;
}
}
while (choice < INSTRUCTIONS || choice > EXIT);
return 0;
}
void instructions()
{
cout << "YOUR MISSION, SHOULD YOU DECIDE TO" << endl;
cout << "ACCEPT IT, IS TO HUINT FOR THE WUMPUS" << endl;
cout << "IN HIS CAVE. TO SUCCEED, YOU MUST SHOOT" << endl;
cout << "IT WITH ONE OF YOUR 7 ARROWS." << endl;
cout << "IF YOU SHOOT INTO A ROOM WHICH IS NOT" << endl;
cout << "DIRECTLY CONNECTED TO YOURS, THE ARROW" << endl;
cout << "WILL BOUNCE TO ONE OF THE ROOMS THAT" << endl;
cout << "DOES CONNECT. THE BATS IN THE CAVE MAY" << endl;
cout << "PICK YOU UP AND PLACE YOU IN A DIFFERENT ROOM" << endl;
cout << "IF YOU ENTER A ROOM WHICH HAS A PIT" << endl;
cout << "YOU WILL FALL INTO IT!" << endl;
cout << "IF THE WUMPUS FINDS YOU OR YOU RUN OUT OF ARROWS " << endl;
cout << "YOU WILL LOSE!!" << endl;
}
void menu()
{
cout << "\n\t\tMain Menu" << endl;
cout << "1. Instructions" << endl;
cout << "2. Begin" << endl;
cout << "3. Exit" << endl;
cout << endl;
cout << "Enter Menu Selection:" << endl;
}
void printMemory()
{
gameArray[3];
cout << gameArray[3] << endl;
roomArray[ROOMS][ROOMS];
cout << roomArray[ROOMS][ROOMS] << endl;
bats[MAX_BATS];
cout << bats[MAX_BATS] << endl;
pits[MAX_PITS];
cout << pits[MAX_PITS] << endl;
}
Last edited on Apr 1, 2013 at 5:47pm UTC
Apr 1, 2013 at 5:48pm UTC
cout << gameArray[3] << endl;
This is out of bounds.
gameArray[3];
This doesn't do anything at all.
This applies to all of your printMemory() function.
There's actually nowhere in your code where you actually do anything to these arrays. You declare them in global space, and that's it.
Topic archived. No new replies allowed.