when I read in the file for exits, for some reason it skip the exact same amount as num_rooms
I know there a bug somewhere after finished calling the the function read_rooms and going into next iteration of while(input.good())
const int MAX_ROOMS = 50;
const int MAX_EXITS = MAX_ROOMS * 4
std::ifstream input;
int read_world(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms, bool exits[MAX_EXITS],
int &num_exits);
int read_rooms(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms);
int read_exits(std::ifstream &input, bool exits[], int &num_exits);
int main() {
char fileName[26];
std::cout<<"filename::";
std::cin.getline(fileName, 26);
input.open(fileName, std::ios::in);
std::string rooms[MAX_ROOMS];
int num_rooms;
bool exits[MAX_EXITS];
int num_exits;
read_world(input,rooms,num_rooms,exits,num_exits);
input.close();
return 0;
};
int read_world(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms, bool exits[MAX_EXITS],
int &num_exits) {
std::string fnCaller;
while (!input.eof()) {
getline(input, fnCaller, ' ');// to check which function to call
if (fnCaller == "rooms") {
std::string temp;
getline(input, temp);
num_rooms = atoi(temp.c_str());
read_rooms(input, rooms, num_rooms);
}
getline(input, fnCaller, ' ');
if (fnCaller == "exits") {
std::string temp;
getline(input, temp, ' ');
num_exits = atoi(temp.c_str());
read_exits(input, exits, num_exits);
}
}
};
int read_rooms(std::ifstream &input, std::string rooms[MAX_ROOMS], int &num_rooms) {
for (int i = 0; i < num_rooms; i++) {//get the info
std::string str;
getline(input, str, '\n');
rooms[i] = str;
}
return 0;
};
int read_exits(std::ifstream &input, bool exits[], int &num_exits) {
for (int i = 0; i < num_exits; i++) {//get the info
std::string str;
getline(input, str);
std::cout<<str<<std::endl;
if (str == "locked") {
exits[i] = true;
} else if (str == "unlocked") {
exits[i] = false;
}
}
return 0;
};
this is my file content
rooms 7
front of the house
living room
guest bedroom
closet
hallway
master bedroom
garden
exits 6
locked
locked
unlocked
unlocked
locked
locked