0

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. 
Doe
  • 33
  • 1
  • 6

1 Answers1

0

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;
}
J-Mik
  • 896
  • 7
  • 8
  • Thanks that helped a lot but it still giving an error at the strPtr = strstr(phoneDirectory[index].c_str(), name.c_str());. I do have a question why did you have to change it to string instead of char? – Doe Oct 13 '15 at 02:24
  • Here it seems to be working fine. The advantage of using a string is that you're not obliged to specify the length of the line to the getline function. It's less restrictive. I'll post the answer without using a string in 2sec. – J-Mik Oct 13 '15 at 02:30
  • I've updated my answer. Take a look at std::vector too. It's a good practice to use them over arrays. – J-Mik Oct 13 '15 at 02:49