Search code examples
c++objectgetter-setter

Instantiate Object and save items from vector


First, I'm sorry if code is sloppy or wrong. I am having a hard time with c++.

I am trying to create a customer from a file and save each customer as the object customer and store each of the variables.

I have successfully been able to open and read the file save the words to a vector. I now need to create a customer and save the information.

My Question is - Now that I have a vector with all of the information how would I create a new customer and store the first item in vector as fName, second as lName, and the third as acctNumber. The 4th item in the vector would be a new customer saving as their fName and so on.

An example of the text file I am using is below.
Michael Jackson 1
George Jones 2
Brittany Spears 3

Goal: Above file would instantiate 3 customers and set each of their fName, lName and acctNumber for later use.

class CustomerType {
public:
    CustomerType(string f, string l, int a);
    string fName;
    string lName;
    int acctNumber;
    videoType chkdOut;
private:
};

CustomerType::CustomerType(string f, string l, int a) {
    fName = f;
    lName = l;
    acctNumber = a;
}

void createCustomer() {
    vector<string> myVector;
    ifstream myfile;
    myfile.open("custDat.txt");
    if (myfile.is_open())
    {
        while (!myfile.eof())
        {
            string tempString;
            getline(myfile, tempString);
            istringstream iss(tempString);
            string word;

            while (iss >> word) {
                myVector.push_back(word);
            }
        }
        myfile.close();
    }
    else
        cout << "Can't open file" << endl;
}

Solution

  • First, add a constructor to your customer class, which will take the information required:

    class customerType {
    public:
        customerType( string firstName, string lastName, int accountNumber );
        string fName;
        string lName;
        int acctNumber;
        videoType chkdOut;
    private:
    };
    

    The constructor will be defined in this way:

    customerType( string firstName, string lastName, int accountNumber )
    {
        fName = firstName;
        lName = lastName;
        acctNumber = accountNumber;
    }
    

    You have to create a method to split a string with a character, in order to obtain the different information from each line:

    vector<string> split( string line, char c )
    {
        const char *str = line.c_str();
    
        vector<string> result;
    
        do
        {
            const char *begin = str;
    
            while ( *str != c && *str )
                str++;
    
            result.push_back( string( begin, str ) );
        }
        while ( 0 != *str++ );
    
        return result;
    }
    

    Then, in the method to create a customer, you can create a the new object using that constructor, and then return the vector with the customers:

    vector<customerType> createCustomer() {
    
        // create a vector of customers:
        vector<customerType> customers;
    
        vector<string> myVector;
        ifstream myfile;
        myfile.open("custDat.txt");
        if (myfile.is_open())
        {
            while (!myfile.eof())
            {
                string tempString;
                getline(myfile, tempString);
    
                // Here you get a vector with each work in the line
                vector<string> splittedString = split(tempString, ' ');
    
                //Finally here you create a new customer
                customers.push_back(customerType(splittedString[0], splittedString[1], stoi(splittedString[2])));
            }
            myfile.close();
        }
        else
            cout << "Can't open file" << endl;
    
        return customers;
    }
    

    Sorry, I changed the way you store the words.

    The stoi function converts the string to an integer.