如何执行索引搜索方法
我试图对字符串执行索引搜索,但收到错误消息:从 const char* 到 int 的转换无效。有人可以解释一下这是什么意思吗?
这是代码
#include <iostream>
#include<string>
#include <stdio.h>
#include <string.h>
using namespace std;
void clist(char fn[],char ln[], int size);
char search_list(const char fn[],const char ln[], int size, string find);
int main(){
string search;
cout << "This program searches a list .\n";
const int total = 3;
char fn[total];
char ln[total];
clist(fn,ln, total);
cout << "Search contact:____ ";
cin >> search;
search_list(fn,ln, total, search);
return 0;
}
void clist(char fn[],char ln[], int size){
cout << "Enter " << size << " contact.\n";
for (int index = 0; index < size; index++)
cin >> fn[index] >> ln[index] ;
}
// 这个代码块中的某个地方给了我一个错误,但我似乎无法弄清楚,请帮助并感谢 char search_list(const char fn[], const char ln[],int 大小, 字符搜索){
int index = 0;
while ((fn[index] != search) && (index < size))
index++;
if (index == size)//if target is not in a.
index = "";
return index;
}
im trying to perform an index search for string, im getting an error message: invalid conversion from const char* to int. can someone please explain what this means.
here is the code
#include <iostream>
#include<string>
#include <stdio.h>
#include <string.h>
using namespace std;
void clist(char fn[],char ln[], int size);
char search_list(const char fn[],const char ln[], int size, string find);
int main(){
string search;
cout << "This program searches a list .\n";
const int total = 3;
char fn[total];
char ln[total];
clist(fn,ln, total);
cout << "Search contact:____ ";
cin >> search;
search_list(fn,ln, total, search);
return 0;
}
void clist(char fn[],char ln[], int size){
cout << "Enter " << size << " contact.\n";
for (int index = 0; index < size; index++)
cin >> fn[index] >> ln[index] ;
}
// somewhere in this block of code is giving me an error but i cant seem to figure it out, plz help and thanks
char search_list(const char fn[], const char ln[],int size, char search){
int index = 0;
while ((fn[index] != search) && (index < size))
index++;
if (index == size)//if target is not in a.
index = "";
return index;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用一个函数,该函数接受
参数
类型的参数,您可以看到最后一个参数如何不匹配。
另外,您尝试使用以下行将
char const[]
分配给int
:You're calling a function which takes arguments of type
with the arguments
You can see how the last ones don't match up.
Also, you're trying to assign a
char const[]
to anint
with this line:这是错误,你的索引是一个整数,你不能将它设置为 char* 类型。
Here is the error, your index is an integer, you can't set it to a char* type.