【发布时间】:2015-04-12 10:08:57
【问题描述】:
谁能解释一下如何将一个函数中的变量的值带到另一个函数中。我知道一旦函数结束,函数中的变量值就会被清除。但是我如何告诉它不要那样做。
我正在尝试将候选人函数中的 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];
// ...
}
【问题讨论】:
-
你应该看看如何将 arguments 传递给函数并使用 return 值。
-
void candidates(int &ppl)和void votes(int const ppl)
标签: c++ function variables dynamic