疯狂的strcmp和strcpy
Mad on strcmp and strcpy
我的这个程序有问题。我对函数strcpy
和strcmp
不太熟悉。有专业人士能帮我或给我一些建议吗?
正如其他人所说,选择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;
}
}
}
}
请确认您正在组合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和名称),并传递学生记录的向量,您可能会取得更大的成功。然后在该向量上使用迭代器——阅读有关向量和迭代器的内容。
相关文章:
- 使用strcpy将char数组的元素复制到另一个数组
- strcpy 在类中无法使用字符串指针
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- C++ strcpy 函数在少数主要情况下失败
- 在函数 strcpy() 中访问字符数组时出现分段错误
- strcmp 对于相等的字符序列返回 false
- STRCMP内部IF在内部不起作用
- C++编译 strlen 和 strcpy 的问题
- strcmp/char* 转换无法按预期工作
- 堆栈数组c++的Strcpy行为
- 如何使用 strcmp 将字符串与预定义字符串进行比较
- 无法使用 strcpy 将字符串复制到静态结构成员
- GDB - strcmp not working: __strcmp_sse2_unaligned
- 错误:调用'strcmp'没有匹配函数(尝试设置显示用户信息功能)
- Strcmp 类行为应用于C++中的对象列表
- 为什么 == 运算符没有产生与 strcmp 相同的结果?
- strcmp 错误'WCHAR [260]' 'const char *'
- C++ 从文件中获取数据时使用 strcpy 和 strcmp 按字母顺序对数组进行排序?
- 疯狂的strcmp和strcpy
- strcpy和strcmp,我做错了什么