我遇到了这个程序的问题。我对函数strcpy
和strcmp
并不熟悉。
任何专业人士都可以帮助我或给我一些建议吗?
答案 0 :(得分:2)
正如其他人所说,选择C风格的字符串或C ++ std::string
。
我强烈建议不要创建单独的函数来搜索字符串数组,因为初学者很难将数组传递给函数。
尝试类似:
#include <string>
#include <iostream>
#define MAX_NAMES 16
int main(void)
{
std::string name_container[MAX_NAMES];
unsigned int names_in_container = 0;
while (1)
{
std::string name_from_user;
std::cout << "Enter name: ";
if (!getline(std::cin, name_from_user))
{
break; // Exit from the "while" loop
}
// Search the name container for the name.
unsigned int array_slot = 0;
bool name_found = false;
for (array_slot = 0; array_slot < names_in_container; ++array_slot)
{
if (name_from_user == name_container[array_slot])
{
std::cout << "\nName exists in slot " << array_slot << "\n";
name_found = true;
}
}
if (!name_found)
{
if (array_slot >= MAX_NAMES)
{
std::cout << "Name container full, cannot add name.\n";
break;
}
else
{
name_container[names_in_container] = name_from_user;
++names_in_container;
}
}
}
}
答案 1 :(得分:0)
请注意您正在使用C ++字符串(std :: string)和C库strcpy和strcmp函数。他们是两个不同的东西。您可能需要查看std :: string类下的一些可用方法(.c_str,=,==),
以下是一些评论和建议,
void search (string sid[], //do you intend to pass a string sid, or an array of srings?
string name[], //do you intend to pass a string name, or an array of strings?
int No_of_data_input)
{
char id[100];
string input;
char namename[100];
//comments ignored/removed
//these two lines declare the function strcpy
char * strcpy ( char * namename, const char * name );
char * strcpy ( char * id, const char * sid );
//do you intend to copy name and sid instead?
//do you want: strcpy(namename, name[i].c_str() );
//do you want: strcpy(id, sid[i].c_str() );
//if so, you want to do this inside your loop on i, below,
int sameName=0;
int j=0;
cout<<"enter id or name";
getline(cin,input); //you probably want: cin >> input;
//do you intend to declare the function strcpy yet again?
char * strcpy ( char * inputinput, const char * input );
//or do you intend to copy input to some char[]?
//you probably want: strcpy( inputinput, input.c_str() );
//you probably want number of elements in sid[] or name[]?
for(int i=0;i<4;i++){
//you probably want sid[i] here
if ((strcmp(inputinput,id[i])==0) || (strcmp(inputinput,name[i])==0)){
//you could rewrite this as:
//if( (input == id[i]) || (input == name[i]) ){
sameName++;
j=i;
} //do us a favor
} //make the ending braces clearer
cout<<sameName;
if (sameName==0)
cout<<"No student found: ";
else if (sameName==1)
cout<<"student found: "<<name[j]<<endl<<id[j];
else if (sameName>1)
cout<<"More than one student found,please enter id";
}
你想(或需要)编译std :: string和strcpy,strcmp吗?
编辑代码,您可能只想使用std :: string,
void search (string sid[], //do you intend to pass an array of srings?
string name[], //do you intend to pass an array of strings?
int count)
{
string input;
int same=0;
int j=0;
cout<<"enter id or name";
cin >> input;
//you probably want number of elements in sid[] or name[]?
for(int i=0;i<count;++i){
if( (input == sid[i]) || (input == name[i]) ){
++same;
j=i;
}
}
cout<<"same: "<<same<<endl;
if (same==0)
cout<<"No student found: "<<endl;
else if (same==1)
cout<<"student found: "<<name[j]<<","<<id[j]<<endl;
else if (same>1)
cout<<"More than one student found,please enter id"<<endl;
}
也许你可以使用字符串向量?然后你可以在向量上使用迭代器。
如果您定义学生记录(包含sid和名称),并传递学生记录的向量,您可能会获得更多成功。然后在该向量上使用迭代器 - 读取有关向量和迭代器的信息。