- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在通过 CS50 执行 PSET3,但在编译代码时遇到问题。它不断出现错误
plurality.c:44:21: error: expected identifier or '('
candidate[i].name = argv[i + 1];
这很奇怪,因为这是在我们开始问题之前给出的代码部分。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.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 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++)
{
candidate[i].name = argv[i + 1];
candidate[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))
{
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(string name)
{
// TODO
// Local boolean variable
bool valid_vote = false;
// Iterating over number of candidates and comparing string "name" to get a boolean result.
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(name, candidate[i].name) == 0)
{
candidate[i].votes++;
valid_vote = true;
// Force a break in the program to exit with the correct result
break;
}
}
return valid_vote;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// Initialising a variable for the most votes, and the candidate
int highest_votes = candidates[0].votes;
string win = candidates[0].name;
// Looping through results and finding the highest "votes" value
for (int i = 0; i < candidate_count; i++)
{
if (candidate[i].votes > highest_votes)
{
highest_votes = candidate[i].votes;
win = candidate[i].name;
}
}
// Comparing all "votes" values and printing winner(s)
for (int j = 0; j < candidate_count; j++)
{
if (candidate[j].name == win)
{
printf("%s %s", win, candidates[j].name);
}
printf("%s", win);
}
return 0;
}
澄清一下,我不需要这个问题的帮助,只是特定的编译错误。
最佳答案
好的,这应该解决它。
您正在使用结构名称 candidate
:
typedef struct
{
string name;
int votes;
} candidate;
// Array of candidates
candidate candidates[MAX];
candidate
而不是
candidates
:
for (int i = 0; i < candidate_count; i++)
{
candidate[i].name = argv[i + 1];
candidate[i].votes = 0;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
}
关于CS50 Pset3 错误 : expected identifier or '(' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62279964/
更新:添加关于 Hashable 的相同错误 我已经创建了一个 Identifiable 兼容协议(protocol)和兼容结构。然后,当我创建列表并在 ForEach 中引用它时,我收到错误 Typ
这只是我偶然发现的例子! 我正在使用 cout与 operator // imports the declaration of std::cout using namespace std; // ma
我有一些表,我使用 MySQL Workbench 创建了 role_has_action 表。 创建的字段是:(role_id,action_id,action_controller_id): (为
我有一个 codesign 无法完全验证的应用程序,因为它“不满足其指定的要求”。第一次检查返回“在磁盘上有效”,所以没关系。 codesign -dvvvv -r- PATH_TO_APP 告诉我要
我正在使用 Java SDK 创建 SAS 来访问 blob。这是代码: SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy(); p
#include "stdafx.h" #include #include #include #include #include using namespace std; #define T
我在代码中看到了这两种方法。你能解释一下这两者有什么区别吗?正如我认为它与 C++ 完成命名空间查找的方式有关,您能否也提供一些相关信息,或者提供一个好的文档的链接?谢谢。 最佳答案 示例: #inc
我一直在使用一个工具 sbconstants从我的 Xcode 项目中的 Storyboard 标识符和重用标识符创建外部常量。 我已将包含这些常量的所有声明的 header #imported 到项
我想知道 bundle Identifier(在 info.plist 中)之间的区别。以及产品 Bundle Identifier(在 Build Setting -> Packaging -> P
我有课Identifier它本质上是 UUID 的类型安全包装器(因此类 Foo 包含 Identifier )。 FooStore类有一个方法 List> bulkReadIdentifiers()
在 Go 中,公共(public)名称以大写字母开头,私有(private)名称以小写字母开头。 我正在编写一个不是库的程序,它是一个单独的包。是否有任何 Go 习语规定我的标识符应该全部公开还是全部
我有一个页面 url,它看起来像: http://mydomain.com/nodes/32/article/new?return=view 安装 tomcat 7 后,尝试访问它时出现此异常: /n
我正在学习以下教程: http://www.appcoda.com/ios7-programming-ibeacons-tutorial/ 但是,我没有使用 iPhone 作为信标,而是使用制造商(R
我在为我的 iPhone 应用程序的下一版本上传 .app 文件时收到此错误“Bundle Identifier differents from prior bundle identifier”。 注
Scene 1, Layer 'script', Frame 1, Line 9 1084: Syntax error: expecting identifier before this. Sc
升级到 Xcode 7 后,我注意到 CFBundleIdentifier 已开始指向在 Build Settings/Packaging 中找到的产品捆绑标识符,而不是 Info.Plist 中的捆
关闭。这个问题需要debugging details .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this question 我在
我使用 Apache DBCP 来获取连接池,我每次都使用 PoolingDataSource 来获取连接。当我向数据库中插入一个对象时,它工作得很好,但是当我尝试从数据库中选择一个元素时,就会出现问
由于我项目的 react-native 版本 (0.44.3),我正在尝试在版本 0.6.4 中安装包 react-native-today-widget,我能够成功安装包: yarn add rea
之前有人问过这个问题,我已经查看了所有其他 stackoverflow 主题的答案,但我无法解决这个问题。 我的应用程序在所有平台的模拟器中运行良好,但是当我在我的设备上运行该应用程序时,我收到错误代
我是一名优秀的程序员,十分优秀!