我正在尝试创建一个函数,该函数将使用用户输入的名称(字符串),并将其与自定义数据类型/结构中的字符串进行比较,以验证用户输入的名称是否存在于内存中。
自定义数据类型设置为全局数据类型,如下所示:
// 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];
不同候选人的名称通过命令行参数存储在数组candidates
中。
之后,通过使用:string name = get_string("Vote: ");
插入候选人的姓名,提示用户投票给候选人。
然后将其用于此函数,以验证候选人的姓名是否存在。
以下是功能:
bool vote(string name)
{
// TODO
int i;
string cn = candidates[i].name;
for (i = 1; i < MAX; i++)
{
int strcmp(cn, name);
}
return strcmp;
}
然而,我不知道这一错误意味着什么:
a parameter list without types is only allowed in a function definition
int strcmp(cn, name);
^
我怎么才能解决这个问题?
发布于 2022-10-29 07:41:31
strcmp()
按字符对两个字符串进行比较。
如果字符串相等,则函数返回0
。
使用strcmp
的简单方法
int returnValue = strcmp(cn, name);
if (returnValue == 0) {
// string matched, so this name exists
}
请查看下面的2个链接以了解更多关于strcmp()
的信息。
另一个要提到的错误是,您试图从循环之外访问ith候选人的名字。
你需要从里面去做。
例如:
for(int i = 0; i < MAX; i++) {
string cn = candidates[i].name;
}
https://stackoverflow.com/questions/74243453
复制相似问题