简体   繁体   English

如何更改以下代码以从文本文件中读取多个地址而不是硬编码信息?

[英]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:一般来说,方法是:

  1. Open File in Read Mode以读取模式打开文件
  2. If file is opened, move forward.如果文件已打开,请继续前进。
  3. Until you have lines to read:直到你有几行要读:
  • Read one line (this is very generic here, you might need to skip some lines if you have a header section, for example)阅读一行(这在这里非常通用,例如,如果您有标题部分,您可能需要跳过一些行)
  • Parse the line.解析行。 If you consider each line referring to a person and the space is used for separating values, then you might want to split the line based on the space.如果您考虑每行引用一个人并且空格用于分隔值,那么您可能希望根据空格拆分行。 A quick search on google gives me this link:https://www.techiedelight.com/split-string-cpp-using-delimiter/在谷歌上快速搜索给了我这个链接:https ://www.techiedelight.com/split-string-cpp-using-delimiter/
  • Assign the values to the variables that you have defined in your class.将值分配给您在类中定义的变量。 Make sure to convert to float, int, whatever, as needed.确保根据需要转换为 float、int 等。
  1. Do whatever else your code is supposed to do.做你的代码应该做的任何其他事情。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM