Search code examples
c++getlinestrstr

Trouble with cin.getline and strstr


Im trying to write a simple program that will read in a list of names and allow you to search through them. Im having a problem with my cin.getline and my strstr. Im new to c++ and im have a hard time getting my head around c string and its functions. The error I get from the cin.getline is cannot convert parameter 1 from 'std::ifstream' to 'char *' and cannot convert parameter 1 from 'char' to 'char *'

The error I get from the strstr is error C2665: 'strstr' : none of the 2 overloads could convert all the argument types

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

Solution

  • char name doesn't represent a string, but 1 single character. The getline function accepts a character pointer and an integer, and in your case you're feeding it a character and an integer.

    Also, in c++ I'd suggest using std::string instead of characters array. It's much easier to handle and less error prone.

    I've corrected your code to do what I think you're looking for. let me know if that's not what you're looking for:

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

    If you really want to stick with the characters array, here's what your code should look like:

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