C++ 如果变量位于不同的函数集中,如何对其进行加减?

C++ 如果变量位于不同的函数集中,如何对其进行加减?,c++,C++,我在一个函数(int main)中声明了一个变量,然后在一个函数(void)中创建了一个语句,当我操作该变量时,它的值在函数(int main)中一点也不改变。如果我所做的语句位于不同的函数中,如何操作该变量 这是我的密码 #include <iostream> #include <cstdlib> using namespace std; void shoppingList (int money, int userChoice) { cout<<"1

我在一个函数(int main)中声明了一个变量,然后在一个函数(void)中创建了一个语句,当我操作该变量时,它的值在函数(int main)中一点也不改变。如果我所做的语句位于不同的函数中,如何操作该变量

这是我的密码

#include <iostream>
#include <cstdlib>
using namespace std;

void shoppingList (int money, int userChoice)
{
   cout<<"1. Apples for 20$"
       <<"\n2. Oranges for 30$"
       <<"\n\nChoice: ";
   cin>>userChoice;

       if (userChoice == 1)
       {
           money = money - 20;
           cout<<"20$ had been deducted ";
           cout<<"\nMoney: "<<money<<endl;
       }
       if (userChoice == 2)
       {
           money = money - 30;
           cout<<"30$ had been deducted ";
           cout<<"\nMoney: "<<money<<endl;
       }
}

int main()
{
   int userChoice;
   int money = 500;
   while (true)
   {
       char choices[2];
       cout<<"Money: "<<money<<"\n\n"<<endl;
       cout<<"1. Draw some Numbers"
           <<"\n2. Exit"
           <<"\n\nChoice: ";
       cin>>choices;

       if (choices[0] == '1')
       {
           system ("CLS");
           shoppingList (money ,userChoice);
           system ("PAUSE");
           system ("CLS");
       }

       else if (choices[0] == '2')
       {
           return 0;
       }

       else if (choices[0] > '2' || choices[0] < '1')
       {
       cout<<"\nInvalid Input\nPLs try again"<<endl;
       system ("PAUSE");
       system ("CLS");
       }

       else
       {
       cout<<"\nInvalid Input\nPLs try again"<<endl;
       system ("PAUSE");
       system ("CLS");
       }
   }
}
#包括
#包括
使用名称空间std;
无效购物列表(int money,int userChoice)
{

cout以下是一些观察结果

案例1:在下面的快照中
money
shoppingList()
中的
userChoice
具有局部作用域,因此该变量中的任何更改都不会反映在调用方法中

void shoppingList (int money, int userChoice) { /* catch by value */
}
int main(void) {

    shoppingList (money ,userChoice);

    return 0;
}
如果我所做的语句位于不同的函数中,如何操作该变量?请使用pass by引用变量,而不是pass by value

/* catch by references */
void shoppingList (int &money, int &userChoice) { /* this money is reference of money 
                                                    declared in main(), so any change
                                                    with money in this API will reflect
                                                    in main() */ 

}
int main(void) {
     shoppingList (money ,userChoice);
     return 0;
}

在上述情况下,从
main()
函数中,您使用参考变量传递
money
&捕获,即
购物列表中没有为
money
创建新内存()
即两者具有相同的内存位置,因此如果您在
shoppingList()
中对
money
userchoice
进行任何更改,方法将在
main()
函数中得到反映。

每个变量都有一个“范围”,并且只有在其范围内,一个变量才是“可见的”。您可以将此类变量设置为全局变量,或将其传递给带有引用/指针的函数。查看(了解)有关引用(甚至指针)的信息C中的函数是按值传递的,这意味着您复制了值并将其传递给函数。因此,您对后者所做的更改将丢失给前者。要修改前者,您必须按指针或引用传递。@ImagePowers您需要按引用传递参数,而不是按值传递参数,否则它们的值将被复制,而C离开shoppingList()后,更改将丢失。将shoppingList()声明为
void shoppingList(int&money,int&userChoice)
(注意
&
字符),所有操作都应按预期进行。