Error LNK2019 - Unsure how to fix (1 unresolved external) [closed] - c++

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am nearly done with this program, however I am getting an error that I am unsure how to deal with. I have looked for other solutions but none have seemed to help
The error I'm getting is:
error LNK2019: unresolved external symbol "void __cdecl deposit(class customer * const,int)" (?deposit##YAXQAVcustomer##H#Z) referenced in function _main
C:\Users\D\Desktop\Programs\Project 3\DProject3\Debug\DProject3.exe : fatal error LNK1120: 1 unresolved externals
It seems to have something to do with the deposit function, however I have tried fooling around with it to no avail, if anyone could help it would be much appreciated, I have 3 files for this project
Header
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
struct Date
{
int Day;
int Month;
int Year;
};
struct Name
{
std::string first_name;
std::string middle_name;
std::string last_name;
};
class customer
{
private:
Date birth_date;
Name name;
float balance_saving, balance_checking, initial_saving, initial_checking, amount, account_type;
public:
customer(); //Customer constructor
customer(Date birth_date, Name name, float initial_saving, float initial_checking); // Customer constructor with info
void withdraw(float amount, int account_type);
void deposit(float amount, int account_type);
float check_balance_saving(); // Print out the savings balance on screen
float check_balance_checking(); // Print out the checking balance on screen
void setDate(int Day, int Month, int Year);
Date getDate();
void setName(std::string first_name, std::string middle_name, std::string last_name);
Name getName();
void setSaving(float initial_saving);
void setChecking(float initial_checking);
};
#endif
Implementation File
#include "customer.h"
//Contructor without info / Default info
customer::customer()
{
birth_date.Day = 01;
birth_date.Month = 01;
birth_date.Year = 1901;
name.first_name = "N/A";
name.middle_name = "N/A";
name.last_name = "N/A";
balance_saving = 0;
balance_checking = 0;
}
// Constructor with user input data
customer::customer(Date DMY, Name FML, float initSav, float initCheck)
{
birth_date = DMY;
name = FML;
initial_saving = initSav;
initial_checking = initCheck;
}
void customer::withdraw(float amtW, int accW)
{
amount = amtW;
account_type = accW;
}
void customer::deposit(float amtD, int accD)
{
amount = amtD;
account_type = accD;
}
float customer::check_balance_saving()
{
return initial_checking;
}
float customer::check_balance_checking()
{
return initial_checking;
}
void customer::setDate(int Day, int Month, int Year)
{
birth_date.Day = Day;
birth_date.Month = Month;
birth_date.Year = Year;
}
Date customer::getDate()
{
return birth_date;
}
void customer::setName(std::string First, std::string Middle, std::string Last)
{
name.first_name = First;
name.middle_name = Middle;
name.last_name = Last;
}
Name customer::getName()
{
return name;
}
void customer::setSaving(float setSav)
{
initial_saving = setSav;
}
void customer::setChecking(float setCheck)
{
initial_checking = setCheck;
}
Main
#include "customer.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//Function prototypes
void withdraw (customer[], int);
void deposit (customer[], int);
void checkBal (customer[], int);
void newAccountFile (customer[], int);
int main ()
{
customer custInfo[49]; //Creates the customer array for info to be stored in
int customerAmt = 0; //Keeps track of the amount of customers in the array
ifstream customerfile ("account.dat"); //Open existing customer file
while (customerAmt < 2) //Loop to read the data from the existing file
{
int D, M, Y; // Birth day, month, year
string FN, MN, LN; //First name, middle name, last name
float Save, Check; //Savings & Checking initial amount
customerfile >> D;
customerfile >> M;
customerfile >> Y;
customerfile >> FN;
customerfile >> MN;
customerfile >> LN;
customerfile >> Save;
customerfile >> Check;
custInfo[customerAmt].setDate(D, M, Y);
custInfo[customerAmt].setName(FN, MN, LN);
custInfo[customerAmt].setSaving(Save);
custInfo[customerAmt].setChecking(Check);
customerAmt++;
}
customerfile.close(); //Close customer file and end file reading
//Variables for the User menu
int accountNumber, option, D1, M1, Y1;
string FN1, MN1, LN1;
float Save1, Check1;
//Loop used for User menu
do
{
cout << "1. withdraw" << endl;
cout << endl;
cout << "2. Deposit" << endl;
cout << endl;
cout << "3. Check Balances" << endl;
cout << endl;
cout << "4. Create a New Account" << endl;
cout << endl;
cout << "0. Exit" << endl;
cin >> option;
if (option == 1) // Uses withdraw function
{
cout << "Enter the account number: ";
cin >> accountNumber;
withdraw(custInfo, accountNumber);
}
else if (option == 2) // Uses deposit function
{
cout << "Enter the account number: " << endl;
cin >> accountNumber;
deposit(custInfo, accountNumber);
}
else if (option == 3) // Uses check balance function
{
cout << "Enter the account number: " << endl;
cin >> accountNumber;
checkBal(custInfo, accountNumber);
}
else if (option == 4) //Option to handle creating a new account
{
cout << "Enter the new account information, starting with birth info: " << endl;
cin >> D1 >> M1 >> Y1;
custInfo[customerAmt].setDate(D1, M1, Y1);
cout << endl;
cout << "Enter the new customer's full name: " << endl;
cin >> FN1 >> MN1 >> LN1;
custInfo[customerAmt].setName(FN1, MN1, LN1);
cout << endl;
cout << "Enter the inital savings balance: " << endl;
cin >> Save1;
custInfo[customerAmt].setSaving(Save1);
cout << endl;
cout << "Enter the initial checking balance: " << endl;
cin >> Check1;
custInfo[customerAmt].setChecking(Check1);
customerAmt++;
}
}while (option != 0);
newAccountFile(custInfo, customerAmt); // Creates new updated account info file
system ("pause");
return 0;
}
void withdraw(customer custInfo[], int number)
{
float withdrawAmount;
int optionWit;
float tempSave;
float tempCheck;
cout << "1. Withdraw from savings" << endl;
cout << endl;
cout << "2. Withdraw from checking" << endl;
cin >> optionWit;
cout << "Enter the amount to be withdrawn: ";
cin >> withdrawAmount;
if (optionWit == 1)
{
tempSave = custInfo[number].check_balance_saving() - withdrawAmount;
custInfo[number].setSaving(tempSave);
}
else if (optionWit == 2)
{
tempCheck = custInfo[number].check_balance_checking() - withdrawAmount;
custInfo[number].setChecking(tempCheck);
}
}
void desposit(customer custInfo[], int number)
{
float depositAmount;
int optionDep;
float tempSave;
float tempCheck;
cout << "1. Deposit to savings" << endl;
cout << endl;
cout << "2. Deposit to checking" << endl;
cin >> optionDep;
cout << "Enter the amount to be deposited: ";
cin >> depositAmount;
if (optionDep == 1)
{
tempSave = custInfo[number].check_balance_saving() + depositAmount;
custInfo[number].setSaving(tempSave);
}
else if (optionDep == 2)
{
tempCheck = custInfo[number].check_balance_checking() + depositAmount;
custInfo[number].setChecking(tempCheck);
}
}
void checkBal(customer custInfo[], int number)
{
int optionBal;
cout << "1. Check savings balance" << endl;
cout << endl;
cout << "2. Check checking balance" << endl;
cin >> optionBal;
if (optionBal == 1)
{
cout << "Current savings balance is: " << custInfo[number].check_balance_saving() << endl;
}
else if (optionBal == 2)
{
cout << "Current checking balance is: " << custInfo[number].check_balance_checking() << endl;
}
}
void newAccountFile(customer custInfo[], int size)
{
int i;
Date tempDate;
Name tempName;
ofstream newAccountStorage;
newAccountStorage.open ("updated_account.dat"); //Opens new file for updated account info
for (i = 0; i < size; i++);
{
tempDate = custInfo[i].getDate();
tempName = custInfo[i].getName();
newAccountStorage << tempDate.Day << endl;
newAccountStorage << tempDate.Month << endl;
newAccountStorage << tempDate.Year << endl;
newAccountStorage << tempName.first_name << endl;
newAccountStorage << tempName.middle_name << endl;
newAccountStorage << tempName.last_name << endl;
newAccountStorage << custInfo[i].check_balance_saving() << endl;
newAccountStorage << custInfo[i].check_balance_checking() << endl;
}
newAccountStorage.close(); //Closes the file after writing new information
}

You have misprint in your function definition
void desposit(customer custInfo[], int number)
^^^^

Related

Why is the balance not getting updated after Deposit or Withdraw?

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.

Program Ends unexpectedly after the function is called

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.

How to call integer from one method to another method in c++

I am trying to make a program that can take student's information(name, Fee) from the user, then ask the user to enter raise percentage value and then calculate the new fee of all students.
For this, I need the Fee details entered in setValues() method to be called in calculateFee() method. But I can't figure it out that how can I do this.
Kindly help me with this.
class student
{
public:
int fee;
char name[20];
//member function to read details
void setValues()
{
cout << "Enter Name:";
cin >> name;
cout << "Enter Fee:";
cin >> fee;
}
// member function to calculate the new fee
void calculateFee()
{
float rFee;
cout << "Enter the Percent value for Fee Increment";
cin >> rFee;
if (rFee <= 0)
{
cout << "Please enter value greater than 0" << endl;
}
else
{
float temp;
temp = fee * rFee / 100;
rFee = fee + temp;
cout << "New Fee is" << endl;
cout << "Name" << name;
cout << "Fee" << rFee << endl;
}
}
};
int main()
{
student std[3]; //array of students object
int n, i;
n = 3;
for (i = 0; i<n; i++){
cout << "Enter details of student " << i + 1 << ":\n";
std[i].setValues();
}
for (i = 0; i < n; i++){
cout << "\nDetails of student " << i + 1 << ":\n";
std[3].calculateFee();
}
return 0;
}

C++ Virtual Void

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];
}
}

Can't figure out error when adding to an array

I am having a hard time figuring this out. Everything else in my project is working so far, but when I try to add to my array of objects, it crashes. I get an error of cannot read string. This is the message, I have tried so many things. Here is the error, and it crashes after the input on the SetNewAccountInfo() function. I am new to programming, and hope this code is formatted on here right. This is a school project so I'm not asking for the answer, maybe just explain why I am unable to add to my array in laymen terms. Pointers and such are confusing so far.
static _Elem *__CLRCALL_OR_CDECL copy(_Elem *_First1, const _Elem *_First2, size_t _Count)
{ // copy [_First2, _First2 + _Count) to [_First1, ...)
**return (_Count == 0 ? _First1** : (_Elem *)_CSTD memcpy(_First1, _First2, _Count));
}
// TestClassC.cpp : main project file.
#include "stdafx.h"
#include "BankClassType.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>
using namespace std;
void MainMenu(BankClassType bo[],int num, int cAcct);
void AcctOptionsMenu(BankClassType bo[], int num, int cAcct);
int SetNewAccountInfo();
void PrintAccountInfo();
void AccountDeposit();
void AccountWithdrawal();
int main()
{
const int num = 4;
BankClassType *bo = new BankClassType[num];
BankClassType b;
BankClassType bo0("12345", 'P', "Gates", "Bill", 175253.99, 6875250.23);
BankClassType bo1("12346", 'R', "Jones", "Tommy", 845.21, 2700.00);
BankClassType bo2("12347", 'P', "Asimov", "Isaac", 300.67, 14750.29);
bo[0] = bo0;
bo[1] = bo1;
bo[2] = bo2;
int cAcct = 3;
b.MainMenu(bo, num, cAcct);
_getch();
return 0;
}
//BankClassType.h File
#pragma once
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>
#ifndef BankClassType_h
#define BankClassType_h
using namespace std;
class BankClassType
{
private:
string AccountNumber;
string FirstName;
string LastName;
double CheckingBalance;
double SavingsBalance;
char AccountType;
public:
BankClassType()
{
FirstName = "";
LastName = "";
AccountNumber = "";
AccountType = 'p';
CheckingBalance = 0;
SavingsBalance = 0;
}
BankClassType(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal);
//~BankClassType();
void SetNewAccountInfo(BankClassType bo[], int num, int cAcct);
void PrintAccountInfo();
void AccountDeposit(BankClassType bo[], int number, int num, int cAcct);
void AccountWithdrawal(BankClassType bo[], int number, int num, int cAcct);
void AcctOptionsMenu(BankClassType bo[], int num, int cAcct);
void MainMenu(BankClassType bo[], int num, int cAcct);
//void ExistingAccounts(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal);
string getAccountNumber()
{
return AccountNumber;
}
char getAccountType()
{
return AccountType;
}
double getCheckingBalance(double amount)
{
CheckingBalance = CheckingBalance + amount;
return CheckingBalance;
}
double getSavingsBalance(double amount)
{
SavingsBalance = SavingsBalance + amount;
return SavingsBalance;
}
double getWithdrawalCheckingBalance(double amount)
{
CheckingBalance = CheckingBalance - amount;
return CheckingBalance;
}
double getWithdrawalSavingsBalance(double amount)
{
SavingsBalance = SavingsBalance - amount;
return SavingsBalance;
}
string getFirstName()
{
return FirstName;
}
string getLastName()
{
return LastName;
}
};
#endif
//BankClassImp.cpp
#include "stdafx.h"
#include "BankClassType.h"
#include <iostream>
#include <conio.h>
#include <string>
#include <ostream>
using namespace std;
BankClassType::BankClassType(string acctNumber, char acctType, string lname, string fname, double cBal, double sBal)
{
AccountNumber = acctNumber;
AccountType = acctType;
LastName = lname;
FirstName = fname;
CheckingBalance = cBal;
SavingsBalance = sBal;
/*cout << "\nName: " << LastName << ", " << FirstName;
cout << "\nAccountNumber: " << AccountType << AccountNumber;
cout << "\nChecking Balance: " << CheckingBalance;
cout << "\nSavings Balance: " << SavingsBalance<<endl;
*/
}
/*BankClassType::~BankClassType()
{
delete[] FirstName;
delete[] LastName;
delete[] AccountNumber;
delete[] AccountType;
}*/
void BankClassType::SetNewAccountInfo(BankClassType bo[], int num, int cAcct)
{
//BankClassType * bt = new BankClassType;
BankClassType c;
string fName;
string acctNum;
string lName;
char accType;
double cBal, sBal;
if (cAcct >= 5)
{
"Sorry the accounts are currently full, press any key to return to main menu: ";
c.MainMenu(bo, num, cAcct);
_getch();
}
else
cout << "\nEnter the 5 digit account number: "; cin >> acctNum;
cout << "\nEnter the account type (p) or (r): "; cin >> accType;
cout << "\nEnter the first name: "; cin >> fName;
cout << "\nEnter the last name: "; cin >> lName;
cout << "\nEnter the checking balance: "; cin >> cBal;
cout << "\nEnter the savings balance: "; cin >> sBal;
BankClassType bo3(acctNum, accType, lName, fName, cBal, sBal);
bo[cAcct + 1] = bo3;
}
void BankClassType::PrintAccountInfo()
{
cout << "Name: " << LastName << ", " << FirstName;
cout << "\nAccountNumber: " << AccountType << AccountNumber;
cout << "\nChecking Balance: " << CheckingBalance;
cout << "\nSavings Balance: " << SavingsBalance << endl << endl;
}
void BankClassType::AccountDeposit(BankClassType bo[], int number, int num, int cAcct)
{
int choice;
BankClassType b;
cout << "\nEnter 1 for checking or 2 for savings: ";
cin >> choice;
if (choice == 1)
{
double amount;
cout << "Enter amount to deposit into checking: " << endl;
cin >> amount;
bo[number].getCheckingBalance(amount);
cout << "You added $" << amount << " to checking account number " << bo[number].getAccountNumber();
_getch();
b.MainMenu(bo, num, cAcct);
}
else if (choice == 2)
{
double amount;
cout << "Enter amount to deposit into savings: " << endl;
cin >> amount;
bo[number].getSavingsBalance(amount);
cout << "You added $" << amount << " to savings number" << bo[number].getAccountNumber();
_getch();
b.MainMenu(bo, num, cAcct);
}
else
{
cout << "Incorrect Input, try again: ";
_getch();
AccountDeposit(bo, number, num, cAcct);
}
}
void BankClassType::AccountWithdrawal(BankClassType bo[], int number, int num, int cAcct)
{
int choice;
BankClassType b;
cout << "\nEnter 1 for checking or 2 for savings: ";
cin >> choice;
if (choice == 1)
{
double amount;
cout << "Enter amount to withdrawal from checking: " << endl;
cin >> amount;
bo[number].getWithdrawalCheckingBalance(amount);
cout << "You withdrew $" << amount << " from checking account number " << bo[number].getAccountNumber();
cout << "\nPress any key to return to main menu: ";
_getch();
b.MainMenu(bo, num, cAcct);
}
else if (choice == 2)
{
double amount;
cout << "Enter amount withdrawal from savings: " << endl;
cin >> amount;
bo[number].getWithdrawalSavingsBalance(amount);
cout << "You withdrew $" << amount << " from savings account number " << bo[number].getAccountNumber();
_getch();
b.MainMenu(bo, num, cAcct);
}
else
{
cout << "Incorrect Input, try again";
_getch();
AccountDeposit(bo, number, num, cAcct);
}
}
void BankClassType::MainMenu(BankClassType bo[], int num, int cAcct)
{
cout << "\nWelcome, here are the current accounts on file:\n" << endl;
for (int i = 0; i < cAcct; i++)
{
cout << i + 1 << "--";
bo[i].PrintAccountInfo();
}
int choice;
BankClassType b;
cout << "Enter number (1-5) of account you wish to make changes\n Enter 6 to add an account\nEnter 7 to exit\n --->";
cin >> choice;
if (choice < 6)
b.AcctOptionsMenu(bo, num, cAcct);
else if (choice == 6)
{
bo[cAcct + 1].SetNewAccountInfo(bo, num, cAcct);
cout << "You have successfully added an account!" << endl;
_getch();
cAcct++;
b.MainMenu(bo, num, cAcct);
}
else if (choice == 7)
{
cout << "You have exited, press any key to continue: ";
_getch();
system("cls");
}
else if(choice > 7)
{
cout << "Not a valid input, press a key to try again: ";
_getch();
system("cls");
MainMenu(bo, num, cAcct);
}
}
void BankClassType::AcctOptionsMenu(BankClassType bo[], int num, int cAcct)
{
char choice;
int check = 0;
int count = 0;
int number;
BankClassType b;
while (check == 0) {
cout << "\t\t\n\n" << "Main Menu";
cout << "\t\n\n" << "Select by letter:";
cout << "\t\n" << "(d) - Deposits";
cout << "\t\n" << "(w) - Withdrawals";
cout << "\t\n" << "(s) - Show Account Information.";
cout << "\t\n" << "(q) - Quit Program.\n\n";
cout << "\t" << "Choice: ";
choice = _getche();
switch (choice) {
case 'd':
case 'D':
system("cls");
cout << "\nChoose Account:\n ";
for (int i = 0; i < cAcct; i++) cout << i +1 << "---" << bo[i].getAccountNumber()<< "\n\n" ;
cin >> number;
bo[number].AccountDeposit(bo,number, num, cAcct);
system("cls");
break;
case 'w':
case 'W':
system("cls");
cout << "\nChoose Account:\n ";
for (int i = 0; i < cAcct; i++) cout << i + 1<< "---" << bo[i].getAccountNumber() << "\n\n";
cin >> number;
bo[number].AccountWithdrawal(bo, number, num, cAcct);
system("cls");
break;
case 's':
case 'S':
system("cls");
cout << "\nChoose Account:\n ";
for (int i = 0; i < cAcct; i++) cout << i + 1 << "---" << bo[i].getAccountNumber() << "\n\n";
cin >> number;
bo[number].PrintAccountInfo();
_getche();
system("cls");
break;
case 'q':
case 'Q':
check = 1;
break;
default:
cout << "\nInvalid selection. Press a key to return to main menu.";
_getche();
}
if (check == 1)
break;
}
}
It looks like the array allocated in main is too short. You allocate a block of 4 elements, pass '3' as the cAcct, then bo[cAcct + 1] = bo3. The index will be 4 which is one pass the end of the array. Results are undefined and a crash is likely