[英]CS50 Plurality using voters_count
我认为在我的print_winners
function 中也使用main
function 中的int voter_count
真的很实用。但是由于int voter_count
nt candidates_count
在main
function 之前引入,例如,我无法使用voter_count int
。
当我在main
之前引入int
并在调用main
voters_count
中删除 voters_count 之前的int
时,我的代码可以正常工作,我尝试的所有场景都可以正常工作。
但我知道我们不应该更改main
function 中的代码,即使更改了main
function,我的代码仍然没有通过 check50。
有谁知道为什么我的代码没有通过 check50?
void print_winner(void)
{
for (int c = voter_count; (c > 0); c--)
{
int u = 0;
for (int j = 0; (j < candidate_count); j++)
{
if (candidates[j].votes == c)
{
u++;
printf("%s \n", candidates[j].name);
}
}
if (u != 0)
{
return;
}
}
}
检查响应:
voter_count = get_int("Number of voters: ");
在这里,我通过删除 voter_count 之前的int
来更改main
的voter_count
,因为
//Numver of votes
int voter_count;
我把程序介绍到header文件上面的int
。
C 中的函数可以使用全局变量(但请不要)、局部变量或它们的 arguments。一个 function 中的局部变量不能在另一个中访问。
例如
void foo(void);
int main(void) {
int bar = 42;
foo();
return 0;
}
void foo(void) {
printf("%d\n", bar);
}
这是行不通的。 但是,我们可以将bar
作为参数传递给foo
。
void foo(int bar);
int main(void) {
int bar = 42;
foo(bar);
return 0;
}
void foo(int bar) {
printf("%d\n", bar);
}
以下将起作用,但您不应该使用它。
int bar;
void foo(void);
int main(void) {
bar = 42;
foo();
return 0;
}
void foo(void) {
printf("%d\n", bar);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.