passing the value ti a variable of a function to other function

Can someone know how to use the value of this variable a to other function(menu function)?

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
void DisplayshopItem()
{
    int c_item, quantity , a=250, gold=0;;        //250 here is the base value
    char choice;

    stats st;               //default constructor
    itemprice it;

    do
    {
        system("CLS");
        menu:
        cout.width(62); cout<<right<<a<< " Gold";           //i want the value of a is used in other function 
        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.");
        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 Menu()
{
    int menuchoice, b, a=250;
    stats p;

    system("CLS");
    cout<<endl<<endl;
    horizonsmoothline();
    cout<< "|| ";
    centerstring1("HEROES STATS");
    cout.width(31); cout<<right<<"||"<<endl;
    horizonsmoothline();
    cout<< "|| NAME:\t"<<pname<<"\t";
    //b= goldresult(b);
    p.currentGold= p.currentGold-b;
    a= p.currentGold;
    cout.width(43); cout<< right<<a-b<<" Gold ||"<<endl;        //to be in here
    cout<< "|| LEVEL:\t"<<p.level<< "\t\t";
    cout.width(43); cout<< right<<"   ||"<<endl;
    cout<< "|| EXP:\t\t"<<p.exp<< "\t\t";
    cout.width(43); cout<< right<<"   ||"<<endl;
    cout<< "|| ATTCK:\t"<<p.ATTK<< "\t\t";
    cout.width(43); cout<< right<<"   ||"<<endl;
    cout<< "|| HP:\t\t"<< p.HP<< "\t\t";
    cout.width(43); cout<< right<<"   ||"<<endl;
    cout<< "|| MP\t\t"<<p.MP<< "\t\t";
    cout.width(43); cout<< right<<"   ||"<<endl;
    horizonsmoothline();
    cout<<endl<<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;
    }
}
Last edited on
Hello Vinz24,

I just found this. I am not sure what you want to do, but this might help.

In the function "void DisplayshopItem()" you define "int a = 250" and want to pass this to say the "Menu()" function. At some point when you call "Menu();" you would have to use "Menu(a);". This will pass a value of "a" to "Menu" then when you define "void DisplayshopItem()" it would be "void DisplayshopItem(int a)" and now you could use "a" in the "DisplayshopItem" function, but this is only a copy of "a"'s value and any change to a is not reflected in the calling function. You could define "void DisplayshopItem(int a)" as "void DisplayshopItem(int& a)" where the "&" means pass by reference and any change in the "DisplayshopItem" function will be reflected back in the calling function.

The other option is to return the value of "a" back to the calling function and this would be "int DisplayshopItem(int a)" and in the calling function it would be "a = Menu(a);". based on what you have shown, but the concept could be used anywhere in the program.

Give this a read for a different explaination: http://www.cplusplus.com/doc/tutorial/functions/

Hope that helps,

Andy
Topic archived. No new replies allowed.