need help in getting the value of a varriable in 1 function ro be used in 3 other function

i am making a turn-based rpg game with inventory. i want that in main page of the game(option to start the game, go to shop, credits and exit the game) will show the amount of gold, in the game interface be show also and in the shop. But when i dont know how to do 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<iostream>
#include<cstdlib>
#include <stdlib.h>
#include<ctime>
#include<windows.h>
#include<math.h>
void Menu();
using namespace std;

class stats
{
public:
	int HP, MP, ATTK, Mhp, Mattk, Distance, Bhp, Bmp, Battk, currentGold, level, exp;

	stats()
	{
		HP = 10;            //base hp
		MP = 5;             //base mp
		ATTK = 2;           //base attack
		Mhp = 20;           //base mobs attack
		Mattk = 2;          //base mobs attack
		Distance =10;       //base distance
		Bhp =  100;            //base boss hp
		Bmp = 100;             //base boss mp
        Battk = 4;           //base boss attack
        currentGold = 250;     //base gold 250
        level = 1;              //base level of all including mobs and boss
        exp = 0;                //base experience
	}
};

class itemprice
{
public:
    int HP_Potion, MP_Potion, Attack_Potion, Helmet, Armour, Leggings, Boots, Arms, Sword;

    itemprice()
    {
        HP_Potion = 25;
        MP_Potion = 25;
        Attack_Potion = 30;
        Helmet = 500;
        Armour = 500;
        Leggings = 500;
        Boots = 500;
        Arms = 500;
        Sword = 1000;
    }
};

string pname;

void animate1(string msg1)      //animate the text print by letter
{
    int x;

    for(int x=0; msg1[x] != '\0'; x++)
    {
	    Sleep(90);
        cout <<msg1[x];
        if( msg1[x] == ' ')
        Sleep(50);
        if( msg1[x] == '\n')
        Sleep(1000);
    }
}

void animate2(string name)          //animate the another print by letter
{
    int z;

    for(int z=0; name[z] != '\0'; z++)
    {
	    Sleep(90);
        cout <<name[z];
        if( name[z] == ' ')
        Sleep(50);
    }
}

void centerstring2(string a)
{
    int l=a.length();
    int pos=(int)((30-1)/2);
    for(int i=0;i<pos;i++)
        cout<<" ";
    cout<<a;
}

void centerstring(string s)
{
    int l=s.length();
    int pos=(int)((80-l)/2);
    for(int i=0;i<pos;i++)
        cout<<" ";
    cout<<s;
}

int itemselect(int c_item)
{
    switch(c_item)
    {
    case 1:
        cout<< "HP Potion";
        break;
    case 2:
        cout<< "MP Potion";
        break;
    case 3:
        cout<< "Attack Potion";
        break;
    case 4:
        cout<< "Helmet";
        break;
    case 5:
        cout<< "Armour";
        break;
    case 6:
        cout<< "Leggings";
        break;
    case 7:
        cout<< "Boots";
        break;
    case 8:
        cout<< "Arms";
        break;
    case 9:
        cout<< "Sword";
        break;
    case 0:
        break;
    }
}
Last edited on
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*int selectedItem(int s)
{
    switch(s)
    {
        case 1: s= 1; ret
    }
}*/

int item(int item, int quan)
{
    itemprice it;

    switch(item)
    {
    case 1: item = it.HP_Potion * quan; return item; break;
    case 2: item = it.MP_Potion * quan; return item; break;
    case 3: item = it.Attack_Potion * quan; return item; break;
    case 4: item = it.Helmet * quan; return item; break;
    case 5: item = it.Armour * quan; return item; break;
    case 6: item = it.Leggings * quan; return item; break;
    case 7: item = it.Boots * quan; return item; break;
    case 8: item = it.Arms * quan; return item; break;
    case 9: item = it.Sword * quan; return item; break;
    case 0: break;
    }
}

/*int goldresult(int a)
{
    stats p;

    p.currentGold= p.currentGold-item()
    return p.currentGold;
}*/

void DisplayshopItem()
{
    int c_item, quantity , a=250, gold=0;;
    char choice;

    stats st;               //default constructor
    itemprice it;

    do
    {
        system("CLS");
        menu:
        cout.width(62); cout<<right<<a<< " Gold";
        cout<<endl<<endl<<endl;
        centerstring("0====================ITEM SHOP====================0");
        cout<<endl<<endl<<endl;
        centerstring2("1. HP Potion------------------------------- ");
        cout<<it.HP_Potion;
        cout<< " Gold"<<endl;
        centerstring2("2. MP Potion------------------------------- ");
        cout<<it.MP_Potion;
        cout<< " Gold"<<endl;
        centerstring2("3. ATTACK Potion--------------------------- ");
        cout<<it.Attack_Potion;
        cout<< " Gold"<<endl;
        centerstring2("4. Helmet---------------------------------- ");
        cout<<it.Helmet;
        cout<< " Gold"<<endl;
        centerstring2("5. Armour---------------------------------- ");
        cout<<it.Armour;
        cout<< " Gold"<<endl;
        centerstring2("6. Leggings-------------------------------- ");
        cout<<it.Leggings;
        cout<< " Gold"<<endl;
        centerstring2("7. Boots----------------------------------- ");
        cout<<it.Boots;
        cout<< " Gold"<<endl;
        centerstring2("8. Arms------------------------------------ ");
        cout<<it.Arms;
        cout<< " Gold"<<endl;
        centerstring2("9. Sword----------------------------------- ");
        cout<<it.Sword;
        cout<< " Gold"<<endl;
        centerstring2("0. Exit from the shop");
        cout<<endl;

        while(!(cin>>c_item))
        {
            animate2("Please choose an item that has in the Item Shop only: ");
            system("PAUSE");
            system("CLS");
            cin.clear();
            cin.ignore(10000, '\n');
        }
        if(c_item==0)
        {
            Menu();
        }
        animate2("You choose ");
        itemselect(c_item);
        cout<<endl;
        animate2("How many ");
        itemselect(c_item);
        animate2(" do you want?");
        cout<<endl;
        cin>>quantity;

        animate2("You spent ");
        gold= item(c_item, quantity);
        cout<<gold;
        animate2(" gold and you have ");
        st.currentGold= st.currentGold-gold;
        a= st.currentGold;
        cout<< a;
        animate2(" gold left.");
        //goldresult(item(c_item, quantity));
        cout<<endl<<endl<<endl;
        if(c_item>=4 || c_item==9)
        {
            requip:
            animate2("You hsve a new equipment. Do you want to equip it now?(y/n) ");
            cout<<endl<<endl;
            itemselect(c_item);
            char equip;
            cin>>equip;
            if(equip=='y' || equip=='Y')
            {
                animate2("Congratulation! You have equipped \n");
                cout<<endl;
                itemselect(c_item);
            }
            else if(equip=='n' || equip=='N')
            {

            }
            else if(equip!='y' || equip!='Y' || equip!='n' || equip!='N')
            {
                goto requip;
            }
        }
        again:
        animate2("\nDo you want to buy again items from shop?(y/n)");
        cout<<endl<<endl;
        cin>>choice;
        if(choice=='n' || choice=='N')
        {
            Menu();
        }
        else if(a<25 || a<=0 && choice=='y' || choice=='Y')
        {
            system("CLS");
            cout<<endl;
            cout<< "You do not have enough gold!"<<endl;
            system("PAUSE");
            system("CLS");
            goto menu;
            Menu();
        }
        st.currentGold-gold;
    }
    while(choice=='y' || choice=='Y');
}

void credits()
{
    system("CLS");
    cout<<endl<<endl;
    cout<< "\t\t";
    for(int c=0; c<37; c++)
    {
        cout<< "-";
        if(c==18)
        {
            cout<< "Credits";
        }
    }
    cout<<endl;
    cout<<endl<< "\t\t    Created by: Vincent Alcala Tubio"<<endl;
    cout<<endl<< "\tSpecial Thanks to God for giving me the Wisdom and Knowledge"<<endl<<endl;
    system("pause");
    system("CLS");
    Menu();
}

void Exitgame()
{
    system("CLS");
    exit(0);
}

void getcharname()
{
    system("CLS");
    cout<< "\t\t\t\t";
    animate1("Welcome Crusader! \n");
    cout<<endl;
    cout<< "\t\t\t\t";
    animate1("What is your name? \n");
    cout<<endl<<endl;
    againname:
    animate2("Please Enter Your Name: \n");
    cin>>pname;
    cout<<endl<< "\nHi! "<<pname;
    system("PAUSE");
    system("CLS");
}

void Menu()
{
    int menuchoice, b, a=250;
    stats p;

    system("CLS");
    cout<< "NAME: "<<pname;
    //b= goldresult(b);
    p.currentGold= p.currentGold-b;
    a= p.currentGold;
    cout.width(40); cout<< right<<a-b<<" Gold"<<endl;
    cout<< "LEVEL: "<<p.level<<endl;
    cout<<"HP: "<< p.HP<<endl;
    cout<< "MP: "<<p.MP<<endl;
    centerstring("Enter Your choice: ");
    cout<<endl<<endl;
    centerstring("1. Start The Game");
    cout<<endl;
    centerstring("2. Go To Shop     ");
    cout<<endl;
    centerstring("3. Credit        ");
    cout<<endl;
    centerstring("4. Exit          ");
    cout<<endl;

    cin>>menuchoice;

    switch(menuchoice)
    {
    case 1:
        //game_interface();
        break;
    case 2:
        DisplayshopItem();
        break;
    case 3:
        credits();
        break;
    case 4:
        Exitgame();
        break;
    }
}


int main()
{
    int gold=
    system("CLS");
    getcharname();
    system("CLS");
    Menu();
}

Last edited on
i want the value of a in line 48 will be the value in line 213
The output should be like this:


                                Welcome Crusader!

                                What is your name?


Please Enter Your Name:
Vincent


Hi! Vincent
Press any key to continue . . .



NAME: Vincent                                -4586806 Gold
LEVEL: 1
HP: 10
MP: 5
                              Enter Your choice:

                               1. Start The Game
                               2. Go To Shop
                               3. Credit
                               4. Exit



instead of this:


NAME: Vincent                                           250 Gold
LEVEL: 1
HP: 10
MP: 5
                              Enter Your choice:

                               1. Start The Game
                               2. Go To Shop
                               3. Credit
                               4. Exit
2




                                                           250 Gold


              0====================ITEM SHOP====================0


              1. HP Potion------------------------------- 25 Gold
              2. MP Potion------------------------------- 25 Gold
              3. ATTACK Potion--------------------------- 30 Gold
              4. Helmet---------------------------------- 500 Gold
              5. Armour---------------------------------- 500 Gold
              6. Leggings-------------------------------- 500 Gold
              7. Boots----------------------------------- 500 Gold
              8. Arms------------------------------------ 500 Gold
              9. Sword----------------------------------- 1000 Gold
              0. Exit from the shop
1
You choose HP Potion
How many HP Potion do you want?
3
You spent 75 gold and you have 175 gold left.



Do you want to buy again items from shop?(y/n)

y

Last edited on
Topic archived. No new replies allowed.