Cleaning out the main

Hi guys, I have these two routines in my main. I wanted to move them out to help "clean up" the plate of spaghetti I have now. The trouble is, I have no idea how to do it returning the Map and vector to main where I can manipulate the data. I'm not even certain that's the best route to go. I put them in their own functions, but I get tons of type and scope issues.

Any suggestions would be greatly appreciated. I don't think it matters, but it may be helpful to know that over the next two months, the Item class / vector will be expanded to hold about 15 more variables of various types.


Inside the main:

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

 // Read Textfile and place data into a map
     int rnum=0, exitnum[10];
     std:: string rname, shdesc,lgdesc="",dirarray[10]= {"n","ne","e","se","s","sw","w","nw","up","down"};
     bool light, fight,lock, beenhere;
     std::ifstream myfile;
     myfile.open("Roomdata.dat");
     if (myfile.is_open())
     {
        while (myfile.good())
            {
            myfile >> rnum; myfile.ignore(1,'\n');
            getline(myfile,rname,'}'); myfile.ignore(1,'\n');
            getline(myfile,shdesc,'}'); myfile.ignore(1,'\n');
            getline(myfile,lgdesc,'}'); myfile.ignore(1,'\0');
            myfile >> fight; myfile >> lock;
            myfile >> light; myfile >> beenhere;
            myfile.ignore(1,'\0');
            for (int i=0; i<10; i++) 
            {
                myfile >> exitnum[i];
            }
                  Room *rname = new Room(rnum,shdesc,lgdesc,fight,lock,light,beenhere);
            for (int i=0; i<10; i++){
                rname->addExit(dirarray[i],exitnum[i]);
            }

            roomMap.addRoom(rname); // adds room to the map
        }
        myfile.close();
    }
    roomMap.changeRoom(1); // allows movement on the game map



     // Read Item text file and place it into a class / vector of objects

    std :: vector <Item> object;
    std::string name, desc, tag= "";
    int id, cost, loc = 0;
    std::ifstream thefile;
    thefile.open("Itemdata.dat");
    if (thefile.is_open())
        {
            while (thefile.good())
            {
                getline(thefile,name,'}');
                thefile.ignore(1,'\0');
                getline(thefile,tag,'}');
                thefile.ignore(1,'\0');
                getline(thefile,desc,'}');
                thefile.ignore(1,'\0');
                thefile >> id >> cost >> loc;
                thefile.ignore(1,'\0');
                Item newobject (name, tag, desc, id, cost, loc);
                object.push_back(newobject);
                thefile.close();
                }
                }

how to do it returning the Map and vector to main
1) Implement proper move constructors for Map
2)
1
2
return roomMap;
return object;
In your main:
1
2
/*Map*/ auto roomMap = load_rooms("Roomdata.dat");
/*std::vector<Item>*/ auto objects = load_objects("Itemdata.dat");

Thank you MiiNiPaa. I'll read up on auto tomorrow. I appreciate it.
The main idea is to use move semantics (proper move constructor + return by value + RVO) to allow for easy and fast returning. auto is just to comply with DRY — Don't Repeat Yourself — pronciple. You can manually write the types (I commented the out), but there is no need to do that.

Info on auto: http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html
http://en.cppreference.com/w/cpp/language/auto
Last edited on
Topic archived. No new replies allowed.