cin.getline 和 strstr 有问题

Trouble with cin.getline and strstr

我正在尝试编写一个简单的程序来读取姓名列表并允许您在其中进行搜索。我的 cin.getline 和 strstr 有问题。我是 C++ 的新手,我很难理解 C 字符串及其函数。 我从 cin.getline 得到的错误是无法将参数 1 从 'std::ifstream' 转换为 'char *' 并且无法将参数 1 从 'char' 转换为 'char *'

我从 strstr 得到的错误是错误 C2665:'strstr':2 个重载中的 none 可以转换所有参数类型

 #include <iostream>
 #include <string>
 #include <fstream>
 #include <cstring>

 using namespace std;

 int main()
 {
 const int SIZE = 50; 
 int size = 0;

 // Array of product descriptions
 char phoneDirectory[SIZE]; 

 char name; // For user input
 char *strPtr = NULL; // Result from strstr 

 ifstream inFile;
 inFile.open("phonebook");
 while (!inFile.fail()) 
 {
 cin.getline(inFile,phoneDirectory[size]);
 size++;
 }
 inFile.close();

 // Get user input
 cout << "Enter a name to search for: ";
 cin.getline(name, SIZE);

 // Search for the string
 int index = 0; 
 while(index < size)
 {
 strPtr = strstr(phoneDirectory[index], name);
 if (strPtr != NULL)
 break;
 index++; 
 }

 // Output the result of the search
 if (strPtr == NULL)
 cout << "No matching names were found.\n";
 else
 cout << phoneDirectory[index] << endl;
 return 0;
 }

 I cant seem to fix the cin.getline's and the strstr. 

char name 不代表字符串,而是1个单个字符。 getline 函数接受一个字符指针和一个整数,在您的情况下,您正在为它提供一个字符和一个整数。

此外,在 C++ 中,我建议使用 std::string 而不是字符数组。它更容易处理并且不易出错。

我已经更正了您的代码以实现我认为您正在寻找的功能。如果这不是您要找的,请告诉我:

 #include <iostream>
 #include <string>
 #include <fstream>
 #include <cstring>

 using namespace std;

int main()
{
    const int SIZE = 50;
    int size = 0;

    // Array of product descriptions
    string phoneDirectory[SIZE];

    string name; // For user input
    char *strPtr = NULL; // Result from strstr

    ifstream inFile;
    inFile.open("phonebook");
    while (!inFile.fail())
    {
        getline(inFile, phoneDirectory[size]);
        size++;
    }
    inFile.close();

    // Get user input
    cout << "Enter a name to search for: ";
    getline(cin, name);

    // Search for the string
    int index = 0;
    while(index < size)
    {
        strPtr = strstr(phoneDirectory[index].c_str(), name.c_str());
        if (strPtr != NULL)
            break;
        index++;
    }

    // Output the result of the search
    if (strPtr == NULL)
        cout << "No matching names were found.\n";
    else
        cout << phoneDirectory[index] << endl;

    return 0;
}

如果您真的想坚持使用字符数组,您的代码应该如下所示:

 #include <iostream>
 #include <string>
 #include <fstream>
 #include <cstring>

 using namespace std;

int main()
{
    const int DIRECTORY_SIZE = 50;
    const int STRING_SIZE = 50;
    int size = 0;

    // Array of product descriptions
    char phoneDirectory[DIRECTORY_SIZE][STRING_SIZE];
    char name[STRING_SIZE]; // For user input
    char *strPtr = NULL; // Result from strstr

    ifstream inFile;
    inFile.open("phonebook");
    while (!inFile.fail() && size < DIRECTORY_SIZE)
    {
        inFile.getline(phoneDirectory[size], STRING_SIZE);
        size++;
    }
    inFile.close();

    // Get user input
    cout << "Enter a name to search for: ";
    cin.getline(name, STRING_SIZE);

    // Search for the string
    int index = 0;
    while(index < size)
    {
        strPtr = strstr(phoneDirectory[index], name);
        if (strPtr != NULL)
            break;
        index++;
    }

    // Output the result of the search
    if (strPtr == NULL)
        cout << "No matching names were found.\n";
    else
        cout << phoneDirectory[index] << endl;

    return 0;
}