分段错误(核心转储)和奇怪的问题

问题描述 投票:0回答:1

该代码适用于小数字,但由于某种原因,当我输入大数字并启动程序时,然后写两次名称,第一次程序添加一个,但第二次程序不添加任何内容并告诉我“无效名称”对于大数字,当我开始写其他名称时,它会给我“分段错误(核心转储)”。

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);
int largest(int list[], int n);

int main(int argc, string argv[])
{
    // 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
        vote(name);
        if (strcmp(name, candidates[i].name) != 0)
        {
            printf("Invalid vote.\n");
        }
    }

    // Display winner of election
    print_winner();
}

// Update vote totals given a new vote
bool vote(string NAME)
{
    for(int i = 0; i < candidate_count ;i++)
    {
        if (strcmp(NAME, candidates[i].name) == 0)
        {
            candidates[i].votes++;
        }
    }
    return false;
}

// Print the winner (or winners) of the election
void print_winner(void)
{
    int Votes_here[candidate_count];

    for(int j = 0; j < candidate_count ;j++)
    {
        Votes_here[j] = candidates[j].votes;
    }


    for(int i = 0; i < candidate_count ;i++)
    {
        if(candidates[i].votes == largest(Votes_here, candidate_count))
        {
            printf("%s\n", candidates[i].name);
        }
    }

    return;
}

int largest(int list[], int n)
   {
      int i;
      int l = list[0];
      for(i = 1; i < n; i++)
      {
         if(list[i] > l)
           l = list[i];
      }

      return l;

   }

我希望该程序能够统计选票并正常工作

c cs50
1个回答
0
投票

问题是您使用变量

i
来计算选民作为
candidates
数组的索引。如果选民多于候选人,您将访问数组外部。而且即使不走出数组,比较
strcmp(name, candidates[i].name) != 0
也是没有意义的——为什么第i个选民要投票给第i个候选人?

vote()
函数应返回一个指示符,指示是否找到了候选者。然后您可以在您的
if
声明中检查这一点。

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }

...
// Update vote totals given a new vote
bool vote(string NAME)
{
    for(int i = 0; i < candidate_count ;i++)
    {
        if (strcmp(NAME, candidates[i].name) == 0)
        {
            candidates[i].votes++;
            return true; // vote is valid
        }
    }
    return false; // NAME was not found, vote is invalid
}
© www.soinside.com 2019 - 2024. All rights reserved.