mardi 26 septembre 2017

Assist me with a pointer based c++ data structure program please

I'm making a program using a custom data structure with 4 links and 4 variables each. The code is still incomplete, but what I expected from this much is not outputting the result. From what is written, if you choose a as first input, I expected to go ahead and traverse the map without energy depletion. However, all the if conditions are getting satisfied, thus outputting '>' 4 times an not showing the elements after the '>', which, for the first iteration, should be 'b'. Have a look at the code, and please help me out if you get what is glitching out.

#include <iostream>
#include <cstdlib>
using namespace std;
void dogwalk();
class map
{
    public:
    int d1,d2,d3,d4;
    map* w1;
    map* w2;
    map* w3;
    map* w4;
    map()
    {
        d1=0;
        d2=0;
        d3=0;
        d4=0;
    }

}*start,*current;
map* mapbuilder()
{
    map a[7];
    start=&a[0];
    a[0].d1=1;
    a[0].d2=0;
    a[0].d3=0;
    a[0].d4=0;
    a[0].w1=&a[1];
    a[0].w2=&a[0];
    a[0].w3=&a[0];
    a[0].w4=&a[0];
    a[1].d1=1;
    a[1].d2=3;
    a[1].d3=5;
    a[1].d4=3;
    a[1].w1=start;
    a[1].w2=&a[2];
    a[1].w3=&a[5];
    a[1].w4=&a[6];
    a[2].d1=3;
    a[2].d2=4;
    a[2].d3=7;
    a[2].d4=0;
    a[2].w1=&a[1];
    a[2].w2=&a[3];
    a[2].w3=&a[5];
    a[2].w4=&a[2];
    a[3].d1=4;
    a[3].d2=3;
    a[3].d3=6;
    a[3].d4=0;
    a[3].w1=&a[2];
    a[3].w2=&a[3];
    a[3].w3=&a[4];
    a[3].w4=&a[3];
    a[4].d1=6;
    a[4].d2=5;
    a[4].d3=0;
    a[4].d4=0;
    a[4].w1=&a[3];
    a[4].w2=&a[5];
    a[4].w3=&a[4];
    a[4].w4=&a[4];
    a[5].d1=5;
    a[5].d2=3;
    a[5].d3=3;
    a[5].d4=5;
    a[5].w1=&a[4];
    a[5].w2=&a[6];
    a[5].w3=&a[1];
    a[5].w4=&a[2];
    a[6].d1=3;
    a[6].d2=3;
    a[6].d3=0;
    a[6].d4=0;
    a[6].w1=&a[5];
    a[6].w2=&a[1];
    a[6].w3=&a[6];
    a[6].w4=&a[6];
    map* p=&a[0];
    return p;

}
int main()
{
    cout << "So you paid Miss Augustin a visit today. It\'s her 25th Anniversary. Her husband, Zack, is in Dubai,\nand cannot make it to home today.\n\nYou reach her place, and are standing at the doorway.\na>Ring the calling bell\nb>Turn back and go home.\n";
    char c;
    cin >> c;
    if(c=='a')
    {
        cout << "She opens after exactly 32 seconds. You wonder why you were counting the seconds, but find no plausible reason.\nShe thanks you for the bouquet of yellow lillies you got for her, and welcomes you inside. However, after some tea, she tells you how her back is aching like anything, and she needs someone to walk Tommy, her dog, for the day. You and Tommy know each other, and you like him. Being a good guy, you volunteer to help. She says she\'ll treat you with a big slice of blueberry cheesecake that she\'s making today, once you return. You can\'t wait for it. So you put the leash on Tommy, and head towards the door.\n\n";
        dogwalk();
    }
    else if(c=='b')
    {
        cout << "You are a mean person. You don\'t deserve to play this game.\n";
        return 0;
    }
    else
    {
        cout << "Which part of the two choices, a and b, did you not get?";
        main();
    }
    return 0;
}
int getIndex(map* a,map* b)
{
    int x=-1;
    for(int i=0;i<7;i++)
    {
        if ((a+i)==b)
        {
            x=i;
            break;
        }
    }
    return x;
}
int getIndex(char c, string p)
{
    int x=-1;
    for(int i=0;i<7;i++)
    {
        if(p[i]==c)
        {
            x=i;
            break;
        }
    }
    return x;
}
void dogwalk()
{
    int energy=rand()%50+2;
    char places[]={'a','b','c','d','e','f','g'};
    cout << "You\'re standing in the front of her house.\nTommy has " << energy << " energy.\n\n";
    map* a=mapbuilder();
    cout << "The map of the town looks like this:\n";
    cout << "f * * * * * e * * * * * * d"<<endl;
    cout << "*   *   *               *\n";
    cout << "*     *    *        * *\n";
    cout << "*       *     *   *\n";
    cout << "g * * * b * * * c\n";
    cout << "        *\n";
    cout << "        a\n";
    cout << "You start at a\n";
    current=start;
    int i=0,flag=1,dir=0;
    string v;
    while(true)
    {
        v="";
        map* z=&a[0];
        cout << "You can go to the following places: \n";
        if(a[i].w1!=&a[i])
        {
            cout << ">" << places[getIndex(z,a[i].w1)]<< endl;
            v=v+places[getIndex(z,a[i].w1)];
        }
        if(a[i].w2!=&a[i])
        {
            cout << ">" << places[getIndex(z,a[i].w2)]<< endl;
            v=v+places[getIndex(z,a[i].w2)];
        }
        if(a[i].w3!=&a[i])
        {
            cout << ">" << places[getIndex(z,a[i].w3)]<< endl;
            v=v+places[getIndex(z,a[i].w3)];
        }
        if(a[i].w4!=&a[i])
        {
            cout << ">" << places[getIndex(z,a[i].w4)]<< endl;
            v=v+places[getIndex(z,a[i].w4)];
        }
        while(true)
        {
            char x;
            cin >> x;
            if(getIndex(x,v)==-1)
                cout << "Invalid Input. Try again.\n";
            else
            {
                current=&a[getIndex(x,v)];
                cout << "Okay. Now you are at " << x << endl;
            }
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire