【问题标题】:Function not updating an int field函数不更新 int 字段
【发布时间】:2014-09-04 20:52:29
【问题描述】:

除了我的 SPOILED votes int 保持在 0 并且没有被函数更新之外,一切似乎都运行良好。我尝试添加一个指针,认为它会指向它所执行的函数之外的对象,但 ++ 函数没有更新

#include <stdio.h>

struct candidates
{
 char name[20];
 int votes;
};
struct candidates electionCandidates[7];

void Initialize(struct candidates EC[]);
void Processvotes(struct candidates EC[], int *BadVote);
void printResults(struct candidates EC[], int *BadVote);

int main()

{

    int i, SPOIL = 0;

    Initialize(electionCandidates);
    Processvotes(electionCandidates, &SPOIL);
    printResults(electionCandidates, &SPOIL);

    for(i = 0; i < 7; i++)
    {
        printf("%s",electionCandidates[i].name);
        printf("%d\n\n",electionCandidates[i].votes);
    }

    printf("%d", SPOIL);

}

void Initialize(struct candidates EC[])
{
    int i;

    FILE *fp;
    fp = fopen("elections.txt", "r");

    for (i = 0; i < 7; i++)

    {
        fgets(EC[i].name, 20, (FILE*)fp);
    }

    fclose(fp);
}


void Processvotes(struct candidates EC[], int *BadVote)
{
        int TVOTE, i;

        FILE *fp;
        fp = fopen("elections.txt", "r");

        for (i = 0; i < 7; i++)
        {
            fgets(EC[i].name, 20, (FILE*)fp);
        }

        for (i = 0; i < 365; i++)
        {
            fscanf(fp, "%d", &TVOTE);

            if (TVOTE == 1)
                EC[0].votes++;
            if (TVOTE == 2)
                EC[1].votes++;
            if (TVOTE == 3)
                EC[2].votes++;
            if (TVOTE == 4)
                EC[3].votes++;
            if (TVOTE == 5)
                EC[4].votes++;
            if (TVOTE == 6)
                EC[5].votes++;
            if (TVOTE == 7)
                EC[6].votes++;

            if (TVOTE < 1 || TVOTE > 7)
                *BadVote++;
        }

        fclose(fp);
}

void printResults(struct candidates EC[], int *BadVote)
{

    int i, Win = 0, WinSCORE = 0, Runner = 0, RunnerSCORE = 0;

    for (i = 0; i < 7; i++)
    {

        if (EC[i].votes > WinSCORE)
        {
            WinSCORE = EC[i].votes;
            Win = i;
        }

        if (EC[i].votes == WinSCORE)
        {
            RunnerSCORE = EC[i].votes;
            Runner = i;
        }
    }


    if (WinSCORE == RunnerSCORE)
    {
        printf("There was a tie between %s and %s who both got a total of %d votes each. There were %d spoiled votes\n", EC[Win].name, EC[Runner].name, WinSCORE, *BadVote);
    }

    else
        printf("%s won the election with a total of %d votes. There was a total of %d spoiled votes.\n", EC[Win].name, WinSCORE, *BadVote);


}

我们将不胜感激。

【问题讨论】:

  • 忽略 main 中的循环,使用 printf 语句进行名称投票,破坏者正在检查值是什么。
  • 使用 switch case 而不是这么长的一系列 if 和 for 7 可以使用默认大小写。连同建议的 (*BadVotes)++;

标签: c


【解决方案1】:

后缀++ 运算符优先于*;当您编写*BadVote++ 时,您实际上是在递增指针,而不是指向的值。你应该写(*BadVote)++。请参阅this question 了解更多信息。

【讨论】:

  • 哦,好的。总是那么简单。非常感谢。
【解决方案2】:

我很确定这是运算符优先级的问题。尝试这个: 如果 (TVOTE 7) (*坏投票)++;

【讨论】:

    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多