Hi, this is my first time posting and I would like thank everyone in advance. I'm doing a homework assignment that requires me to design a teller application which simulates a bank. I'm currently getting a segment fault, after I call one of my getBranch(). I've tried to do this the other way by returning a Branch* and the same error was returned. Here is my main():
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <vector>
#include "Branch.h"
#include <sstream>
//#include <map.h>
//void getBranch(Branch *currBranch);
using namespace std;
//Branch* getBranch(string branchName);
void getBranch(string branchName, Branch *currBranch);
void writeBranch(Branch *branch);
/////////////////////////////////main//////////////////////////
int main(){
string centralBankName="BA Central";
double centralNetVal=0.0;
vector <int> *interestingAccounts;
cout << "Welcome to BA Bank! Enter 'Q' to quit anytime "<<endl;
//make Central Ba bank structure from interesting accounts and update centralNetVal
//ifstream inFile("Branch.txt");
//getline(inFile
//inFile.close();
//approve or disapprove interest accounts;
//for each interestingAccounts approve or disapprove if approve write to Account.txt
int checkBranch=1;
int checkAccount=0;
int checkMethod=0;
while (checkBranch==1) {
cout << "Please choose a branch to login to"<<endl;
string branchName;
Branch *currBranch;
getline(cin,branchName);
//look in Branch.txt for branchName
string currentLine;
if (branchName=="Q") {
checkBranch=0;
checkAccount=0;
checkMethod=0;
break;
}
getBranch(branchName,currBranch);
checkAccount=1;
cout << "got here"<<endl;
cout <<currBranch->bName<<endl;
while (checkAccount==1) {
//print list of accounts or choose different account for -1 or quit for -2
cout << "Please choose an option:\n1.Open an account in current branch\n2.modify an account in current branch\n3.incriment time for all accounts\n4.switch branch"<<endl;
//for each account in currentBranch print
int accSelector;
string input;
cin>>input;
if (input=="Q") {
checkBranch=0;
checkAccount=0;
checkMethod=0;
break;
}
accSelector=atoi(input.c_str());
switch (accSelector) {
case 1://open account
break;
case 2://modify account
break;
case 3://incriment time
break;
case 4://switch branch
checkMethod=0;
checkAccount=0;
break;
default:
break;
}
int accType;
//get ofject at index accSelector and accType in currentBranch.accType[accSelector] set checkMethod=0
while (checkMethod==1) {
//switch for 1-4 by accType;
//-1 for change accounts
//-2 for change branch
//-3 to quit
}
}//end of checkAccount
//write back
writeBranch(currBranch);
}//end of checkBranch
return 0;
}
void writeBranch(Branch *branch){
cout << "inside of write Branch"<<endl;
string name,val;
//name=branch.bName;
ofstream outFile("Branch.txt",ios::app);
//outFile<<*branch <<endl;
outFile.close();
}
void getBranch(string branchName,Branch *currBranch){
//Branch *currBranch;
ifstream inFile("Branch.txt");
ofstream outFile("tmp.txt");//,ios::app);
int found=0;
if (inFile.is_open()&&found==0) {
size_t foundString;
string currentLine="init";
while (!inFile.eof()&¤tLine!="\0") {
getline(inFile,currentLine);
foundString=currentLine.find(branchName);
cout << "looking for "<<branchName<<" in Branch.txt in: "<<currentLine<<endl;
if (foundString!=string::npos) {
cout << "found it in Branch.txt"<<endl;
found=1;
//checkAccount=1;
string word;
string currName;
vector<string> *objVar;
double curValue;
stringstream stream(currentLine);
int i=0;
while( getline(stream, word, ',') ){
cout << word << "\n";
//objVar->push_back(word.c_str());
currBranch =new Branch(&branchName);
switch (i) {
case 0:
currBranch->bName=word;
cout << "setting Branch Name: "<<currBranch->bName<<endl;
break;
case 1:
curValue=atof(word.c_str());
currBranch->netValue=curValue;
cout << "setting netVal: "<<currBranch->netValue<<endl;
break;
default:
break;
}
i++;
}
//make branch object;
}
else {
//write out to tmp.txt
outFile<<currentLine<<endl;
outFile.flush();
}
//if currentLine contains branchName set found=1 and make object
}
if (found==0) {
//make object out of branchName
currBranch->bName=branchName;
currBranch->netValue=0.0;
cout <<currBranch->bName<<" ::Newest branch in BA Bank with net value: $"<<currBranch->netValue<<endl;
found=1;
}
}
inFile.close();
outFile.close();
//write tmp back to Branch.txt
inFile.open("tmp.txt");
outFile.open("Branch.txt");
//ifstream inFile2("Branch.txt");
//ofstream outFile2("tmp.txt");
if (inFile.is_open()){
string line;
while (!inFile.eof()){
getline(inFile,line);
outFile<<line<<endl;
outFile.flush();
}
}
inFile.close();
outFile.close();
//return currBranch;
}
This is my Branch class
#include "Branch.h"
#include <string>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <vector>
//#include <map.h>
using namespace std;
//string bName;
//double netValue;
//double action;
//vector<double> accounts;
//vector <int> typeAccout;
Branch::Branch(string *name){
this->bName= *name;
this->netValue=0;
this->action=0;
vector <int> *accounts;
vector <int> *accType;
}
Branch::Branch(string *name, double val,double action)//if action is -1
{
this->bName=*name;
this->netValue=val;
this->action=action;
vector <int> *accounts;
//open account.txt and match using bName and update type account:: also remove line from file once added to vector
ifstream inFile("Account.txt");
inFile.close();
}
Branch::~Branch(){}
void Branch::applyInterest(){
//for each account in accounts look up accType and call apply interest. use switch statment
}
void Branch::addAccount(string name, int ssn, int cred, int type, double val){
//construct Client object and write to Client.txt
//add account to current array and also it's type to accType
}
void Branch::withdraw(double value, double index){
//get accType at index and ceate account use switch statement
}
void Branch::update(){
//for each account get accType and cast it, call update() for that account
}
void Branch::close(){
//write back to Account.txt
ofstream outFile("Account.txt",ios::app);
outFile.close();
//write back to Bank.txt
outFile.open("Bank.txt",ios::app);
outFile.close();
}
I've ran into similar problems before due to shallow copy, but shouldn't using a pointer solve the problem?