用C ++编写一个简单的电话目录程序,在一个包含姓名和电话号码列表的文件中查找电话号码。应提示用户输入名字和姓氏,然后程序输出相应的号码,或者表示该名称不在目录中。在每次查找之后,程序应该询问用户是否要查找另一个号码,然后重复该过程或退出程序。应组织文件上的数据,以便每行包含由空格分隔的名字,姓氏和电话号码。您可以通过再次打开它来返回文件的开头。
我不能排队 check = strstr(phoneDirectory,name); 工作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;
}
答案 0 :(得分:1)
您的name
变量是char
。应该是char*
吗?