c++ - 电话号码列表

标签 c++ pointers strstr

用 C++ 编写一个简单的电话簿程序,在包含姓名和电话号码列表的文件中查找电话号码。应提示用户输入名字和姓氏,然后程序输出相应的数字,或者指示该名称不在目录中。每次查找后,程序应询问用户是否要查找另一个号码,然后重复该过程或退出程序。文件中的数据应进行组织,以便每行包含名字、姓氏和电话号码,并以空格分隔。您可以通过关闭文件并再次打开它来返回到文件的开头。

我打不通 检查= strstr(电话目录,名称); 工作 strstr 部分不断给出错误:重载函数“strstr”的实例与参数列表参数类型不匹配。

这是我的代码的拷贝:

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

最佳答案

您的name 变量是一个char。它应该是 char* 吗?

关于c++ - 电话号码列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33057727/

相关文章:

c++ - C++ 函数可以返回多个值吗?

c++ - 有条件地忽略 C++11 正则表达式中的大小写

返回指针但不允许删除该指针的 C++ 类公共(public)函数?

c++ - * 或 & 的位置重要吗?

c - 使用 strchr 从数组中删除字符,在普通 C 中不返回正确的值

c - 每次 strstr() 函数返回 false。当我传递任何输入时,函数总是返回 false

c++ - C++ 中的内存分配(抛出异常 : read access violation.)

更改 LinkedList 中的数组值

C 编程。将字符串 www.as.com 转换为 3www2as3com0

c++ - 关于QList类型实例的复制构造函数的奇怪问题