疯狂的strcmp和strcpy

Mad on strcmp and strcpy

本文关键字:strcpy strcmp 疯狂      更新时间:2023-10-16

我的这个程序有问题。我对函数strcpystrcmp不太熟悉。有专业人士能帮我或给我一些建议吗?

正如其他人所说,选择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和名称),并传递学生记录的向量,您可能会取得更大的成功。然后在该向量上使用迭代器——阅读有关向量和迭代器的内容。