C++ C++;数组和从文件读取
我正在创建一个通讯簿程序,允许用户按名字、姓氏、电话号码和地址进行搜索。系统将提示用户输入文件名,并将该文件读入数组。我在修改现有的SearchFirstName函数以循环数组时遇到问题。这个话题我已经读了好几遍了,我就是不明白。任何帮助都将不胜感激 文件 头文件C++ C++;数组和从文件读取,c++,arrays,struct,C++,Arrays,Struct,我正在创建一个通讯簿程序,允许用户按名字、姓氏、电话号码和地址进行搜索。系统将提示用户输入文件名,并将该文件读入数组。我在修改现有的SearchFirstName函数以循环数组时遇到问题。这个话题我已经读了好几遍了,我就是不明白。任何帮助都将不胜感激 文件 头文件 #include<string> using namespace std; enum Title {Mr, Mrs, Ms, Dr, NA}; struct NameType { Title title; string
#include<string>
using namespace std;
enum Title {Mr, Mrs, Ms, Dr, NA};
struct NameType {
Title title;
string firstName;
string lastName;
};
struct AddressType {
string street;
string city;
string state;
string zip;
};
struct PhoneType {
int areaCode;
int prefix;
int number;
};
struct entryType {
NameType name;
AddressType address;
PhoneType phone;
};
const int MAX_RECORDS = 50;
struct addressBookType {
entryType record[MAX_RECORDS];
int numEntries;
};
#包括
使用名称空间std;
枚举标题{先生、太太、女士、博士、NA};
结构名称类型{
职称;
字符串名;
字符串lastName;
};
结构地址类型{
弦街;;
字符串城市;
字符串状态;
拉链;
};
结构音素类型{
国际区号;
int前缀;
整数;
};
结构入口类型{
名称类型名称;
地址类型地址;
电话类型电话;
};
const int MAX_记录=50;
结构addressBookType{
entryType记录[最大记录];
国际货币基金组织;
};
代码
string bookArray[MAX_RECORDS];
int main()
{
entryType用户记录;
字符串文件名;
Iftream inData;
字符搜索选项;
OpenFile(文件名,inData);
主菜单(inData,文件名);
返回0;
}
void OpenFile(字符串和文件名、ifstream和inData)
{
做{
cout>文件名;
open(filename.c_str());
如果(!inData)
无法搜索名称;
normalSearchName=NormalizeString(searchName);//将名称转换为全大写
//循环浏览文件中的所有记录
while(GetRecord(inData,userRecord)){
normalFirstName=NormalizeString(userRecord.name.firstName);//将检索到的字符串转换为全大写
如果(normalFirstName==normalSearchName){//请求的名称匹配
打印记录(用户记录);
选择;
选择=toupper(选择);
库特
创建您的数组,在本例中,它将是一个std::vector
,因为这更易于使用,方法是在while循环中运行GetRecord
函数,并将结果附加到vector w/vector\u变量\u名称中
。NormalizeString
部分就是这样,您就不必在几十亿次之后调用它了
像这样传入数组void SearchFirstName(std::vector*in_data_arr>)
将while
循环更改为for
循环:for(int i=0;i
在循环内部,将normalSearchName=NormalizeString(searchName);
更改为normalSearchName=in_data\u arr[i].name.firstName;
从这里开始,它通常应该是相同的。你到底在哪里遇到了问题?@chbchb55我编辑了我的问题以显示我的尝试。我的代码没有正确地在SearchFirstName函数的数组中循环。有没有一种方法不使用向量来实现这一点,你有两个选择:使用G动态分配字符串数组etRecord(您必须多次重新分配它),或者只是将其全部存储在一个字符串中,然后找出有多少条记录,然后根据其大小创建一个数组,并手动将整个内容加载到for循环中。我建议使用向量,它只需要一个,如果您真的需要一个数组,那么用向量生成数组非常简单!
#include<string>
using namespace std;
enum Title {Mr, Mrs, Ms, Dr, NA};
struct NameType {
Title title;
string firstName;
string lastName;
};
struct AddressType {
string street;
string city;
string state;
string zip;
};
struct PhoneType {
int areaCode;
int prefix;
int number;
};
struct entryType {
NameType name;
AddressType address;
PhoneType phone;
};
const int MAX_RECORDS = 50;
struct addressBookType {
entryType record[MAX_RECORDS];
int numEntries;
};
string bookArray[MAX_RECORDS];
int main()
{
entryType userRecord;
string filename;
ifstream inData;
char searchOption;
OpenFile(filename, inData);
MainMenu(inData, filename);
return 0;
}
void OpenFile(string& filename, ifstream& inData)
{
do {
cout << "Enter file name to open: ";
cin >> filename;
inData.open(filename.c_str());
if (!inData)
cout << "File not found!" << endl;
} while (!inData);
if(inData.is_open())
{
for(int i=0; i<MAX_RECORDS;i++)
{
inData>> bookArray[i];
}
}
}
// Searches passed file stream for a first name read from the user
void SearchFirstName(ifstream& inData)
{
string searchName;
entryType userRecord;
string normalSearchName, normalFirstName;
char choice;
bool found = false;
cout << "Enter first name to search for: ";
cin >> searchName;
normalSearchName = NormalizeString(searchName); // Convert name to all uppercase
// Loop through all records in the file
while (GetRecord(inData, userRecord)){
normalFirstName = NormalizeString(userRecord.name.firstName); // Convert retrieved string to all uppercase
if (normalFirstName == normalSearchName) { // Requested name matches
PrintRecord(userRecord);
cout << "Is this the correct entry? (Y/N)";
cin >> choice;
choice = toupper(choice);
cout << endl;
if (choice == 'Y') {
found = true;
break;
}
}
}
// Matching name was found before the end of the file
if (inData && !found){
cout << "Record found: " << endl;
PrintRecord(userRecord);
cout << endl;
}
else if (!found) // End of file. Name not found.
{
cout << searchName << " not found!" << endl << endl;
}
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
}
void SearchFirstName(ifstream& inData)
{
string searchName;
entryType userRecord;
cout << "Enter first name to search for: ";
cin >> searchName;
string newSearchName = NormalizeString(searchName);
string upFirst = NormalizeString(userRecord.name.firstName);
for (int i=0;i<MAX_RECORDS;i++)
{
while(newSearchName == upFirst)
{
if (bookArray[i]== upFirst)
{
cout<<"Name Found";
cout <<bookArray[i]; //test case
}
}
}
}