1

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
  • you should include a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that reproduces your problem. Your code has two `read_world` functions, none is complete '{' are not well balanced. – Franck Nov 01 '16 at 19:11
  • I rechecked my code and fixed everything and I already cut out a lot stuff from my code. – Note Ngernkuakul Nov 01 '16 at 19:51
  • `while (input.good())` has the same problems as [`while(!input.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – molbdnilo Nov 01 '16 at 20:10
  • One problem is that you call `getline(input, fnCaller, ' ');` a second time so if it doesn't equal rooms the first read then the numeric value is read into fnCaller with that second read. Remove the second `getline(input, fnCaller, ' ');` – Jerry Jeremiah Nov 01 '16 at 21:49
  • The other problem is that when you read the size of the exit you delimit with a space but there are no spaces until the next read for fnCaller so the number of exits read into temp is `6\nlocked\nlocked\nunlocked\nunlocked\nlocked\nlocked` so you need to read that number like you do the number of rooms: `getline(input, temp);` – Jerry Jeremiah Nov 01 '16 at 21:56
  • Hopefully I will have time to write a proper answer.... – Jerry Jeremiah Nov 01 '16 at 21:58

1 Answers1

0

There are two things I noticed:

  • One problem is that you call getline(input, fnCaller, ' '); a second time so if it doesn't equal rooms the first read then the numeric value is read into fnCaller with that second read. Remove the second getline(input, fnCaller, ' ');
  • The other problem is that when you read the size of the exit you delimit with a space but there are no spaces until the next read for fnCaller so the number of exits read into temp is 6\nlocked\nlocked\nunlocked\nunlocked\nlocked\nlocked so you need to read that number like you do the number of rooms: getline(input, temp);

Here it is with those two changes:

#include <fstream>
#include <iostream>

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() {
    std::string fileName;
    std::cout<<"filename::";
    std::getline(std::cin,fileName);
    input.open(fileName, std::ios::in);
    if(input) {
        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();
    } else {
        std::cout<<"Error: couldn't read from "<<fileName<<std::endl;
    }
    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 (std::getline(input, fnCaller, ' ')) { // to check which function to call
        std::cout<<"function::"<<fnCaller<<"\n";
        if (fnCaller == "rooms") {
            std::string temp;
            getline(input, temp);
            std::cout<<"num_rooms::"<<temp<<"\n";
            num_rooms = atoi(temp.c_str());
            read_rooms(input, rooms, num_rooms);
        }
        if (fnCaller == "exits") {
            std::string temp;
            std::getline(input, temp);
            std::cout<<"num_exits::"<<temp<<"\n";
            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;
        std::getline(input, str);
        std::cout<<"room["<<i<<"]::"<<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;
        std::getline(input, str);
        std::cout<<"exit["<<i<<"]::"<<str<<"\n";
        if (str == "locked") {
            exits[i] = true;
        } else if (str == "unlocked") {
            exits[i] = false;
        }
    }
    return 0;
};
Jerry Jeremiah
  • 7,408
  • 2
  • 21
  • 27