有人可以请解释如何将一个函数中的变量的值带入另一个函数。我知道,只要函数结束,函数中的变量值就会被清除。但我怎么告诉它不要这样做。如何将变量的值从一个函数带到另一个函数?
我正在尝试将候选函数中的int ppl;
值带入投票函数。但是,正如你所看到的,它不会自己做。
void candidates()
{
int ppl;
cout << "Hello please how many candidates are there?: ";
std::cin >> ppl;
string *cans = new string[ppl];
for (int i = 0; i < ppl; i++)
{
cout << "Please type in the last name of candidate number: " << i + 1 << endl;
cin >> cans[i];cout << endl << endl;
}
for (int a = 0; a < ppl; a++)
{
cout << cans[a] << endl << endl;
}
}
void votes()
{
int *vote = new int[ppl];
// ...
}
查看返回值和参考变量。 [在你学习的时候,这个网站应该会有很多帮助!](http://www.learncpp.com/) – Conduit 2015-02-11 21:09:13
你应该看看如何将**参数**传递给函数并使用**返回**值。 – CoryKramer 2015-02-11 21:09:15
'void candidates(int&ppl)'和'void votes(int const ppl)' – 101010 2015-02-11 21:10:49