0

Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the program. The data on the file should be organized so that each line contains a first name, a last name, and a phone number, separated by blanks. You can return to the beginning of the file by closing it an opening it again.

I cant get the line check = strstr(phoneDirectory, name); to work strstr part keep giving the error: no instance of overloaded function "strstr" matches the argument list argument types.

Here a copy of my code:

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

using namespace std;

int arraySize();

 int main()
{
    const int SIZE = arraySize();

    char *phoneDirectory;
    int size=0;
    char name; //name to look for
    char *check = NULL;
    bool find = false;

    phoneDirectory = new char [SIZE];

    ifstream phoneNumbers;
    phoneNumbers.open("phoneNumbers.txt");

    if (!phoneNumbers)
        cout << "Error opening data file\n";

    //looping throught the name file
    else
    {
        for (int i = 0; i < SIZE; i++)
        {
            phoneNumbers >> phoneDirectory;
        }

        phoneNumbers.close();       //closes data file
    }

    phoneNumbers.close();

    // Get a name or partial name to search for.
    cout << "Enter a name or partial name to search for: ";
    cin.getline(phoneDirectory, name);

    cout << "\nHere are the results of the search: " << endl;
    int entries = 0;

    for (int i = 0; i < size; i++)
    {
        check = strstr(phoneDirectory, name);

        if (check != NULL)
            find = true;
    }

    if (!find) 
        cout << "No matches!" << endl;


    delete [] phoneDirectory;
    return 0;
} 

 int arraySize()
{
    string phoneNum;
    int size = 0;

    ifstream phoneNumbers;      // Input file stream object

    // Open the data file.
    phoneNumbers.open("phoneNumbers.txt");
    if (!phoneNumbers)
        cout << "Error opening data file\n";

    //looping throught the name file
    else
    {
        while (getline(phoneNumbers, phoneNum))
        {
            size++;
        }
        phoneNumbers.close();       //closes data file
    }
    return size;
}
Doe
  • 33
  • 1
  • 6
  • Please use [MCVE](https://stackoverflow.com/help/mcve) – Paul Evans Oct 10 '15 at 18:59
  • There are several issues with your program. Your `name` variable is a single `char` and you are trying to read multiple characters into it. Your `phoneDirectory` variable is an array of `char` that is only as big as the number of lines, as opposed to an array of strings to hold each line. Since you are using the `string` class I would also suggest your phone directory be a `std::vector` of `std::string`. Then you don't need to open the file to count the lines, you can just keep reading strings and putting them in the vector. – pstrjds Oct 10 '15 at 19:04
  • We never learned vectors so I cant use them and I'm not suppose to use the string I just had it in their for debugging. We are suppose to be using c strings. – Doe Oct 10 '15 at 19:15

1 Answers1

1

Your name variable is a char. Should it be a char* instead?

S.Clem
  • 491
  • 5
  • 10
  • Oh that fixed that problem. Thanks. My program still crashed but a different point now. At least I'm closer. Thank You – Doe Oct 10 '15 at 19:12