麻烦cin.getline和strstr

时间:2015-10-13 01:51:00

标签: c++ getline strstr

我正在尝试编写一个简单的程序,该程序将读入名称列表并允许您搜索它们。我的cin.getline和我的strstr有问题。我是c +的新手,我很难了解c字符串及其功能。 我从cin.getline得到的错误无法从' std :: ifstream'转换参数1。到#char;' 并且无法从' char'转换参数1到' char *'

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

 #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. 

1 个答案:

答案 0 :(得分:0)

char name并不表示字符串,而是1个单字符。 getline函数接受一个字符指针和一个整数,在你的情况下,你给它一个字符和一个整数。

另外,在c ++中,我建议使用std :: string而不是characters数组。它更容易处理,更不容易出错。

我已修正您的代码,以执行我认为您正在寻找的内容。请告诉我,如果这不是您正在寻找的内容:

 #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;
}