When I run my Main Menu, I am able to add data that is needed for the map, but the problem happens when I go back to the menu and find that when I try to print the data nothing appears. In case you are wondering, there are no errors, it will just not print the data stored within the map when you run print contacts from the menu. Below is the code.
Main
#include <iostream>
using namespace std;
#include "AddContact.h"
//PASSWORD IS "Delta" use upper case D and the rest is lower case
int main() {
AddContact con;
con.Menu();
return 0;
}
AddContact.h
#include <iostream>
#include<cstdlib>
#include<map>
using namespace std;
#ifndef ADDCONTACT_H_
#define ADDCONTACT_H_
class AddContact {
public:
std::map<int, AddContact> people;
private:
string ContactName;
long long ContactPhone;
string Address;
string Email;
string Skype;
public:
AddContact();
AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype);
void ADD();
void print()const;
void view();
void Menu();
void Password();
};
#endif /* ADDCONTACT_H_ */
AddContact.cpp
#include "AddContact.h"
AddContact::AddContact(): ContactName(""), ContactPhone(0), Address(""), Email(""), Skype(""){
}
AddContact::AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype) {
this->ContactName = ContactName;
this->ContactPhone = ContactPhone;
this->Address = Address;
this->Email = Email;
this->Skype = Skype;
}
void AddContact::Password(){
cout << "Please Enter Your Password. " << endl;
string pass;
cin >> pass;
if (pass != "Delta") {
Password();
}
}
void AddContact::Menu(){
cout << "!!MyCircle Contact!!" << endl;
cout << endl;
Password();
int selection;
do {
cout << "***************************" << endl;
cout << "1. Add New Contact " << endl;
cout << "2. Display Contact " << endl;
cout << "3. Quit " << endl;
cout << "***************************" << endl;
cin>> selection;
switch(selection){
case 1:
ADD();
break;
view();
break;
}
} while (selection != 0);
}
void AddContact::ADD() {
for (int i = 0; i < 1; i++) {
string temp1;
cout << "Please Enter Contact Name: " << endl;
cin >> temp1;
long long temp2;
cout << "Please Enter Contact Phone Number: " << endl;
cin >> temp2;
int choice;
cout<< "Press 1 if you would like to add additional contact info or press 0 to return to main menu "<< endl;
cin >> choice;
if (choice == 1) {
string temp3 = "";
cout << "Please enter contact Address: " << endl;
cin>> temp3;
getline(cin, temp3);
string temp4= "";
cout << "Please enter contact Email: " << endl;
cin >> temp4;
string temp5 = "";
cout << "Please enter contact Skype: " << endl;
cin >> temp5;
people[i] = AddContact(temp1, temp2, temp3, temp4, temp5);
} else {
}
}
}
void AddContact::print()const{
cout<<ContactName<<" "<<ContactPhone<<" "<<Address<<" "<<Email<<" "<<Skype<<endl;
}
void AddContact::view(){
for(map<int, AddContact>::iterator it = people.begin(); it != people.end();it++){
it->second.print();
}
}
It looks like you're mising a default in your switch statement:
switch(selection){
case 1:
ADD();
break;
view();
break;
}
is mean to be
switch(selection){
case 1:
ADD();
break;
default:
view();
break;
}
Related
How can i save arrays and print them in c++,here's what i mean, i am creating a student database, and in one class i am giving the students name, surname, id, age, etc. So how can i save them and print those information that i have given. Here is what i have tried so far, and it hasn't been working any body have any ideas?
#ifndef Student_h
#define Student_h
class Student
{
public:
virtual void Print() = 0;
};
#endif
#include "Student.h"
#ifndef University_h
#define University_h
class University :public Student
{
private:
string University_Name;
public:
University(string Uni);
void Set_University_Name(string University);
string Get_University_Name();
void Print();
};
#include<iostream>
#include<string>
#include"University.h"
#include"Student.h"
using namespace std;
University::University(string Uni)
{
Set_University_Name(Uni);
}
void University::Set_University_Name(string U_Name)
{
University_Name = U_Name;
}
string University::Get_University_Name()
{
return University_Name;
}
void University::Print()
{
cout << "\n South East European University" << University_Name;
}
class Data_Base
{
private:
string Student_Name;
string Student_Surname;
string Course;
int Student_Age;
int Student_ID;
public:
void Set_Name(string Name);
string Get_Name();
void Set_Surname(string Surname);
string Get_Surname();
void Set_Course(string _Course);
string Get_Course();
void Set_Age(int Age);
int Get_Age();
void Set_ID(int ID);
int Get_ID();
void Print();
void Print_Data();
double Average();
};
void Data_Base::Print(){
int n = 0;
cout << "\n Enter how many students you wish to add: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "\n Student:" << i + 1 << endl;
cout << "\n Enter Student Name:";
cin >> Student_Name;
cout << "\n Enter Student Surname:";
cin >> Student_Surname;
cout << "\n What Course is the Student Studying:";
cin >> Course;
cout << "\n Enter Student Age:";
cin >> Student_Age;
cout << "\n Enter Student ID:";
cin >> Student_ID;
}
}
void Data_Base::Print_Data(){
cout << "Student Data Base: " << endl;{
cout << "\n Student Name :" << Student_Name;
cout << "\n Course :" << Course;
cout << "\n ID : " << Student_ID;
}
}
#include<iostream>
#include<string>
#include"Student.h"
#include"University.h"
#include"DataBase.h"
using namespace std;
int main()
{
int Menu;
Data_Base add, view, average;
Data_Base* AddStudent = &add;
Data_Base* ViewStudents = &view;
Data_Base* AverageGrade = &average;
menu:
cout << "\n===========================" << endl;
cout << "\n Press - 1 - To Add New Students";
cout << "\n Press - 2 - To View Added Students";
cout << "\n Press - 3 - To To Find the Average Grade";
cout << "\n Press - 0 - To Quit Program";
cout << "\n ----Please Select An Option----";
cin >> Menu;
switch (Menu)
{
case 1:
AddStudent->Print();
break;
case 2:
ViewStudents->Print_Data();
break;
case 3:
AverageGrade->Average();
break;
case 0:
return 0;
cout << "\n Thank You for Using our Program!";
break;
default:
cout << "\n Wrong Button!Try Again";
goto menu;
break;
}
return 0;
}
int main()
{
int Menu;
Data_Base db;
menu:
cout << "\n===========================" << endl;
cout << "\n Press - 1 - To Add New Students";
cout << "\n Press - 2 - To View Added Students";
cout << "\n Press - 3 - To To Find the Average Grade";
cout << "\n Press - 0 - To Quit Program";
cout << "\n ----Please Select An Option----";
cin >> Menu;
switch (Menu)
{
case 1:
db.Print();
break;
case 2:
db.Print_Data();
break;
case 3:
db.Average();
break;
case 0:
return 0;
cout << "\n Thank You for Using our Program!";
break;
default:
cout << "\n Wrong Button!Try Again";
goto menu;
break;
}
return 0;
}
This should let you see one student after you added it. As mentioned in the comments, you will need a list in order to store multiple students in the same database. Also it was wrong to use three diferent Data_base objects hoping for them to have the same private members becouse they have different memory unless they point to the same object.
Ok. I'm trying to finish writing to a file and then reading from a file but I keep getting an error when I start the void readData(void).The error is "no matching token found." What could I do to make this work/ make it easier?
I'm not understanding how to use the void function, I think. I'm sure there are more issues because when I remove void readData(void) the error moves to void writeData(void).
//Specification: Append and display records in a address database
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
ifstream infile;
string cont = "Y";
string name;
string street;
string city;
string state;
string zip;
string choice;
const char FileName[] = "TestAddress.txt";
ofstream outfile("TestAddress.txt", ios::app);
int main() {
menu();
writeData();
readData();
return 0;
} //end main
void menu(void) {
cout << "Would you like to add to the records, display them, or exit?\n";
cout << "Enter a to add, d to display, or e to exit";
cin >> choice;
}
//allow user to choose to append records, display records or exit the program
//end menu
void writeData(void) {
while (choice != "a") {
cout << "Enter the address name: ";
getline(cin, name);
cout << "Enter the house number and street: ";
cin >> street;
cout << "Enter the city: ";
cin >> city;
cout << "Enter the state: ";
cin >> state;
cout << "Enter the zip code: ";
cin >> zip;
outfile << name << "#" << street << city << "#" << state << "#" << zip << endl;
cin.ignore();
cout << "Do you want add another addressor exit? (a/e)";
getline(cin, choice);
}
outfile.close();
//Write the Address Info to a file
//loop while user still has data to write to file
//eg outStream<<name<<”#”; //where # is the delimiter
}
//end write data
void readData(void) {
//read data from a file
//use the split function to break a
//deliminated line of text into fields
infile.open("TestAddress.txt");
ifstream inMyStream("TestAddress.txt");
while (choice != "d") {
if (inMyStream.is_open()) {
//set character to use as a line between record displays
string recBreaks = "#";
recBreaks.assign(20, '#');
int fieldCount = 0; //keep track of the number of fields read
int recordCount = 1; //keep track of the number of records read
//read the first field
fieldCount = 1;
string fieldBuffer;
getline(inMyStream, fieldBuffer, '#');
while (!inMyStream.eof()) {
//display the field
switch (fieldCount) {
case 1:
cout << recBreaks << endl;
cout << "record # " << recordCount << endl;
cout << "Name...." << fieldBuffer << endl; break;
case 2:
cout << "Street.." << fieldBuffer << endl; break;
case 3:
cout << "City...." << fieldBuffer << endl; break;
case 4:
cout << "State..." << fieldBuffer << endl; break;
case 5:
cout << "Zip....." << fieldBuffer << endl;
fieldCount = 0;
recordCount++; break;
}
//read the next field
getline(inMyStream, fieldBuffer, '#');
fieldCount++;
}
cout << recBreaks << endl;
inMyStream.close();
}//end read data
}
This program is supposed to call a read and write function from a menu, ask for inputs, save those inputs to a file then output the data to the screen. The menu works but I can only write to the file and save the data and exit. I cannot read or output what is saved to the file. Any help with this will be greatly appreciated. I should have three working functions, main(), writeData(), and readData().
#include < iostream >
#include < fstream >
#include < string >
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
const char FileName[] = "TestAddress.txt";
string choice;
int main()
{
menu();
return 0;
}
void menu(void)
{
while (choice != "Q") {
cout << "What would you like to do?\nA. Read the existing data:\nB. Write new data to the list:\nQ.Quit\n";
cin >> choice;
if (choice == "A") {
readData();
}
else if (choice == "B") {
writeData();
}
else
break;
}
}
void writeData(void)
{
ofstream outFile("testAddress.txt");
string name;
string street;
string city;
string state;
int zip;
string cont = "y";
ifstream inFile;
while (cont != "q") {
cout << "Please enter the name:\n";
getline(cin, name);
cout << "Please enter the street:\n";
getline(cin, street);
cout << "Please enter the city:\n";
getline(cin, city);
cout << "Please enter the state:\n";
getline(cin, state);
cout << "Please enter the zip code:\n";
cin >> zip;
outFile << name << street << "#" << city << state << zip << endl;
cin.ignore();
cout << "Would you like to continue? Type q to quit:";
getline(cin, cont);
cin.ignore();
}
outFile.close();
inFile.open("testAddress.txt");
string fieldBuffer;
if (inFile.is_open()) {
while (inFile.eof()) {
getline(inFile, fieldBuffer, ',');
cout << fieldBuffer;
cout << endl;
}
}
}
//use # sign for delimiter
void readData(void)
{
ifstream inMyStream(FileName);
if (inMyStream.is_open()) {
string recBreaks = "";
recBreaks.assign(20, '*');
int fieldCount = 0;
int recordCount = 1;
fieldCount = 1;
string fieldBuffer;
getline(inMyStream, fieldBuffer, '#');
while (inMyStream.eof()) {
switch (fieldCount) {
case 1:
cout << recBreaks << endl;
cout << "Record #" << recordCount << endl;
cout << "Name...." << fieldBuffer << endl;
break;
case 2:
cout << "Street...." << fieldBuffer << endl;
break;
case 3:
cout << "City...." << fieldBuffer << endl;
break;
case 4:
cout << "State...." << endl;
break;
case 5:
cout << "Zip Code...." << endl;
fieldCount = 0;
recordCount++;
break;
getline(inMyStream, fieldBuffer, '#');
fieldCount++;
}
cout << recBreaks << endl;
inMyStream.close();
}
}
}
I have fixed a number of errors in this code, THANX to all who commented! Everything is working now except the readData function. When I input data to the file and run the readData function I get an infinite loop. Is there a break statement out of place?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
const char FileName[] = "TestAddress.txt";
string choice;
int main()
{
menu();
return 0;
}
void menu(void)
{
while (choice != "Q")
{
cout << "What would you like to do?\nA. Read the existing data:\nB. Write new data to the list:\nQ.Quit\n";
cin >> choice;
if (choice == "A")
{
readData();
}
else if (choice == "B")
{
writeData();
}
else
break;
}
}
void writeData(void)
{
ofstream outFile("TestAddress.txt");
string name;
string street;
string city;
string state;
int zip;
string cont = "y";
ifstream inFile;
while (cont != "q")
{
cout << "Please enter the name:\n";
cin.ignore();
getline(cin, name);
cout << "Please enter the street:\n";
getline(cin, street);
cout << "Please enter the city:\n";
getline(cin, city);
cout << "Please enter the state:\n";
getline(cin, state);
cout << "Please enter the zip code:\n";
cin >> zip;
outFile << name<< "#" << street<< "#"<< city << "#" << state << "#" << zip << "#" << endl;
cin.ignore();
cout << "Would you like to continue? Type q to quit:";
getline(cin, cont);
cin.ignore();
}
outFile.close();
inFile.open("TestAddress.txt");
string fieldBuffer;
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile, fieldBuffer, '#');
cout << fieldBuffer;
cout << endl;
}
}
}
//use # sign for delimiter
void readData(void)
{
ifstream inMyStream(FileName);
if (inMyStream.is_open())
{
string recBreaks = "";
recBreaks.assign(20, '*');
int fieldCount = 0;
int recordCount = 1;
fieldCount = 1;
string fieldBuffer;
getline(inMyStream, fieldBuffer, '#');
while (!inMyStream.eof())
{
switch (fieldCount)
{
case 1:
cout << recBreaks << endl;
cout << "Record #" << recordCount << endl;
cout << "Name...." << fieldBuffer << endl;
break;
case 2:
cout << "Street...." << fieldBuffer << endl;
break;
case 3:
cout << "City...." << fieldBuffer << endl;
break;
case 4:
cout << "State...." << fieldBuffer << endl;
break;
case 5:
cout << "Zip Code...." << fieldBuffer << endl;
fieldCount = 0;
recordCount++;
break;
getline(inMyStream, fieldBuffer, '#');
fieldCount++;
}
cout << recBreaks << endl;
inMyStream.close();
}
}
}
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 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