So, I am making this bank management system in c++ where I will have to give the user an option to create an account, deposit the money, withdraw it, and display the details. I also need to store in the array of objects so that the entire data can be displayed after the user exits. The restrictions are that I cannot use file handling. But it isn't working properly.
Please help.
When I run it, it keeps asking me for full name. How do I resolve this issue?
I feel like this issue is occurring because of the persons array of type bankaccount, but I don't see any other possible way to do this.
I have deleted the details of some functions because it became a lengthy block of code.
#include<iostream>
#include<string>
#include <time.h>
#include <cstdlib>
using namespace std;
class bankaccount {
private:
int id;
string name;
int cash;
int money;
int age;
public:
string get_name() {
return name;
}
int get_id() {
return id;
}
void withdraw();
void deposit();
int see_money();
bankaccount(int id1) {
id = id1;
cout << "\n Enter Full Name:";
getline(cin, name);
}
friend ostream& operator<<(ostream& os, const bankaccount& d);
};
ostream& operator<<(ostream& os, bankaccount& d) {
os << "\n Your name is:" << d.get_name();
os << "\n Your id is:" << d.get_id();
os << "\n You have a total of : " << d.see_money();
}
int main() {
bankaccount persons[100] = 0;
int option;
int id;
int number = 0;
cout << "BANKING MANAGEMENT SYSTEM!" << endl;
cout << "-------------------------------------------------------------------------------";
while (1) {
cout << "\nEnter 1 to create an account. Enter 2 to deposit money. Enter 3 to withdraw money. Enter 4 to check money. Enter 5 to display. Enter 6 to exit";
cin >> option;
switch (option) {
case 1: {
bankaccount p(number);
persons[number] = p;
cout << "Your ID is:" << number << endl;
number++;
break;
}
case 2: {
cout << "\n Enter Your ID:";
cin >> number;
persons[number].deposit();
break;
}
case 3: {
cout << "\n Enter Your ID:";
cin >> number;
persons[number].withdraw();
break;
}
case 4: {
cout << "\n Enter Your ID:";
cin >> number;
persons[number].see_money();
break;
}
case 5: {
cout << "\n Enter Your ID:";
cin >> number;
cout << persons[number];
break;
}
}
}
}
bankaccount persons[100]=0;
Here you construct 100 objects of your bankaccount.
Your bankaccount constructor has these 2 lines:
cout<<"\n Enter Full Name:";
getline(cin,name);
So each time you create a bankaccount object, you're prompted for its name. You probably didn't intend that. You need to de-couple this, so asking the user for the Full Name, assigning it to a bankaccount, and constructing a bankaccount object are separate.
e.g. you can create option 6 to assign a name to the bankaccount instead of doing it inside the constructor of your bankaccount class.
If you aren't restricted the use of pointers or you don't have to specifically store it in a static array you can try this. You can create an array of bankaccount type pointers. In this case an array of 100 pointers. Each pointer will point to a separate object of bankaccount. https://imgur.com/a/KflNVbc for a better visual understanding.
You can create it like this:
bankaccount* persons[100];
So in case 1, when you create a new account and add it to the array you can do this:
persons[number] = new bankaccount();
number++;
This way you won't have to change your constructor. Just a cool way of doing this.
Related
Below program take three details of family members i.e name,age and occupation. Stores them in txt file.
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <fstream>
using namespace std;
class Dewan_Family
{
char name[50]; // array for name of the family member
int age; // integer for age of the family member
char oc[50]; // array for occupation of family member
public:
void getdata(); // This func. to collect information
void displaydata(); // This func to display information
};
void Dewan_Family::getdata() // creating func. to get family members info
{
cout << "Enter the Name of the Family member: "; cin.getline(name,50);
cout << "Enter the Age of the member: ";
cin >> age;
cin.ignore();
cout << "Enter the occupation of the member: "; cin.getline(oc,50);
}
void Dewan_Family::displaydata() // creating func. to display info
{
cout << "Name of the Dewan family's member: " << name << endl;
cout << "Age of the member: " << age << endl;
cout << "Occupation of the member: " << oc << endl;
}
int main()
{
Dewan_Family obj[5]; // created object array of class Dewan_Family
fstream file; // created object to handle file
file.open("family.txt", ios_base::app | ios_base::in); // created and attached file with the object.
char ch;
int choice; int j=0;
cout << "Main Menu ! Choose any option 1-3.\n";
cout << "1. To add Members\n";
cout << "2. To Display all Members\n";
cout << "3. Exit\nChoice: ";
cin >> choice;
cin.ignore();
cout << "\n";
switch(choice)
{
case 1: ch='y';
do{
obj[j].getdata();
file.write((char*)&obj[j],sizeof(obj[j]));
j++;
cout << "Data added successfully !.\nDo you want to continue to add: y or n: ";
cin >> ch;
}while(ch =='y' || ch == 'Y');
break;
case 2: file.seekg(0);
while(file)
{
file.read((char*)&obj[j],sizeof(obj[j]));
obj[j].displaydata();
j++;
}
break;
case 3: exit(1);
};
return 0;
}
My question is as follow:-
1) This little program successfully write data(with write function) to family.txt file. But when I see data by opening this family.txt file, data appears in weird form.
for example if I enter below details
Name = Ashok Dewan
Age = 25
occupation = job
then data in the text file is like this
Ashok #{}#1.^.36 .. ) job#..#...
2) when I read data(with read func.) It display like below
Name of the Dewan family's member: Ashok Dewan
Age of the member: 25
Occupation of the member: Job
Name of the Dewan family's member: ##$8&
Age of the member: 42565571
Occupation of the member: {...&6^
It displays other data also which I did not enter. I don't know Why it is showing extra data. Is this because write() and Read() use binary mode also by default? Please check this code at your end.
I am using latest codeblocks
Please help me to understand this behaviour?
In your code you are not checking if read is successful before using it, so the last obj created from a failed read. a possible fix is checking before serializing to an object:
case 2: file.seekg(0);
while(file)
if(file.read((char*)&obj[j],sizeof(obj[j]))){
obj[j].displaydata();
j++;
}
break;
Another solution might be to change the while statement as follows:
case 2: file.seekg(0);
while(file.read((char*)&obj[j],sizeof(obj[j])))
{
obj[j].displaydata();
j++;
}
break;
I have written my code for an employee management system that stores Employee class objects into a vector, I am getting no errors until I try and compile, I am getting the error: C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion). But I am not sure why any help would be great, Thank you!
// Employee Management System
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Employee
{
public:
Employee();
string GetName();
string GetStatus();
float GetSalary();
int GetAge();
int GetYearHired();
private:
string m_Name;
string m_Status;
float m_Salary;
int m_Age;
int m_YearHired;
};
Employee::Employee()
{
m_Salary = 0;
m_Age = 0;
m_YearHired = 0;
}
string Employee::GetName()
{
string fName;
string lName;
cout << "Please enter the new employee's first name: ";
cin >> fName;
cout << "Please enter the new employee's last name: ";
cin >> lName;
m_Name = fName + lName;
return m_Name;
}
string Employee::GetStatus()
{
string status;
cout
<< "Please enter the employee's status (full time, part time, or manager): ";
cin >> status;
return m_Status;
}
float Employee::GetSalary()
{
float salary;
cout << "Please enter the employee's salary: ";
cin >> salary;
return m_Salary;
}
int Employee::GetAge()
{
int age;
while (true)
{
cout << "Please enter the employee's age: ";
cin >> age;
if (age > 0)
break;
else
cout << "Error: Please enter a positive value.";
}
return m_Age;
}
int Employee::GetYearHired()
{
int yearHired;
cout << "Please enter what year the employee was hired: ";
cin >> yearHired;
return m_YearHired;
}
class Staff
{
vector<Employee*> emps;
vector<Employee*>::const_iterator iter;
public:
Staff();
virtual ~Staff();
void Add();
void Remove();
void Clear();
void Display();
};
Staff::Staff()
{
emps.reserve(20);
}
Staff::~Staff()
{
Clear();
}
void Staff::Add()
{
Employee* emp = new Employee;
emp->GetName();
emp->GetStatus();
emp->GetSalary();
emp->GetAge();
emp->GetYearHired();
emps.push_back(emp);
}
void Staff::Remove()
{
Employee* emp;
cout << "Which employee would you like to remove?";
emp->GetName();
iter = find(emps.begin(), emps.end(), emp->GetName()); // Trying to find the employee in the datbase.
if (iter != emps.end()) // If the employee is found in the vector it is removed.
{
cout << "\n" << *iter << " was removed\n\n";
emps.erase(iter); // removes employee from the vector.
}
else // If the employee is not found in the vector, it tells the user that the employee was not found.
{
cout << "Employee not found, please choose anoter employee.\n\n";
}
}
void Staff::Clear()
{
cout << "\nDo you really want to clear all employees? (yes/no)\n"; // Asking the user if they want to clear the database.
string response;
// Storing the response of the user.
cin >> response; // Getting the users response (yes/no).
if (response == "yes") // If response is yes.
{
vector<Employee*>::iterator iter = emps.begin(); // Declares an iterator for the emps vector and sets it to the beginning of the vector.
for (iter = emps.begin(); iter != emps.end(); ++iter) // Iterates through vector.
{
delete *iter; // Deletes the iterators in the vector, freeing all memory on the heap.* iter = 0;
// Sets iterator to zero so it does not become a dangling pointer.
}
emps.clear(); // Clear vector of pointers.
}
else // If response is no.
{
cout << "\nAll employee's remain in the database.\n";
}
}
void Staff::Display()
{
Employee* emp;
if (emps.size() == 0) // Checking to see if the database is empty.
cout
<< "\nThere are no employee's in the database, add employee's to view them here.\n ";
else // If the cart contains any items.
{
cout << "\nThe database contains: \n";
for (iter = emps.begin(); iter != emps.end(); ++iter) // Displaying the inventory.
{
cout << "-------------------------------------------------";
cout << "Employee's Name : " << emp->GetName() << endl;
cout << "Employee's Status : " << emp->GetStatus() << endl;
cout << "Employee's Salary : " << emp->GetSalary() << endl;
cout << "Employee's Age : " << emp->GetAge() << endl;
cout << "Year employee was hired : " << emp->GetYearHired() << endl;
cout << "-------------------------------------------------";
}
}
}
int main()
{
int option = 0;
Staff stf;
// Welcoming the user to the Employee Management System program.
cout
<< "Welcome to our Employee Management System! To get started see the menu options below :\n ";
// Main loop
while (option != 5) // The loop will repeat until the user enters 5 as the option.
{
cout << "------------------------------------------------------------------------------------- - ";
cout << "\nMenu Options: \n";
cout << "\nTo select an option, please type in the number that corresponds to that option.\n ";
cout << "1 - Add an Employee\n2 - Remove an Employee\n3 - Clear the database\n4 - Display Employee's in Database\n5 - Quit" << endl;
cout << "\nWhat would you like to do? ";
cout << "------------------------------------------------------------------------------------- - ";
// Start of the validity check.
bool validInput = false;
while (!validInput) // The loop will repeat until the users input is valid.
{
cin >> option; // User inputs first option choice.
validInput = true; // Assign the input as valid.
if (cin.fail()) // Tests to make sure the value assigned is valid for the variable type.
{
cout << "\nPlease choose a menu option by number\n";
cin.clear(); // Clears stream error.
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Removes an invalid characters.
validInput = false; // Sets the input back to false, repeats the loop.
}
}
switch (option)
{
case 1:
{
stf.Add();
break;
}
case 2:
{
stf.Remove();
break;
}
case 3:
{
stf.Clear();
break;
}
case 4:
{
stf.Display();
break;
}
case 5: // If option = 5.
cout << "\nThank you for using the Employee Management Program!\n";
// Thanks the user for using the Employee Management program.
break;
default: // If the user does not put in a valid option, it tells them to try again.
cout << "\nThat's not a valid option. Please try again.\n";
break;
}
}
system("pause");
return 0;
}
Problem
std::find is going to compare the Employee *s in emps against the std::string that is returned by GetName. There is no comparison operator defined for Employee * that does this. We can make one, but because the behaviour of GetName is to Get and name for the user, not simply return the Employee's name this will quickly become a mess.
Solution
First Stop storing pointers to Employees in the vectors. This simple change will eliminate the vast majority of your past, present and future pain. In general, use new as little as possible (Why should C++ programmers minimize use of 'new'?) and when you do find yourself needing new, prefer a smart pointer.
vector<Employee*> emps;
becomes
vector<Employee> emps;
that has a ripple effect through your code, not the least of which is
void Staff::Add()
{
Employee* emp = new Employee;
emp->GetName();
emp->GetStatus();
emp->GetSalary();
emp->GetAge();
emp->GetYearHired();
emps.push_back(emp);
}
must become
void Staff::Add()
{
Employee emp;
emp.GetName();
emp.GetStatus();
emp.GetSalary();
emp.GetAge();
emp.GetYearHired();
emps.push_back(emp);
}
But also look into emplace_back and strongly consider getting the user input and then constructing emp around it.
bool operator==(const Employee & rhs) const
{
return m_Name == rhs.m_Name;
}
or a friend function
bool operator==(const Employee & lhs,
const Employee & rhs)
{
return lhs.m_Name == rhs.m_Name;
}
and then change the call to find to compare Employees
iter = find(emps.begin(), emps.end(), emp); // Trying to find the employee in the datbase.
This may lead to more problems because iter is a const_iterator and a member variable (Rubber Ducky wants a word with you about this). Also completely ignores the fact that there are a few dozen more logical mistakes in the the code.
It seems to me (EDIT: had a commented out) string response; declaration before
cin >> response; // Getting the users response (yes/no).
Hope this points you in the right direction
EDIT:
It's there but it's commented out. Try:
cout << "\nDo you really want to clear all employees? (yes/no)\n";
// Asking the user if they want to clear the database.
string response;
// Storing the response of the user.
cin >> response; // Getting the users response (yes/no).
if (response == "yes") // If response is yes.
And I'd double check all the code to avoid the comments interfering with the code
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have this c++ assignment....and iam having a bit of a problem since iam used more to c#
i created a class student in order to create an array with different elements...the problem is how can i see the array in the functions in the class part...inorder to fill it with students details and display it..here is the code:
#include <iostream>
#include <string>
#include <array>
#include <iomanip>
using namespace std;
class student{
private:
int id, age;
string fname, lname, cob;
char s;
int stdcount;
public:
void addstd();
void searchstd(int n);
void displayinfo();
void deletestd(int n);
void displayrange();
void modifyinfo(int n);
};
void student::addstd()
{
cout<<"Enter ID Number"<<endl;
cin>>id;
cout<<"Enter First Name"<<endl;
cin>>fname;
cout<<"Enter last Name"<<endl;
cin>>lname;
cout<<"Enter age"<<endl;
cin>>age;
cout<<"Enter student's Sex(F or M) "<<endl;
cin>>s;
cout<<"Enter Country of birth"<<endl;
cin>>cob;
}
void student::displayinfo()
{
for(int i=0;i<100;i++)
{
cout<<ar[i];
}
}
void student::searchstd(int m)
{
}
void main()
{
student s;
student std[100];
int choice;
do{
cout << "-----------Menu------------" << endl;
cout << " 1. Add a student " << endl;
cout << " 2. Search for student by ID " << endl;
cout << " 3. Display all students information " << endl;
cout << " 4. Remove a students " << endl;
cout << " 5. Display students aged between 34 - 50 " << endl;
cout << " 6. Modify a student's information " << endl;
cout << " 7. Exit " << endl;
cout << " Enter your choice 1, 2, 3, 4 ,5,6,7 " << endl;
cin>>choice;
switch(choice) {
case 1:
for(int i=0;i<3;i++)
{
std[i].addstd();
}
break;
case 2:
int numid;
cout <<"enter id "<<endl;
cin>>numid;
s.searchstd(numid);
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
cout << "Please enter 1, 2, 3, 4, 5, 6 or 7 : " << endl << endl;
}
}while(choice!=7);
}
Your problem has very little to do with C++ and everything to do with proper design. Even if you used C#, you would have very little, if any justification for creating a student object as the way you did for C++.
First, as others have mentioned, a student should only know about themself (first name, last name). It isn't a student's responsibility to keep track of other students. A very rough design would look like this:
#include <string>
class student {
private:
int id, age;
std::string fname, lname, cob;
char s;
public:
student() {}
void setId(int n) { id = n; }
void setFirstName(const std::string& s) { fname = s; }
void setLastName(const std::string& s) { lname = s; }
//etc..
int getId() const { return id; }
std::string getFirstName() const { return fname; }
std::string getLastName() const { return lname; }
// etc...
};
That is all a student should have. Setting and getting the student's information. Nothing more, nothing less. You can embellish this by adding a constructor to easily create an entire student in one call (if you want to do this). But this is the basic gist of what a student class should look like.
Now you create an array of these students:
typedef std::array<student, 100> StudentList; // creates an array of 100 students.
or preferably, a vector< student >, so that it becomes a lot easier to handle:
#include <vector>
//..
typedef std::vector<student> StudentList;
Then you use StudentList as your array / vector of students.
It then becomes trivial when it comes to display the students -- just go through the array one by one and display the students information.
Modify your displayInfo function to take an input
void student::displayInfo(student ar[], int numStudents)
{
for(int i=0;i<numStudents;i++)
{
cout<<ar[i]; // if you have << operator defined
}
}
Then in main you could say:
student std[100];
displayInfo (std, 100); // this is passing your array in (you need to populate it though)
Im having a issue with my employee class. The program itself has a sample employee and allows the user to input more employees to the program. The issue I am having is that I can add in 1 new employee and print the list of both the sample employee and the newly added employee, however when i try to add in a second employee and print, the second one with over write the first added employee.
Im not sure if the issue is with the array im using (which has 4 elements, number, first name, last name and department) or if its with the function thats being called, below the code ive done up.
header file:
#include <iostream>
using namespace std;
class employee
{
public :
employee();
employee(int, char*, char*, char*); // employee works number, name, department
void Set( int, char*, char*, char*); // set the number, name and dept
void Print();
void printmenu();
~employee(); //destructor
private:
int e_num; // employee number
char e_fname[30];
char e_lname [30];
char e_dept[30];
};
CPP File:
#include <iostream>
#include "employee.h"
using namespace std;
employee::employee()
{
}
employee::employee(int num, char* fname, char* lname, char* dept)
{
Set(num, fname, lname, dept);
}
void employee::Set( int num, char* fname, char* lname, char* dept)
{
if (num < 0 )
{
return ; // add in code here to give error message if works is less than 0
}
e_num = num;
strcpy (e_fname, fname);
strcpy (e_lname, lname);
strcpy (e_dept, dept);
}
void employee::Print()
{
cout << e_num <<" \t " << e_fname <<" "<< e_lname <<" \t " << e_dept << " \n";
}
void employee::printmenu()
{
cout << "EMPLOYEE MENU\n"
<< "~~~~~~~~~~~~~\n"
<< "1. Add New Employee\n"
<< "2. Edit Employee\n"
<< "3. Delete Employee\n"
<< "4. Print Employee List\n"
<< "5. Exit\n";
}
employee::~employee()
{
}
Main:
#include <iostream>
#include "employee.h"
using namespace std;
void main (void)
{
char input;
bool done = false;
employee emp1(1, "Joe", "Bloggs", "Customer Service");// sample employee
int empcount = 1;
employee empnew[20];
int num, j=0, k=0; // num is employee number
char* fname = new char [20]; // employee first name
char* lname = new char [20]; // employee last name
char* dept = new char [30]; // employee department
while(!done)
{
employee pmenu;
pmenu.printmenu();
cout <<"Please make your selection:";
cin >> input;
switch (input)
{
case '1': // add new employee
cout<<"\nEnter Employee Number:";
cin >> num;
cout<<"\nEnter first name:";
cin >> fname;
cout<<"\nEnter Last name:";
cin >> lname;
cout << "\nEnter Department:";
cin >> dept;
cout << "\n";
empnew[j].Set(num, fname, lname, dept);
empcount++;
cout << "New employee added:"<< fname <<" "<<lname << "\n";
cout<< "New Number of Employees:" << empcount<< "\n\n";
break;
case '2': //Edit Employee
// enter stuff here
break;
case '3': //Delete Employee
// enter stuff here
break;
case '4': // print employee list
cout <<"Total number of Employees:"<<empcount<<endl;
cout <<"Number \t Employee Name \t Department \n" ;
if (empcount == 1)
{
emp1.Print();
}
else
{
emp1.Print();
empnew[j].Print();//print input
}
cout <<"\n";
break;
case '5':
cout << "Program closing\n";
done = true;
break;
}
}
getchar();
return ;
}
(sorry for posting all the code, didnt want to leave something important out)
Ive tried several different ways to get the 2nd (and subsequent employee) to print but which ever way i try to change the array or the function i get errors and the code wont compile.
I thought when added the new employee to use
empnew[j]= new employee (num ,fname,lname,dept);
but that gives me "Error: no operator "=" matches these operands"
along with many other variations of that which dont work either.
As you might guess ive just started with c++ so any help would be greatly appricated.
You set employee data into empnew[j] array item, yet j variable never changes. Judging from this snippet:
empnew[j].Set(num, fname, lname, dept);
empcount++;
probably you should change into to
empnew[empcount++].Set(num, fname, lname, dept);
The new operator returns a pointer and you have an array of values.
Either you have to create an array of pointers, or (what I recommend) implement a copy-constructor and use copying:
empnew[j] = employee (num ,fname, lname, dept);
I have been working on a trivial assignment to get used to coding. I am designing an ATM machine and at the moment it is composed of 2 classes:
BankAccount.cpp
Constructor for different types of account
Only has balance as a member
Transaction.cpp
Performs a method on the BankAccount (i.e make deposit, make withdrawl & get balance)
Problem: BankAccount is automatically initialized to a balance of 10 which is undesired. So for example, if I made a checking account and chose to deposit $10, balance would print out $20.
//BankAccount.h
//This class will simply take in a bank account
//with a balance, other classes will use a bank account object
//such as saving/checkings and perform operations on the
//balance
#ifndef BANK_ACCOUNT_H
#define BANK_ACCOUNT_H
class BankAccount {
private:
float balance;
public:
BankAccount ();
float getBalance ();
void makeDeposit ();
void makeWithdrawl ();
};
#endif
//BankAccount.cpp
#include "BankAccount.h"
#include <iostream> //remove once done *not to self
using namespace std; //remove once done *note to self
BankAccount::BankAccount() {
balance = 0.00;
}
float BankAccount::getBalance() {
return balance;
}
void BankAccount::makeDeposit() {
cout << "How much would you like to deposit: ";
float deposit_value;
cin >> deposit_value;
balance += deposit_value;
}
void BankAccount::makeWithdrawl() {
cout << "How much would you like to withdrawl: ";
float withdrawl_value;
cin >> withdrawl_value;
balance -= withdrawl_value;
}
//Transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H
class Transaction {
private:
BankAccount m_bao;
public:
Transaction(BankAccount&);
void displayOptions();
void printReciept();
};
#endif
//Transaction.cpp
#include "BankAccount.h"
#include "Transaction.h"
#include <iostream>
using namespace std;
Transaction::Transaction(BankAccount& bao) {
m_bao = bao;
}
void Transaction::displayOptions() {
cout << "\nPlease make a choice\n\n";
cout << "1: Make Deposit\n";
cout << "2: Make Withdrawl\n";
cout << "3: Check Balance\n";
int choice;
cin >> choice;
switch (choice) {
case 1:
m_bao.makeDeposit();
break;
case 2:
m_bao.makeWithdrawl();
break;
case 3:
m_bao.getBalance();
break;
}
}
void Transaction::printReciept() {
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
}
int main () {
BankAccount checking;
Transaction q(checking);
q.displayOptions();
q.printReciept();
}
I am sure the answer is right in front of my eyes, but my brain is just fried right now. I will continue to look for the solutions and let you guys know if my problem has been solved yet.
[EDIT]
Alright, now I am trying to make it so that the customer can choose to perform transactions on either Checking or Savings account. Currently I got it looking like this in my main():
int main () {
BankAccount checking(0.00);
BankAccount savings(0.00);
Transaction c(checking);
Transaction s(savings);
for(int i = 0; i < 10 ; i++) {
cout << "Make an option" << endl;
cout << "1. Checking " << endl;
cout << "2. Savings" << endl;
int choice;
cin >> choice;
if (choice == 1) {
c.prompt();
c.printReciept();
}
else {
s.prompt();
s.printReciept();
}
}
}
It works fine, but I would like to make this process more OOP-alized, if that makes sense :)
One option I was trying to look into was making a prompt function which would belong to Transaction.cpp. This would do everything that is done in main, except initializing the objects of course.
Your problem is this line:
cout << "Current balance is now: " << m_bao.getBalance() + '\n';
Which the compiler sees as:
cout << "Current balance is now: " << (m_bao.getBalance() + '\n');
'\n' is 10 as an int, so you get this:
cout << "Current balance is now: " << (m_bao.getBalance() + 10);
You probably meant to do this:
cout << "Current balance is now: " << m_bao.getBalance() << '\n';
Remember that in C++, + almost always means "add these two numbers".