C语言 分段故障(核心转储)和奇怪的问题

3duebb1j  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(89)

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

  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. // Max number of candidates
  5. #define MAX 9
  6. // Candidates have name and vote count
  7. typedef struct
  8. {
  9. string name;
  10. int votes;
  11. }
  12. candidate;
  13. // Array of candidates
  14. candidate candidates[MAX];
  15. // Number of candidates
  16. int candidate_count;
  17. // Function prototypes
  18. bool vote(string name);
  19. void print_winner(void);
  20. int largest(int list[], int n);
  21. int main(int argc, string argv[])
  22. {
  23. // Check for invalid usage
  24. if (argc < 2)
  25. {
  26. printf("Usage: plurality [candidate ...]\n");
  27. return 1;
  28. }
  29. // Populate array of candidates
  30. candidate_count = argc - 1;
  31. if (candidate_count > MAX)
  32. {
  33. printf("Maximum number of candidates is %i\n", MAX);
  34. return 2;
  35. }
  36. for (int i = 0; i < candidate_count; i++)
  37. {
  38. candidates[i].name = argv[i + 1];
  39. candidates[i].votes = 0;
  40. }
  41. int voter_count = get_int("Number of voters: ");
  42. // Loop over all voters
  43. for (int i = 0; i < voter_count; i++)
  44. {
  45. string name = get_string("Vote: ");
  46. // Check for invalid vote
  47. vote(name);
  48. if (strcmp(name, candidates[i].name) != 0)
  49. {
  50. printf("Invalid vote.\n");
  51. }
  52. }
  53. // Display winner of election
  54. print_winner();
  55. }
  56. // Update vote totals given a new vote
  57. bool vote(string NAME)
  58. {
  59. for (int i = 0; i < candidate_count; i++)
  60. {
  61. if (strcmp(NAME, candidates[i].name) == 0)
  62. {
  63. candidates[i].votes++;
  64. }
  65. }
  66. return false;
  67. }
  68. // Print the winner (or winners) of the election
  69. void print_winner(void)
  70. {
  71. int Votes_here[candidate_count];
  72. for (int j = 0; j < candidate_count; j++)
  73. {
  74. Votes_here[j] = candidates[j].votes;
  75. }
  76. for (int i = 0; i < candidate_count; i++)
  77. {
  78. if (candidates[i].votes == largest(Votes_here, candidate_count))
  79. {
  80. printf("%s\n", candidates[i].name);
  81. }
  82. }
  83. return;
  84. }
  85. int largest(int list[], int n)
  86. {
  87. int i;
  88. int l = list[0];
  89. for (i = 1; i < n; i++)
  90. {
  91. if (list[i] > l)
  92. l = list[i];
  93. }
  94. return l;
  95. }

我期待着该程序计算选票,并返回谁拥有最多的选票赢家

zbdgwd5y

zbdgwd5y1#

问题是,您使用的变量i将选民计数作为candidates数组的索引。如果投票者比候选人多,您将访问数组外部。即使你不去数组外,strcmp(name, candidates[i].name) != 0的比较也是没有意义的--为什么第i个投票者要投票给第i个候选人?
vote()函数应该返回一个指示符,指示是否找到了候选对象。然后,您可以在if语句中检查这一点。

  1. // Loop over all voters
  2. for (int i = 0; i < voter_count; i++)
  3. {
  4. string name = get_string("Vote: ");
  5. // Check for invalid vote
  6. if (!vote(name))
  7. {
  8. printf("Invalid vote.\n");
  9. }
  10. }
  11. ...
  12. // Update vote totals given a new vote
  13. bool vote(string NAME)
  14. {
  15. for(int i = 0; i < candidate_count ;i++)
  16. {
  17. if (strcmp(NAME, candidates[i].name) == 0)
  18. {
  19. candidates[i].votes++;
  20. return true; // vote is valid
  21. }
  22. }
  23. return false; // NAME was not found, vote is invalid
  24. }
展开查看全部

相关问题