gpt4 book ai didi

c++ - 电话号码列表

转载 作者:行者123 更新时间:2023-11-28 06:02:08 24 4
gpt4 key购买 nike

用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com