如何在函数中传递 2 个值?
How can I pass 2 values in a function?
现在我正在尝试将 name 和 argc 传递给投票函数
但是我遇到了一堆错误,argc 不起作用,我该如何传递它?
我有 2 个错误说我在函数调用中有很多参数。
它说它只需要一个参数。
int main(int argc, string argv[]) // I want to pass this argc parameter into my vote function
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: plurality [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
}
int voter_count = get_int("Number of voters: ");
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name, argc)) // it doesn't work here
{
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner();
bool vote(string name, int argc) // and here
{
// TODO
return false;
}
}
答案在最后一个错误中:
plurality.c:23:6: previous declaration is here
bool vote(string name);
您有一个必须修复的原型
现在我正在尝试将 name 和 argc 传递给投票函数
但是我遇到了一堆错误,argc 不起作用,我该如何传递它?
我有 2 个错误说我在函数调用中有很多参数。
它说它只需要一个参数。
int main(int argc, string argv[]) // I want to pass this argc parameter into my vote function
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: plurality [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
}
int voter_count = get_int("Number of voters: ");
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
string name = get_string("Vote: ");
// Check for invalid vote
if (!vote(name, argc)) // it doesn't work here
{
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner();
bool vote(string name, int argc) // and here
{
// TODO
return false;
}
}
答案在最后一个错误中:
plurality.c:23:6: previous declaration is here
bool vote(string name);
您有一个必须修复的原型