I have to write a program for bank.The classes if you will say I will provide but the problem is here,
My Problem is that in my main program I first call a function called BankProgram(); But there are other statements in the function after the function is being called The below code isnt executed at all
cout << "Enter y for opening menu again and n for exiting\t";
cin >> option;
while (option != 'y' || option != 'n')
{
cout << "Enter right value Please! only y or n: ";
char option1;
cin >> option1;
if (option1 == 'y' || option1 == 'n')
{
break;
}
}
if (option == 'y')
{
goto label;
}
else if (option == 'n')
{
cout << "The program is ending now! ";
}
Although I want to display the menu again for the user to do anything in the bank but the program terminates and exits I dont know why.I think there is a problem with object lifetime but I think that must not be the problem
Any help is appreciated.
This is the AddAccount Class
#pragma once
#include<iostream>
#include<string>
#include <vector>
using namespace std;
class Accounts
{
vector<int> Account_ID;
vector<string> AccountType;
vector<int> Customer_ID;
vector<int> Account_Balance;
public:
Accounts();
void WithDraw(int);
void Deposit(int,string,int,int);//Also can be named as add Account
void Balance();
void DeleteAccount(int);
int getAccountsNumber();
};
//Definning classes methods
Accounts::Accounts()
{
//no need to initialize vectors.They work perfect without initializing.C++ has done work for it
}
void Accounts::Deposit(int AID,string AT,int CID,int AB)
{
this->Account_ID.push_back(AID);
this->AccountType.push_back(AT);
this->Customer_ID.push_back(CID);
this->Account_Balance.push_back(AB);
}
void Accounts::WithDraw(int index)
{
cout << "\nThe Account ID of " << (index + 1) << " person is equal to: "
<< this->Account_ID[index] << endl;
cout << "\nThe Account Type of " << (index + 1) << " person is equal to: "
<< this->AccountType[index] << endl;
cout << "\nThe Customer ID of " << (index + 1) << " person is equal to: "
<< this->Customer_ID[index] << endl;
cout << "\nThe Account Balance of " << (index + 1) << " person is equal to: "
<< this->Account_Balance[index] << endl;
}
void Accounts::DeleteAccount(int index)
{
Account_ID.erase(Account_ID.begin()+index);
AccountType.erase(AccountType.begin()+index);
Customer_ID.erase(Customer_ID.begin()+index);
Account_Balance.erase(Account_Balance.begin()+index);
//Displaying that the account is successfully removed from the bank
cout << "\nThe Account ID of " << (index + 1) << " was successfully removed from the bank";
cout << "\nThe Account Type of " << (index + 1) <<" was successfully removed from the bank";
cout << "\nThe Customer ID of " << (index + 1) << " was successfully removed from the bank";
cout << "\nThe Account Balance of " << (index + 1) << " was successfully removed from the bank";
}
//It will display all the balance in the bank
void Accounts::Balance()
{
//The static int is not changed on the iteration where ever used in the loop or where ever
static int sum = 0;//To store the sum of all balance
for (unsigned int i = 0; i < Account_Balance.size(); i++)
{
sum = sum + Account_Balance[i];
}
cout << "The total balance in the bank is equal to : " << sum;
}
int Accounts::getAccountsNumber()
{
return Account_ID.size();
}
This is the Customer Class
#pragma once
#include<string>
using namespace std;
#include"Accounts.h"
#include"Manager.h"
class Customer
{
vector<string> Name;
vector<int> ID;
public:
Customer();
void AddCustomer(string,int);
void PrintAllCustomersData();
void DeleteCustomer(int);
void Print_CustomerDetails(int);
string getCustomerName(int);
};
void Customer::AddCustomer(string n, int id)
{
this->Name.push_back(n);
this->ID.push_back(id);
cout << "\nThe customer " << n << "with Id: " << id << " was successfully added in the Bank.";
}
void Customer::PrintAllCustomersData()
{
for (unsigned int i = 0; i < ID.size(); i++)
{
cout << "\nThe ID of " << (i + 1) << "Customer is : " << ID[i] << " and NAME is : " << Name[i];
}
}
void Customer::DeleteCustomer(int index)
{
Name.erase(Name.begin() + index);
ID.erase(ID.begin() + index);
cout << "\nThe customer with Name : " << Name[index] << " and ID: " << ID[index] << " was successfully deleted\n";
}
void Customer::Print_CustomerDetails(int index)
{
cout << "The Customer Name is : " << Name[index] << endl;
cout << "The Id of Customer is : " << ID[index] << endl;
}
string Customer::getCustomerName(int index)
{
return (Name[index]);
}
This is the Manager Class
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Manager
{
string Name;
string Branch;
int ID;
public:
void Print_ManagerDetails();
friend ostream& operator<<(ostream&, const Manager& M);
friend istream& operator>>(istream&, Manager& M);
};
void Manager::Print_ManagerDetails()
{
cout << "\nThe ID of Manager is : " << ID << endl;
cout << "\nThe Name of Manager is : " << Name << endl;
cout << "\nThe Branch of Manager is : " << Branch << endl;
}
istream& operator>>(istream& input, Manager& M) {
input >> M.ID >> M.Name>>M.Branch;
return input;
}
ostream& operator<<(ostream& output, const Manager& M) {
output << "\nThe ID of Manager is : " << M.ID<<endl;
output << "\nThe Name of Manager is : " << M.Name << "\nThe Branch of Manager is : " << M.Branch<<endl;
return output;
}
This is the Bank Class
#pragma once
#include"Customer.h"
#include"Accounts.h"
class Bank
{
Customer* customers_ptr;
Accounts* Accounts_ptr;
Manager manager;
public:
Bank();
void AddAccount(int,string,int,int);
void DeleteAccount(int);
void AddCustomer(string,int);
void DeleteCustomer(int);
int GetNoOfAccounts();
string GetCustomer_Name(int);
};
Bank::Bank()
{
cout << "\nThe program is in the bank class\n";
}
void Bank::AddAccount(int AID, string AT, int CID, int AB)
{
Accounts_ptr->Deposit(AID, AT, CID, AB);
}
void Bank::DeleteAccount(int index)
{
Accounts_ptr->DeleteAccount(index);
}
void Bank::AddCustomer(string name, int ID)
{
customers_ptr->AddCustomer(name, ID);
}
void Bank::DeleteCustomer(int index)
{
customers_ptr->DeleteCustomer(index);
}
int Bank::GetNoOfAccounts()
{
int num=Accounts_ptr->getAccountsNumber();
return num;
}
string Bank::GetCustomer_Name(int index)
{
string name = customers_ptr->getCustomerName(index);
return name;
}
This is the main program
#include<iostream>
#include<string>
#include<conio.h>
#include<algorithm>
#include"Bank.h"
#include"Customer.h"
#include"Manager.h"
#include"Accounts.h"
using namespace std;
void BankProgram();
void BankProgram()
{
Bank b1;
Accounts A1;
Manager m1;
char options;
cout << "\n\nEnter what you want to do \n1 for entering the managers data,\n2 for showing the managers data "
<< "\n3 for adding a customer in the bank\n4 for adding an account in the bank \n5 for deleting the customer\n"
<< "\n5 for deleting the account\n6 for getting customer name\n7 for getting No. of accounts"
<< "\n8 for seeing all the balance in the bank\nPress 'e' for exit";
cin >> options;
switch (options)
{
case '1':
{
//The manager class data
cout << "\nEnter the name,ID,Branch Of Manager: ";
cin >> m1;
break;
}
case '2':
{
cout << "\nThe data of Manager is :" << m1;
break;
}
case '3':
{
string Cname;
int CID;
cout << "\nEnter the name of customer: ";
cin >> Cname;
cout << "\nEnter the Customer ID: ";
cin >> CID;
b1.AddCustomer(Cname, CID);
break;
}
case '4':
{
int AID;
int CID;
int AB;
string AT;
cout << "\nEnter the name of Account ID: ";
cin >> AID;
cout << "\nEnter the name of Customer ID: ";
cin >> CID;
cout << "\nEnter the name of Account BALANCE: ";
cin >> AB;
cout << "\nEnter the name of Account Type: ";
cin >> AT;
b1.AddAccount(AID, AT, CID, AB);
break;
}
case '5':
{
int index;
cout << "\nEnter the customer which you want to delete: ";
cin >> index;
b1.DeleteCustomer(index);
break;
}
case '6':
{
int index;
cout << "\nEnter the account which you want to delete: ";
cin >> index;
b1.DeleteAccount(index);
break;
}
case '7':
{
int cn;
cout << "\nEnter the customer ID which you want to get name: ";
cin >> cn;
b1.GetCustomer_Name(cn);
break;
}
case '8':
{
b1.GetNoOfAccounts();
break;
}
case '9':
{
A1.Balance();
break;
}
case 'e':
{
cout << "The program is ending now: ";
break;
}
default:
{
cout << "\n\nEnter right value please: \n";
}
}
}
int main()
{
// BankProgram();
bool flag = true;
char option;
label:
BankProgram();
cout << "Enter y for opening menu again and n for exiting\t";
cin >> option;
while (option != 'y' || option != 'n')
{
cout << "Enter right value Please! only y or n: ";
char option1;
cin >> option1;
if (option1 == 'y' || option1 == 'n')
{
break;
}
}
if (option == 'y')
{
goto label;
}
else if (option == 'n')
{
cout << "The program is ending now! ";
}
}
Well it turns out to be very simple
Bank::Bank()
{
cout << "\nThe program is in the bank class\n";
}
void Bank::AddAccount(int AID, string AT, int CID, int AB)
{
Accounts_ptr->Deposit(AID, AT, CID, AB);
}
Accounts_ptr is an uninitialised pointer, using it results in a program crash. You must give Accounts_ptr a value in the Bank constructor.
You have the same issue with customers_ptr.
Related
I am a beginner in C++ and I come from a non-CS background. I was making this project of a Bank Management System using C++, but I am facing a problem. Whenever I am Depositing or withdrawing money, it is not getting updated when I do "2 Balance Enquiry". Can anyone help me understand why it is so and also How to fix it? I am new to this so please forgive me if this is a stupid question. Thanks
Here is my code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
//*************** C U S T O M E R ***************
class Customer {
string fname, lname;
long accno;
float balance;
static long count;
public:
Customer() {}
Customer(string fn, string ln, float b) {
count++;
fname = fn;
lname = ln;
balance = b;
accno = count;
}
long getaccno() {
return accno;
}
void greetnewcx() {
cout << "Hello, " << fname << " " << lname << endl;
cout << "Account Successfully Created. Account Number: " << getaccno();
cout<<"\nBalance: Rs." << balance;
}
void deposit(float dep) {
balance += dep;
cout << "Successfully deposited\nNew Balance: Rs." << balance;
}
void greeting() {
cout << "Hello, " << fname << " " << lname << endl;
}
void withdraw(float withdraw) {
balance -= withdraw;
cout << "Successfully withdrawn\nRemaining Balance: Rs." << balance;
}
friend ostream& operator<<(ostream& out, Customer& c);
};
long Customer::count = 0;
//*************** B A N K ***************
class Bank {
map<long, Customer> accounts;
public:
Bank() {
Customer account;
}
Customer open_account(string f, string l, float b);
void show_allaccounts();
void show_balance(long acno);
void deposit(long acno);
void withdraw(long acno);
void removeacc(long acno);
};
//*************** M A I N ***************
int main() {
Bank b;
Customer cx;
int choice;
string fname, lname;
long accountNumber;
float balance;
do {
cout << "\n\n\tSelect one option below ";
cout << "\n\t1 Open an Account";
cout << "\n\t2 Balance Enquiry";
cout << "\n\t3 Deposit";
cout << "\n\t4 Withdrawal";
cout << "\n\t5 Close an Account";
cout << "\n\t6 Show All Accounts";
cout << "\n\t7 Quit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "\nFirst Name: " << endl;
cin >> fname;
cout << "\nLast Name: " << endl;
cin >> lname;
cout << "Initial Balance: Rs." << endl;
cin >> balance;
cx = b.open_account(fname, lname, balance);
break;
case 2:
cout << "Enter Account Number: ";
cin >> accountNumber;
b.show_balance(accountNumber);
break;
case 3:
cout << "Enter Account Number: ";
cin >> accountNumber;
b.deposit(accountNumber);
break;
case 4:
cout << "Enter Account Number: ";
cin >> accountNumber;
b.withdraw(accountNumber);
break;
case 5:
cout << "Enter Account Number: ";
cin >> accountNumber;
b.removeacc(accountNumber);
break;
case 6:
b.show_allaccounts();
break;
case 7:
break;
default:
cout << "\nEnter corret choice";
exit(0);
}
} while (choice != 7);
return 0;
}
//*************** F U N C T I O N S **************
Customer Bank::open_account(string f, string l, float b) {
Customer cx(f, l, b); //calling constructor of Class Customer
accounts.insert(pair<long,Customer>(cx.getaccno(), cx));
cx.greetnewcx();
return cx;
};
void Bank::show_balance(long acno) {
map<long, Customer> ::iterator itr;
itr = accounts.find(acno);
cout << itr->second << endl;
}
void Bank::deposit(long acno) {
map<long, Customer> ::iterator itr;
itr = accounts.find(acno);
auto cx = itr->second;
cx.greeting();
cout << "\nEnter the amount to deposit: Rs.";
float d;
cin >> d;
cx.deposit(d);
}
void Bank::withdraw(long acno) {
map<long, Customer> ::iterator itr;
itr = accounts.find(acno);
auto c = itr->second;
c.greeting();
cout << "\nEnter the amount to withdraw: Rs.";
float d;
cin >> d;
c.withdraw(d);
}
void Bank::removeacc(long acno) {
accounts.erase(acno);
cout << "Account Deleted.";
}
void Bank::show_allaccounts() {
map<long, Customer> ::iterator itr;
for (itr = accounts.begin(); itr != accounts.end(); itr++) {
cout <<"Account No. " <<itr->first<< endl;
cout << itr->second<<endl;
}
}
//*************** OPERATOR OVERLOADED F U N C T I O N S **************
ostream& operator<<(ostream& out, Customer& c) {
out << c.fname << " " << c.lname << endl;
out << "Balance: Rs." << c.balance << endl << endl;
return out;
}
cx = b.open_account(fname, lname, balance);
This makes a copy of the Customer. You now have one Customer in cx and another one inside the map. You then modify the copy you stored in cx leaving the one inside the map untouched.
You do this elsewhere too:
Customer Bank::open_account(string f, string l, float b) {
Customer cx(f, l, b); //calling constructor of Class Customer
accounts.insert(pair<long,Customer>(cx.getaccno(), cx));
cx.greetnewcx();
return cx;
};
Here, you create a Customer in cx, but then you create another Customer inside the accounts structure (that's what insert does), then you return yet another Customer because cx goes out of scope. You only wanted to create one customer but you created three.
In sum, your code does this:
It creates a new Customer with Customer cx(....
It puts a copy of that Customer in the map with insert.
It returns a copy of that Customer using return.
The calling code than initializes an existing Customer to the value of the returned copy with cx = b.open_account....
You really only wanted one Customer, not four.
I have an assignment here that I've been working on for the past few hours and have been met with a problem. Whenever I compile and run the program in VS Code it throws me into an infinite loop where the text of the "menu" is printed repeatedly. I imagine this has something to do with the for(;;){ loop at the beginning of the menu.
I've deleted the for(;;){ statement at the beginning of the menu, and the continuous scrolling stopped, but I was unable to input any numbers (cases) and the program, essentially, just printed the menu and that was the end.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Student {
string name;
float GPA;
public:
string getName() const
{
return name;
}
float getGPA() const
{
return GPA;
}
void setName(string Name)
{
name = Name;
}
void setGPA(float gpa)
{
GPA = gpa;
}
Student(const string& name, float gpa)
: name(name)
, GPA(gpa)
{
}
Student()
{
}
void printDetails(Student s[], int n)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n; i++) {
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
float calcAverageGPA(Student s[], int n)
{
float avg = 0;
float sum = 0;
for (int i = 0; i <= n; i++) {
sum += s[i].getGPA();
}
avg = sum / n;
return avg;
}
float getGPAbyName(Student s[], int n, string name)
{
for (int i = 0; i <= n - 1; i++) {
if (s[i].getName() == name)
return s[i].getGPA();
}
return -1;
}
void showListByGPA(Student s[], int n, float gpa)
{
cout << setw(20) << "Name: " << setw(10) << "GPA: " << endl;
cout << "=========================================" << endl;
for (int i = 0; i < n - 1; i++) {
if (s[i].getGPA() > gpa)
;
cout << setw(20) << s[i].getName() << setw(10) << s[i].getGPA() << endl;
}
}
};
int main()
{
int n = 0;
int ch;
Student s[50];
string name;
float GPA;
float avg = 0;
cout << "======================" << endl;
cout << "(1): Add a student" << endl;
cout << "(2): Print the details of a student" << endl;
cout << "(3): Get the GPA of a Student by name." << endl;
cout << "(4): Get names of students based on GPA." << endl;
cout << "(6): Quit the program." << endl;
cout << "Enter your option" << endl;
switch (ch) {
case '1':
cout << "\nEnter student's name" << endl;
cin >> name;
cout << "Enter GPA" << endl;
cin >> GPA;
s[n] = Student(name, GPA);
n++;
break;
case '2':
s[0].printDetails(s, n);
break;
case '3':
cout << "\nEnter the name of a student" << endl;
cin >> name;
GPA = s[0].getGPAbyName(s, n, name);
if (GPA == -1) {
cout << "\nStudent not found!" << endl;
}
else
cout << "\nGPA is: " << endl;
break;
case '4':
cout << "\nEnter GPA: " << endl;
cin >> GPA;
s[0].showListByGPA(s, n, GPA);
break;
case '5':
avg = s[0].calcAverageGPA(s, n);
cout << "\nAverage GPA: " << avg << endl;
break;
case '6':
exit(0);
}
}
I suspect the problem resides in main(). I included the prior blocks of the program in case they were necessary to provide any suggestions.
You obviously want to read in "ch".
You've also made this an int, and don't zero-initialize, which can cause some undefined behaviour if you don't set it beforehand.
The switch case should be
switch(X){
case 1:
// Code
break;
}
etc..
The difference is "1" or 1. It evaluates an enumeration value, instead of strings.
For safety: you might want to add a case default, which is common practice.
I am supposed to write a program with two classes, Employee and Department. When the main() function runs, it asks the user to choose one of the six numbered options that are displayed and creates arrays for Employee and Department objects. I'm disregarding every other option except option 1, the Create Department Option, so the focus of this issue will be the Department class and the Department department[3] array.
There is a while loop in the main() function that continuously runs until the user decides to exit. If the user enters option 1, a Department array object is created, and then the user also enters the departmentID, departmentName, and departmentHeadName for that object. The while loop notifies the user if the array has three Employee objects. However, I am having difficulties because each departmentID needs to be unique. For example, I cannot enter 1 for the first array object's departmentID, and then enter 1 again for the second array object's departmentID. How do I check if the user's departmentID input already exists in a previous object?
#include <iostream>
#include <string>
using namespace std;
// Employee Class
class Employee
{
private:
string employeeID;
string employeeName;
string employeeDepartmentID;
double employeeSalary;
int employeeAge;
public:
void createEmployee()
{
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> employeeID;
cout << "Employee Name :";
cin >> employeeName;
cout << "Salary: $";
cin >> employeeSalary;
cout << "Age : ";
cin >> employeeAge;
cout << "Department ID : ";
cin >> employeeDepartmentID;
}
};
// Department Class
class Department
{
private:
string departmentID;
string departmentName;
string departmentHeadName;
public:
void createDepartment()
{
cout << "Please Enter Department Details: \n";
cout << "Department ID : ";
cin >> departmentID;
cout << "Department Name : ";
cin >> departmentName;
cout << "Head of Department : ";
cin >> departmentHeadName;
}
};
// Function prototype
void displayMenu();
// Client main function
int main()
{
Employee employee[5];
Department department[3];
int choice;
int departmentCount = 0;
int employeeCount = 0;
while (true)
{
displayMenu();
cin >> choice;
if (choice == 1 && departmentCount < 3)
{
department[departmentCount].createDepartment();
departmentCount = departmentCount + 1;
}
else if (choice == 1 && departmentCount >= 3)
{
cout << "\nThe array is full, you can not add any more Departments." << endl;
}
else if (choice == 2 && employeeCount < 5)
{
employee[employeeCount].createEmployee();
employeeCount = employeeCount + 1;
}
else if (choice == 2 && employeeCount >= 5)
{
cout << "The array is full, you can not add any more Employees." << endl;
}
else if (choice == 6)
{
cout << "Thank you, goodbye." << endl;
break;
}
}
return 0;
}
// Display menu function
void displayMenu()
{
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Write Out Data File" << endl;
cout << "4. Read In Data File" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
cout << "Please make a selection : ";
}
try this one
#include <iostream>
#include <string>
using namespace std;
// Employee Class
class Employee
{
private:
string employeeID;
string employeeName;
string employeeDepartmentID;
double employeeSalary;
int employeeAge;
public:
void createEmployee()
{
cout << "Please Enter Employee Details:" << endl;
cout << "Employee ID : ";
cin >> employeeID;
cout << "Employee Name :";
cin >> employeeName;
cout << "Salary: $";
cin >> employeeSalary;
cout << "Age : ";
cin >> employeeAge;
cout << "Department ID : ";
cin >> employeeDepartmentID;
}
};
// Department Class
class Department
{
private:
string departmentID;
string departmentName;
string departmentHeadName;
public:
void createDepartment()
{
cout << "Please Enter Department Details: \n";
cout << "Department ID : ";
cin >> departmentID;
cout << "Department Name : ";
cin >> departmentName;
cout << "Head of Department : ";
cin >> departmentHeadName;
}
public:
string getDepartmentID(){
return departmentID;
}
};
// Function prototype
void displayMenu();
// Client main function
int main()
{
Employee employee[5];
Department department[3];
int choice;
int departmentCount = 0;
int employeeCount = 0;
while (true)
{
displayMenu();
cin >> choice;
if (choice == 1 && departmentCount < 3)
{
department[departmentCount].createDepartment();
for(int i=0;i<departmentCount;i++)
{
if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
{
cout<<"already exists......................... \n";
}
}
departmentCount = departmentCount + 1;
}
else if (choice == 1 && departmentCount >= 3)
{
cout << "\nThe array is full, you can not add any more Departments." << endl;
}
else if (choice == 2 && employeeCount < 5)
{
employee[employeeCount].createEmployee();
employeeCount = employeeCount + 1;
}
else if (choice == 2 && employeeCount >= 5)
{
cout << "The array is full, you can not add any more Employees." << endl;
}
else if (choice == 6)
{
cout << "Thank you, goodbye." << endl;
break;
}
}
return 0;
}
// Display menu function
void displayMenu()
{
cout << "1. Create Department" << endl;
cout << "2. Create Employee" << endl;
cout << "3. Write Out Data File" << endl;
cout << "4. Read In Data File" << endl;
cout << "5. Display Salary Report" << endl;
cout << "6. -- Quit -- " << endl;
cout << "Please make a selection : ";
}
I used getDepartmentID function in Department Class to get department id from each department object
public:
string getDepartmentID(){
return departmentID;
}
there should be return type is string.because you have created departmentID as a string.
and I used For Loop in main function to compare relevant department id already exists or not
for(int i=0;i<departmentCount;i++)
{
if(department[i].getDepartmentID()==department[departmentCount].getDepartmentID())
{
cout<<"already exists......................... \n";
}
}
Okay, so I have a parent class called employee and 3 subclass called manager,researcher and engineer. I made a vector and want to list them. this is the how I process the making.
vector <Employee*,Manager*> EmployeeDB;
Employee *temp;
temp = new Manager(first, last, salary, meetings, vacations);
EmployeeDB.push_back(temp);
I have no problem in making the vector, my concern is listing the info. all 3 subclasses have firstname, lastname and salary but they're difference is that they have different data members which is unique, example the Manager has the int value vacation and the Engineer has the int value experience so on and so forth.
Employee.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef EMPLOYEE_h
#define EMPLOYEE_h
class Employee
{
public:
Employee();
Employee(string firstname, string lastname, int salary);
string getFname();
string getLname();
int getSalary();
virtual void getInfo();
private:
string mFirstName;
string mLastName;
int mSalary;
};
#endif
Employee.cpp:
#include "Employee.h"
#include <iostream>
#include <string>
using namespace std;
Employee::Employee()
{
mFirstName = "";
mLastName = "";
mSalary = 0;
}
Employee::Employee(string firstname, string lastname, int salary)
{
mFirstName = firstname;
mLastName = lastname;
mSalary = salary;
}
string Employee::getFname()
{
return mFirstName;
}
string Employee::getLname()
{
return mLastName;
}
int Employee::getSalary()
{
return mSalary;
}
void Employee::getInfo()
{
cout << "Employee First Name: " << mFirstName << endl;
cout << "Employee Last Name: " << mLastName << endl;
cout << "Employee Salary: " << mSalary << endl;
}
Main:
#include <vector>
#include <iostream>
#include <string>
#include "Employee.h"
#include "Engineer.h"
#include "Manager.h"
#include "Researcher.h"
using namespace std;
vector <Employee*> EmployeeDB;
Employee *temp;
void add()
{
int emp, salary, vacations, meetings, exp, c;
string first, last, type, school, topic;
bool skills;
do
{
system("cls");
cout << "===========================================" << endl;
cout << " Add Employee " << endl;
cout << "===========================================" << endl;
cout << "[1] Manager." << endl;
cout << "[2] Engineer." << endl;
cout << "[3] Researcher." << endl;
cout << "Input choice: ";
cin >> emp;
system("cls");
} while (emp <= 0 || emp > 3);
cout << "===========================================" << endl;
cout << " Employee Info " << endl;
cout << "===========================================" << endl;
cout << "Employee First name: ";
cin >> first;
cout << "Employee Last name: ";
cin >> last;
cout << "Employee Salary: ";
cin >> salary;
switch (emp)
{
case 1:
cout << "Employee numbers of meetings: ";
cin >> meetings;
cout << "Employee numbers of vacations: ";
cin >> vacations;
temp = new Manager(first, last, salary, meetings,vacations);
EmployeeDB.push_back(temp);
delete temp;
break;
case 2:
cout << endl;
cout << "[1]YES [2]NO" << endl;
cout << "Employee C++ Skills: ";
cin >> c;
if (c == 1)
{
skills = true;
}
else
{
skills = false;
}
cout << "Employee Years of exp: ";
cin >> exp;
cout << "(e.g., Mechanical, Electric, Software.)" << endl;
cout << "Employee Engineer type: ";
cin >> type;
temp = new Engineer(first, last, salary, skills, exp, type);
EmployeeDB.push_back(temp);
delete temp;
break;
case 3:
cout << "Employee School where he/she got his/her PhD: ";
cin >> school;
cout << "Employee Thesis Topic: ";
cin >> topic;
temp = new Researcher(first, last, salary, school, topic);
EmployeeDB.push_back(temp);
delete temp;
break;
}
}
void del()
{
}
void view()
{
for (int x = 0; x < (EmployeeDB.size()); x++)
{
cout << EmployeeDB[x]->getInfo();
}
}
void startup()
{
cout << "===========================================" << endl;
cout << " Employee Database " << endl;
cout << "===========================================" << endl;
cout << "[1] Add Employee." << endl;
cout << "[2] Delete Employee." << endl;
cout << "[3] List Employees." << endl;
cout << "[4] Exit." << endl;
cout << "Please Enter Your Choice: ";
}
int main(int argc, char** argv)
{
bool flag = true;
int choice;
do {
do
{
system("cls");
system("pause>nul");
startup();
cin >> choice;
} while (choice < 0 || choice >4);
switch (choice)
{
case 1:
add();
break;
case 2:
del();
break;
case 3:
view();
break;
case 4:
flag = false;
system("EXIT");
break;
}
} while (flag == true);
return 0;
system("pause>nul");
}
I am getting error on the view() function.
It says no operator<< matches these operands
binary '<<': no operator found which takes a right hand operand of type void etc etc.
The problem is that the getInfo has return type void and you are trying to put that return value into cout.
It's important to understand that the code std::cout << val actually calls the function operator<<(ostream& out, const objectType& val) where objectType is the type of 'val'.
In your case the type is void, and there is simply no implementation of operator<< that takes void as a type. hence the error "no operator found which takes a right hand operand of type void...".
In order to fix your issue you have a few options:
Change view() to be
for (...)
{
EmployeeDB[x]->getInfo();
}
Change getInfo() to return a string the info as you'd like:
std::string getInfo()
{
std::string info;
info =...
return info;
}
Create an operator<< for Employee and change view to call it:
view()
{
for (...)
{
std::cout << EmployeeDB[x];
}
}
I wanted to pass an object - which is also a vector - through a function using pointers.
I believe the issue arises on line 75 (transaction(&user, userID);) where I attempt to pass the object "user" through to the function "transaction".
I don't believe the classes are relevant so I left them out. Thank you so much in advance. Here is the source.cpp code:
void transaction(vector<Customer *> &user, int userID);
void newAccount(vector<Customer*> & user, int userID);
void intr(vector<Customer*> & user, int userID, int account, int interest);
void loans(vector<Customer*> & user, int userID, int account, int loan);
int main()
{
vector<Customer> user(3);
user[0].setname("Josh");
user[0].setID(1);
user[0].bank[0].setbalance(100);
user[0].bank[1].setbalance(101);
user[0].bank[0].setoverdraft(200);
user[0].bank[1].setoverdraft(201);
user[0].accounts = 1;
user[0].setpin(1202);
user[1].setname("John");
user[1].setID(2);
user[1].bank[0].setbalance(102);
user[1].bank[0].setoverdraft(202);
user[1].accounts = 0;
user[1].setpin(1203);
user[2].setname("Jack");
user[2].setID(3);
user[2].bank[0].setbalance(103);
user[2].bank[0].setoverdraft(203);
user[2].accounts = 0;
user[2].setpin(1204);
int input;
int userID;
int pin;
int account;
bool menu = true;
//Menu
while (menu)
{
cout << " - Enter '1' to display all customer names and ID's." << endl;
cout << " - Enter '2' for further transactions." << endl;
cout << " - Enter '3' to make a quick withdrawal of " << char(156) << "10 from an account." << endl;
cout << " - Enter '4' to exit." << endl;
cin >> input;
// List
if (input == 1)
{
for (int i = 0; i < user.size(); i++)
{
cout << "[Name: " << user[i].getname() << "\t" << "ID: " << user[i].getID() << "]" << endl;
}
cout << endl;
}
// Transactions
else if (input == 2)
{
cout << "Enter Customer ID: ";
cin >> userID;
cout << "Enter pin: ";
cin >> pin;
if (pin == user[userID].getpin())
{
transaction(&user, userID);
}
else { cout << "Pin invalid." << endl; }
}
// Quick withdrawal
else if (input == 3)
{
cout << "Enter Customer ID: ";
cin >> userID;
cout << "Enter pin: ";
cin >> pin;
cout << "Enter the account you wish to make a withdrawal from: ";
cin >> account;
if (pin == user[userID].getpin())
{
if (account <= user[userID].accounts)
{
if (user[userID].bank[account].getbalance() - 10 <= -user[userID].bank[account].getoverdraft())
{
user[userID].bank[account].withdraw(10);
}
else { cout << "Insignificunt funds. Overdraft limit (" << char(156) << user[userID].bank[account].getoverdraft() << ")" << endl; }
}
else { cout << "That account does not exist." << endl; }
}
else { cout << "Pin invalid." << endl; }
}
// Exit
else if (input == 4)
{
menu = false;
}
}
return 0;
}
void transaction(vector<Customer *> &user, int userID){...}
ERROR DESCRIPTION: initial value of reference to non-const must be an lvalue.
you are passing wrong arguments to the transaction() first parameter takes reference to vector of customer pointer
std::vector<Customer*> &user
but you are passing an address of vector of customer
vector<Customer> user(3);
transaction(&user, userID);
you should change vector<Customer> to vector<Customer*> user(3).
or
change void transaction(vector<Customer *> &user, int userID);
to void transaction(vector<Customer> &user, int userID);
the same goes for other functions if you are doing the same thing.
about the error, are you sure this is the problem?