用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 filen";
//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 filen";
//looping throught the name file
else
{
while (getline(phoneNumbers, phoneNum))
{
size++;
}
phoneNumbers.close(); //closes data file
}
return size;
}
您的name
变量是char
。应该换成char*
吗?