How to create defined users

I have a start up menu and I am able to create new users. Right now I cannot login until I create a user. My goal is to have 3 pre made users with balances that can log in on the start up menu. Any other tips are welcome to make the code better.


#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

class AutoTellerMachine { //Object to represent each customer who uses the ATM program
public:
void CreateNewAccount(string newUsername, string newPassword);
void AccountLogin(string loginUsername, string loginPassword);
void DepositMoney(double depositAmount);
void WithdrawMoney(double withdrawalAmount);
void SetAccountLogin(int setAccountLocation);
void SetLastMoneyMovement(int accountID, double amount);
void SetBeginningBalance(int accountID);
void SetLastOperation(int accountID, char userInput);
void AccountMenu();
int GetAccountLogin() const;
double GetLastMoneyMovement(int accountID) const;
double GetAccountBalance(int accountID) const;
double GetBeginningBalance(int accountID) const;
char GetLastOperation(int accountID) const;
string GetUsername(int accountID) const;

private:
int loggedInAccountLocation;
double accountBalance;
double beginningBalance;
double lastMoneyMovement;
char lastOperation;
string username;
string password;
};

AutoTellerMachine account;

vector<AutoTellerMachine> AccountList; //This vector allows for multiple accounts to be stored so that if more than one person uses the account, all information is retained

void AccountMenu();
void UserMenu();

void AutoTellerMachine::SetAccountLogin(int setAccountLocation) {

loggedInAccountLocation = setAccountLocation;

return;
}

int AutoTellerMachine::GetAccountLogin() const {

return loggedInAccountLocation;
}

void AutoTellerMachine::CreateNewAccount(string newUsername, string newPassword) { //Adds the new information to the vector to give the account personalized info

int accountListSize = AccountList.size();

AccountList.at(accountListSize - 1).accountBalance = 0.0;
AccountList.at(accountListSize - 1).username = newUsername;
AccountList.at(accountListSize - 1).password = newPassword;

}

void AutoTellerMachine::AccountLogin(string loginUsername, string loginPassword) {

int accountListSize = AccountList.size();
bool successfulLogin = false;
int accountLocation = 0;

for (int i = 0; i < accountListSize; i++) {

if (loginUsername == AccountList.at(i).username) {

if (loginPassword == AccountList.at(i).password) {

successfulLogin = true;
accountLocation = i;
}
}
}

if (successfulLogin != true) {

cout << endl << "******** LOGIN FAILED! ********" << endl << endl;
UserMenu();
}

else if (successfulLogin == true) {

SetAccountLogin(accountLocation);
cout << endl << "Access Granted - " << AccountList.at(loggedInAccountLocation).username << endl;
AccountMenu();
}

return;
}

void AutoTellerMachine::DepositMoney(double depositAmount) {

AccountList.at(loggedInAccountLocation).accountBalance += depositAmount;

return;
}

void AutoTellerMachine::WithdrawMoney(double withdrawalAmount) {

AccountList.at(loggedInAccountLocation).accountBalance -= withdrawalAmount;

return;
}

double AutoTellerMachine::GetAccountBalance(int accountID) const {

return AccountList.at(loggedInAccountLocation).accountBalance;
}

void AutoTellerMachine::SetLastMoneyMovement(int accountID, double amount) {

AccountList.at(accountID).lastMoneyMovement = amount;
}

void AutoTellerMachine::SetBeginningBalance(int accountID) {

AccountList.at(accountID).beginningBalance = AccountList.at(loggedInAccountLocation).accountBalance;
}

void AutoTellerMachine::SetLastOperation(int accountID, char userInput) {

AccountList.at(accountID).lastOperation = userInput;
}

double AutoTellerMachine::GetLastMoneyMovement(int accountID) const {

return AccountList.at(accountID).lastMoneyMovement;
}

double AutoTellerMachine::GetBeginningBalance(int accountID) const {

return AccountList.at(accountID).beginningBalance;
}

char AutoTellerMachine::GetLastOperation(int accountID) const {

return AccountList.at(accountID).lastOperation;
}

string AutoTellerMachine::GetUsername(int accountID) const {

return AccountList.at(GetAccountLogin()).username;
}

You're misusing your vector. An object in a vector (account) shouldn't be self-referential to the vector (accountList).

Line 5: Avoid the use of namespace std.

Line 7: You should have at least two classes. Account and ATM. They represent different things. You are conflating them. An account represents a customer. An ATM is a physical device that dispenses cash from a specific account and accepts deposits to a specific account. i.e. A specific account should be passed to an ATM function as a reference parameter.

Your class declaration(s) and class function definitions should be in separate files (.h and .cpp).

I see no main function. All C++ programs must have a main() function.

Line 54-62: This function is flawed. An instance of an ATM should not reference a global (AccountList). You can't refer to AccountSize-1. What happens when AccountSize is zero? Illegal (out of bounds) reference. Use push_back(obj) to add an object to a vector.

Line 107: What happens if the withdrawalAmount is greater than the balance?

Line 147: Why are you passing accountId to GetUsername()? You don't use it.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Topic archived. No new replies allowed.