Phone 号码列表
Phone Number Lsit
用 C++ 编写一个简单的 telephone 目录程序,在包含姓名列表和 phone 号码的文件中查找 phone 号码。应提示用户输入名字和姓氏,然后程序输出相应的数字,或者指示该名称不在目录中。每次查找后,程序应询问用户是否要查找另一个号码,然后重复该过程或退出程序。文件中的数据应进行组织,以便每行包含一个名字、一个姓氏和一个 phone 数字,以空格分隔。您可以 return 关闭文件并再次打开它来回到文件的开头。
我无法接通电话
检查= strstr(phone目录,名称);
工作 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++ 编写一个简单的 telephone 目录程序,在包含姓名列表和 phone 号码的文件中查找 phone 号码。应提示用户输入名字和姓氏,然后程序输出相应的数字,或者指示该名称不在目录中。每次查找后,程序应询问用户是否要查找另一个号码,然后重复该过程或退出程序。文件中的数据应进行组织,以便每行包含一个名字、一个姓氏和一个 phone 数字,以空格分隔。您可以 return 关闭文件并再次打开它来回到文件的开头。
我无法接通电话 检查= strstr(phone目录,名称); 工作 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*
吗?