如何将字符串转换为const char
how to convert string to const char
所以我解决了第一个问题。我运行代码,提示我在数组中输入一些列表。输入列表后,我将这个函数作为search_func运行。但它保持返回没有发现记录。是因为[0]吗,奇数是因为我把它放在for循环中。
请帮助。book books[]
是一个类对象。
int search(book books[], char search) {
const char* boook =books[0].gettitle();
//......try this but it failed please help
cout << "Search books by title:____ ";
cin >> search;
bool yes = false;
int size=2;
for(int index=0; index<size; index++) {
if(strcmp(boook,search) == 0 )//....error at this line
{
found = true;
cout<<"book found "<<endl;
//cout<<"Author Name: "<<fn<<" "<<ln<<endl;
break;
}
}
if(!yes)
cout<<"no book found"<<endl;
}
试试这个:
const char* c_str = books[0].gettitle().c_str();
http://www.cplusplus.com/reference/string/string/c_str/编辑:如果gettitle()
返回一个临时,那么上述方法将不起作用。您需要这样做:
string title = books[0].gettitle();
const char* c_str = title.c_str();
我假设第一个参数是std::string。试着调用它的c_str()方法