-1

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;
}
Asuu
  • 65
  • 1
  • 9
  • 1
    If i was my project I would write a `operator >>` and `< – NathanOliver Feb 17 '17 at 17:29
  • I think I need a constructor before I can instantiate a new object of customerType, and then use getters and setters to save the variables. If someone could help get me started I think I could proceed. – Asuu Feb 17 '17 at 17:29
  • _@Asuu_ Go with @Nathan's advice. And defining a constructor would be a good idea also. Provide one with parameters to initialize all your member variables. – πάντα ῥεῖ Feb 17 '17 at 17:33
  • @NathanOliver would you please elaborate. I am new to C++ and piecing this together – Asuu Feb 17 '17 at 17:35
  • @Asuu see this: https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx and https://msdn.microsoft.com/en-us/library/x6aebccc.aspx – NathanOliver Feb 17 '17 at 17:36

1 Answers1

0

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.