[英]How do I change the following code to read multiple addresses from a text file instead of the hard coded information?
Here is the clip of the text file!这是文本文件的剪辑!
How do I change the following code to read multiple addresses from a text file instead of the hard coded information in the following line?如何更改以下代码以从文本文件中读取多个地址,而不是下一行中的硬编码信息? Please help!
请帮忙!
"customerData c("Donald","Allen","abc apartment","Newyork","Newyork",10001,"6667676222", 1016,false);" "customerData c("Donald","Allen","abc 公寓","Newyork","Newyork",10001,"6667676222", 1016,false);"
This is the actual assignment:这是实际的任务:
**Design a class named PersonData with the following member variables: lastName firstName address city state zip phone customerNumber mailingList Write the appropriate accessor and mutator functions for these member variables. **使用以下成员变量设计一个名为 PersonData 的类: lastName firstName address city state zip phone customerNumber mailingList 为这些成员变量编写适当的访问器和修改器函数。 The customerNumber variable will be used to hold a unique integer for each customer and you must manage the uniqueness of this variable using a static member variable.
customerNumber 变量将用于保存每个客户的唯一整数,您必须使用静态成员变量管理此变量的唯一性。 The mailingList variable should be a bool and will be set to true if the customer wishes to be on a mailing list, or false if the customer does not wish to be on a mailing list.
mailingList 变量应该是一个布尔值,如果客户希望在邮件列表中,则设置为 true,如果客户不希望在邮件列表中,则设置为 false。 Demonstrate, in a simple printing program, an array of objects from this class, where customer data is read from a file and then into the array of objects using their set() member functions.
在一个简单的打印程序中演示来自此类的对象数组,其中客户数据从文件中读取,然后使用它们的 set() 成员函数进入对象数组。 Once all data is read and the array is populated, the program should print out, using get() member functions, all the info for customers that would like to be included in the mailing list.
一旦读取了所有数据并填充了数组,程序应该使用 get() 成员函数打印出希望包含在邮件列表中的客户的所有信息。
A Sample text file CustomerInfo.txt will be included with this document.**本文档将包含一个示例文本文件 CustomerInfo.txt。**
Here is what I have so far:这是我到目前为止所拥有的:
#include<iostream>
using namespace std;
//Person class definition
class Personaldata
{
private:
//instance variables declaration
string Lastname;
string Firstname;
string Address;
string City;
string State;
int Zipcode;
string Phone;
public:
//constructor to initialize variables
Personaldata(string lName,string fName,string addr,string pCity,
string pState,int zCode,string pPhone)
{
Lastname=lName;
Firstname=fName;
Address=addr;
City=pCity;
State=pState;
Zipcode=zCode;
Phone=pPhone;
}
//getter methods to return Latname,Firstname,Address,City,State,Zipcode and Phone
string getLaststname()
{
return Lastname;
}
string getFirstname()
{
return Firstname;
}
string getAddress()
{
return Address;
}
string getCity()
{
return City;
}
string getState()
{
return State;
}
int getZipcode()
{
return Zipcode;
}
string getPhone()
{
return Phone;
}
//setter methods to set Latname,Firstname,Address,City,State,Zipcode and Phone
void setLasstname(string lName)
{
Lastname=lName;
}
void setFirstname(string fName)
{
Firstname=fName;
}
void setAddress(string addr)
{
Address=addr;
}
void setCity(string pCity)
{
City=pCity;
}
void setState(string pState)
{
State=pState;
}
void setZipcode(int zCode)
{
Zipcode=zCode;
}
void setPhone(string pPhone)
{
Phone=pPhone;
}
};
//customerData class derived from Personaldata class
class customerData:public Personaldata
{
private:
//instance variables declaration
int customerNumber;
bool mailingList;
public:
//constructor to initialize variables
//pase values into base class's constructor
customerData(string lName,string fName,string addr,
string pCity,string pState,int zCode,string pPhone,int number,bool mailing):
Personaldata(lName,fName,addr,pCity,pState,zCode,pPhone)
{
customerNumber=number;
mailingList=mailing;
}
//getter method to return customerNumber and mailingList
int getCustomerNumber()
{
return customerNumber;
}
bool isMailingList()
{
return mailingList;
}
//setter method to set customerNumber and mailingList
void setCustomerNumber(int number)
{
customerNumber=number;
}
void setMailingList(bool mailing)
{
mailingList=mailing;
}
};
int main()
{
//create an object of customerData class
customerData c("Donald","Allen","abc apartment","Newyork","Newyork",10001,"6667676222", 1016,false);
//change mailingList and Address using setter methods
c.setMailingList(true);
c.setAddress("xyz apartment");
//display details of object using getter methods
cout<<"Customer name: "<<c.getFirstname()<<" "<<c.getLaststname()<<endl;
cout<<"Address: "<<c.getAddress()<<endl;
cout<<"City: "<<c.getCity()<<endl;
cout<<"State: "<<c.getState()<<endl;
cout<<"Zip code: "<<c.getZipcode()<<endl;
cout<<"Phone: "<<c.getPhone()<<endl;
cout<<"Customer number: "<<c.getCustomerNumber()<<endl;
cout<<"Is customer wishes to be in mailing list?: "<<c.isMailingList()<<endl;
return 0;
}
First of all, you need to understand the format of the data stored on file.首先,您需要了解存储在文件中的数据的格式。 For example, are the data stored as CSV (comma separated values)?
例如,数据是否存储为 CSV(逗号分隔值)? Do they use the JSON format?
他们使用 JSON 格式吗? You definitely need to know how the file is formatted before moving forward.
在继续之前,您肯定需要知道文件的格式。
Once you figure that out, the next would be to write a function for reading from it.一旦你弄清楚了,接下来就是编写一个读取它的函数。 I'm not going to provide a solution.
我不会提供解决方案。 But here is the link to a resource you might want to check: https://www.w3schools.com/cpp/cpp_files.asp You can find much more stuffs on internet, anyway.
但这里是您可能想要检查的资源的链接: https ://www.w3schools.com/cpp/cpp_files.asp 无论如何,您可以在互联网上找到更多的东西。 Probably some snippets of code that solve a similar problem and that you can study.
可能是一些解决类似问题的代码片段,您可以学习。
In general the approach would be:一般来说,方法是:
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.