I'm trying to dynamically allocate information from a text file using a structure.
The console keeps printing out large numbers instead of what I need it to from the text file, so it leaves me thinking that I didn't allocate correctly.
any pointers (hah see what i did there) as to what I'm doing wrong would be greatly appreciated.
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include <sstream>
usingnamespace std;
struct Room
{
string name;
string description;
int exits[4];
};
int main(int argc, char** argv)
{
//input file stream
Room r;
ifstream fin;
string temp;
string rooms;
int count, numRooms;
//checking command line arguments count
if (argc != 2)
{
cout << "Error: Usage, must input only 1 file to read." << endl;
exit(1);
}
fin.open(argv[1]);
//checking if file is open.
if (!fin.is_open())
{
cout << "Error: File is not open" << endl;
return 1;
}
count = 0;
//count the ~'s in the file
while (getline(fin, temp))
{
if(temp == "~")
{
count++;
}
}
numRooms = (count / 3);
//allocate the memory for the rooms
Room* room;
room = new Room[numRooms];
//read the room information from file
while(getline(fin, rooms))
{
getline(fin, room[numRooms].name);
getline(fin, room[numRooms].description);
fin >> room[numRooms].exits[4];
}
for (int i = 0; i < 3; i++){
cout << room[i].name << endl;
cout << room[i].description << endl;
cout << room[i].exits[4] << endl;
}
delete[] room;
return 0;
}
File contents:
Room #0
~
You are at the start. Your journey begins..
~
s 5
~
Room #1
~
You see feces hanging on the wall. What could it mean?
~
s 6
~
Room #2
~
There seem to be some animal droppings in the corner. Gross.
~
e 3
~
Room #3
~
A dirty hall with cockroaches crawling around. Is this hell?
~
w 2
e 4
~
yeah I want to use vectors with this... it would make it much easier but i've been restricted from using them. I'm gonna try this and see what happens. Thanks for the help.
your solution works in printing all of them out but i have to navigate through the file using the cardinal directions so I think I'll have to change up the switch statements or at least index it differently so i can point to the indexes
I'm not sure what you mean. You do realize that this enum
enum { North, South, East, West };
is just a way of naming the indices from 0 to 3, with North being 0 up to West which is 3.
You just need to print the room name and description, then go through the directions North, South, East, West, and for any that aren't NoExit (-1, which is obviously an invalid room index) you print that direction, something like "There are exits: North, East".
Then you have a switch to convert input like 'n', or 'e' back to the enum values (a value between 0 and 3), then you switch the current room to the value of that exit. The current room would of course start at 0. Just do that in a loop to navigate the rooms.
the easy way to convert to text is
enum dirs {north,south,east,west, maxdirs};
vector<string> dirstxt{"north","south","east","west"}; //can use array of string too if banned.
//string alternate[maxdirs] ....
cout << dirstxt[north]; //"north"
to convert from text is more work:
if(input == "north") something = 0;
else if(...
maxdirs lets you iterate normally (for dirs i = north; i < maxdirs; i++)
and if you grow it later to have northeast, southwest, etc 8 directions instead of 4, the above loop still works if north remains the first one and maxdirs the last one (that is, keep a fixed begin and end name and if you modify the middle it will work itself out).
The text names in the code are not able to be directly related to user I/O in enums. They are just integer constants grouped up. There are some tricks you can play with them, but that is all they are and all they can do. (you can set the values, eg north=23, but most enums let the compiler pick the value and it goes 0,1,2,3,... in order).
I don't know who you're responding to, here. Did you see my post?
how that maps to one of the rooms whenever you enter a direction
That's ridiculously simple.
The value of the exits for the chosen direction is the room that it leads to.
If the value is -1 (NoExit) then there is no exit in that direction.
1 2 3 4 5
int nextRoom = rooms[currentRoom].exits[ndir];
if (nextRoom == NoExit)
cout << "There's no exit in that direction.\n";
else
currentRoom = nextRoom;
Yeah I realized that after I sent the last message.
I was talking to you @dutch.
I've mapped the variables but it seems as if the index is off by one whenever I put a direction into the compiler. Southern direction off of room 0 is supposed to take me to room 5 but it takes me to room 6