Hello, I am having problems with my program: I need to let the user input a name
ID: 3626692 • Letter: H
Question
Hello,I am having problems with my program:
I need to let the user input a name and then an initial starting balance (infinite amount of names/balance), storing all of this information so that I will be able to search later for the name to withdraw/deposit/check balance of that specific person. I think I'll need to write to a file and then search but I'm not quite sure how to do that. Also, I need to assign a random userid to each name but for some reason, it generates the same id for all names. Please help me, any tips suggestions will be great! Thank you!
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
class BankAccount
{
private:
static int userIDgenerator;
int userID;
float currentBalance;
float previousBalance;
string userName;
public:
BankAccount(float previousBalance);
BankAccount(string initialName);
BankAccount() {userID=userIDgenerator++;}
~BankAccount(){};
int getID() const
{
return userID;
}
string getName();
void setName(string name);
float getBalance();
void setBalance(float balance);
float makeWithdraw();
void setWithdraw(float howmuchWithdraw);
float makeDeposit();
void setDeposit(float howmuchDeposit);
};
int BankAccount::userIDgenerator=1000;
BankAccount::BankAccount (string initialName)
{
userName = initialName;
}
string BankAccount::getName()
{
return userName;
}
void BankAccount::setName(string name)
{
userName = name;
}
BankAccount::BankAccount(float previousBalance)
{
currentBalance = previousBalance;
}
float BankAccount::getBalance()
{
return currentBalance;
}
void BankAccount::setBalance (float balance)
{
currentBalance = balance;
}
float BankAccount::makeWithdraw()
{
return currentBalance;
}
void BankAccount::setWithdraw(float howmuchWithdraw)
{
currentBalance -= howmuchWithdraw;
}
float BankAccount::makeDeposit()
{
return currentBalance;
}
void BankAccount::setDeposit(float howmuchDeposit)
{
currentBalance += howmuchDeposit;
}
int main()
{
float howmuchWithdraw, howmuchDeposit, balance;
char namee = 'n';
string name;
name+=namee;
char input = 's';
string enterinput;
enterinput+=input;
char transaction;
while (1)
{
BankAccount Customer;
cout << " Enter username:" << endl;
cin.width(20);
cin >> name;
for (unsigned int i = 0; i < name.length(); i++)
if ((name[i]>='A') && (name[i]<='Z'))
name[i] += 32;
Customer.setName(name);
cout << Customer.getName();
cout << " Starting balance: " << endl;
cin.width(10);
cin >> balance;
Customer.setBalance(balance);
cout << Customer.getBalance();
cout << " " << Customer.getID();
cout << "When you are finished entering data. Type 'done'. " << endl;
ofstream myfile;
myfile.open("Data.txt");
myfile.close();
// How to make all names/initial balances store in data??
//outer loop
do
{
int ID;
ID = Customer.getID();
cout << "Enter Customer name or userID to enter bank account:";
cin >> enterinput;
for (unsigned int i = 0; i < enterinput.length(); i++)
if ((enterinput[i]>='A') && (enterinput[i]<='Z'))
enterinput[i] += 32;
if (enterinput == name)
{
cout << "Welcome back " << name, Customer.getID();
}
// how to make ID = name?
else if (input = ID)
{
cout << "Welcome back " << name, Customer.getID();
}
else if ((enterinput == "quit")||(enterinput == "q"))
{
cout << "Goodbye" << endl;
exit (1);
}
else
{
cout << "Name/UserID not found. Enter another Name, UserID, or Q or quit." << endl;
}
//outer loop, need to search through data
// inner loop
do{
cout << " Do you want to check your balance (B), "
<< " make a deposit (D)"
<< " or withdraw (W)? "
<< " Finished (F) " << endl;
cin >> transaction;
if (transaction == 'B')
{
cout << " Balance is: $" << Customer.getBalance();
}
else if (transaction == 'D')
{
cout << " Deposit how much?" << endl;
cin >> howmuchDeposit;
if (howmuchDeposit <0)
{
cout << "Invalid deposit." << endl;
}
else
{
Customer.setDeposit(howmuchDeposit);
cout << " Your balance is now: $" << Customer.makeDeposit();
}
}
else if (transaction == 'W')
{
cout << " Withdraw how much? " << endl;
cin >> howmuchWithdraw;
if (howmuchWithdraw > Customer.getBalance())
{
cout << "Withdrawal exceeds your account balance." << endl;
}
else if (howmuchWithdraw <0)
{
cout << "Withdrawal amount must be positive." << endl;
}
else
{
Customer.setWithdraw(howmuchWithdraw);
cout << " Your balance is now: $" << Customer.makeWithdraw();
}
}
} while (transaction != 'F');
} while (input != 'quit');
}
system("pause");
return 0;
}
Explanation / Answer
hi there what i see that`s there is a problem with the number generator when u make a variable static in a class all the objects of the class will have the same number no matter what happens the last value will be given to all the objects and to write to a file and close it and open it again i prefers using fstream variable to write to the end of the file and not losing the data inside the file here is an example void main() { fstream out; out.open("test.txt"); out.seekp(0,ios::end);//to make the pointer go to the end of the file outRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.