Is this solution cumbersome?

Sep 12, 2014 at 1:05am
I have tiles that each four descriptions for leaving the room in that direction, for example if you walked north from x y, show that description. I am trying to load the descriptions from files in an organized manner. I will be adding try/catch blocks for opening the files at a later point. Is this solution here too cumbersome or is there a better way to go about it?

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
#include "gameworld.h"
#include <iostream>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;


gameworld::gameworld()
{
    std::ifstream north_file;
    std::ifstream east_file;
    std::ifstream south_file;
    std::ifstream west_file;
    north_file.open("DirectionDescriptions/North.txt");
    east_file.open("DirectionDescriptions/East.txt");
    south_file.open("DirectionDescriptions/South.txt");
    west_file.open("DirectionDescriptions/West.txt");
    string input;
    int x = 0;
    int y = 0;

    while (!north_file.eof())
    {
        std::getline(north_file, input);
        world[x][y].set_description(North, input);
        if ( y < boundary )
        {
            y++;
        }
        else
        {
            x++;
            y = 0;
        }
    }
    x = 0, y = 0;
    north_file.close();

    while (!east_file.eof())
    {
        std::getline(east_file, input);
        world[x][y].set_description(East, input);
        if ( y < boundary )
        {
            y++;
        }
        else
        {
            x++;
            y = 0;
        }
    }
    x = 0, y = 0;
    east_file.close();

    while (!south_file.eof())
    {
        std::getline(south_file, input);
        world[x][y].set_description(South, input);
        if ( y < boundary )
        {
            y++;
        }
        else
        {
            x++;
            y = 0;
        }
    }
    x = 0, y = 0;
    south_file.close();

    while (!west_file.eof())
    {
        std::getline(west_file, input);
        world[x][y].set_description(West, input);
        if ( y < boundary )
        {
            y++;
        }
        else
        {
            x++;
            y = 0;
        }
    }
    west_file.close();
}
Sep 12, 2014 at 1:53am
Yes.

How much information is in the files? If it's not very much, you could include them directly in the code and you wouldn't have to worry about them being missing or not opening for some reason. Though I guess you might want to be able to change the files without recompiling?

If you don't want to or can't put the files directly in the code, this will work better:
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
#include "gameworld.h"
#include <fstream>
#include <string>


void gameworld::gameworld ( )
{
    setDescription ( "North" );

    setDescription ( "East" );

    setDescription ( "South" );

    setDescription ( "West" );
}


// I made it a bool so you can check if the file opened.
bool gameworld::setDescription ( const std::string & direction )
{
    std::ifstream fin ( "DirectionDescriptions/" + direction + ".txt" );


    if ( fin ) // Is the file open?
    {
        std::string input;

        unsigned x = 0, y = 0;


        while ( getline ( fin, input ) )
        { // I don't know what this function takes, so you probably have a better way to do it.
            world[x][y].set_description ( direction, input );

            if ( y < boundary ) ++y;

            else
            {
                ++x;

                y = 0;
            }
        }


        return true;
    }


    return false; // File could not be opened.
}
Last edited on Sep 12, 2014 at 1:56am
Sep 12, 2014 at 2:03am
That is clever, a bit more organized. Thank you.
Sep 12, 2014 at 2:28am
This gives me an error.
std::ifstream fin ( "DirectionDescriptions/" + direction + ".txt" );
Sep 12, 2014 at 2:36am
How about:
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
bool gameworld::setDescription ( const std::string & direction )
{
    const std::string file_name = "DirectionDescriptions/" + direction + ".txt";

    std::ifstream fin ( file_name );


    if ( fin ) // Is the file open?
    {
        std::string input;

        unsigned x = 0, y = 0;


        while ( getline ( fin, input ) )
        { // I don't know what this function takes, so you probably have a better way to do it.
            world[x][y].set_description ( direction, input );

            if ( y < boundary ) ++y;

            else
            {
                ++x;

                y = 0;
            }
        }


        return true;
    }


    return false; // File could not be opened.
}
Sep 12, 2014 at 2:37am
I figured it out, had to do file_name.c_str()
Last edited on Sep 12, 2014 at 2:42am
Sep 12, 2014 at 3:06am
That was a change between C++03 and C++11 - in C++11 the file stream constructors now accept a std::string.
Sep 12, 2014 at 3:27am
So it should work? I'm using codeblocks.
Last edited on Sep 12, 2014 at 3:27am
Sep 12, 2014 at 3:37am
C++11 is often not enabled by default, you need to enable it for your compiler (often with the -std=c++11 flag)
Topic archived. No new replies allowed.