Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So I am trying to follow the directions here: I am very confused as to how to im

ID: 3863381 • Letter: S

Question

So I am trying to follow the directions here:

I am very confused as to how to implement structures, templates, and modify vector elements from a structure.

My code so far is:

Main:

function_file.cpp :

Header file:

So long story short, how exactly do I get this set up so that I can begin getting user input into first name, last name, and balance? (Account number generated randomly)

Create a vector of the bank accounts. Each element of the vector will have all the account information. To do this, you would have to use data structure. The keyword struct in C++ will make a data structure. Inside the data structure, you will define all of the account information The following is an example: struct Account int accountNumber; string lastName; string firstName; double account Balance; vector KAccount bankAccounts; The code snippet above defines data structure type Account and a vector name bankAccounts of type Account. Each element of the vector will have member accountNumber, lastName, firstName, and accountBalance. The vector should be defined in the main0function. This means that as long as the program running (the bank is operating), we have the list of bank accounts. This should not be the global variable. The program should be running until terminated by the user. In order to update the information of bank accounts in the vector. you would have to pass by reference to the functions. Another option is pass by pointer. You can explore this option if desiree. Because the data in the vector is of the special type, you have to make a template for the typename. Template allows you to write functions independent of the data type being passed to the functions. The example of a prototype function as follow: template

Explanation / Answer

This is a simple task to begin with.

You want to add this inside your makeAccount() in which you will ask for first name, last name and account balance. Store that in the appropriate variables.

Generate your account number first. You can use a static variable and increment its value like accno = 1000020 then increment it every time. or can randomly generate it.

Now, make an Account struct variable say 'acc', then save all the 4 values in this variable 'acc'.

This can be achieved as acc.accno = 1230; acc.firstname = "Name"; and so on....

Then you have a struct variable completed with values so you need to store it in a vector so use push_back() function for this. I would suggest you to move the line vector<Account> bankAccount; outside of the main() so that it will become a global variable and can be used in every methods. This is necessary to do the required task.

The code about which I wrote above can be written as:

vector<Account> bankAccount; // take this outside main()
int main()
{

...

}

void makeAccount()
{
Account a;

static int accno = 12050;
string fname, lname;
double bal;

a.accountNumber = ++accno;
cout << "Enter firstname: ";
cin >> a.firstName;

cout << endl << "Enter lastname: ";
cin >> a.lastName;

cout << endl << "Enter account balance: ";
cin >> a.accountBalance;

bankAccount.push_back(a);
}

Here, I suggested the use of static variable because it retains the old value the next time function is called. So, see in the first account making I have given account number 12051 (pre-increment 12050 before assignment so 12051) so the next time when the same function is called, it will give 12052 and will not again define a new variable with value 12050. So, this can be used for auto-generation of account number as well.

What I have done in this code is what you had asked for. I prompted to enter first name, last name and the account balance and set the values to the appropriate variable of the struct variable 'a' I defined inside this method. After setting all the 4 values, I added this account information in our vector bankAccount which is completely valid because the type of 'a' and the type of vector both are same: Account structure. And push back is done so that it is inserted at last of the current list.

Please comment if there is any query. Thank you.. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote