Switch statements and segmentation faults

I get segmentation when I run the program. Please help me figure out why. I've posted the two chunks of code that should include the problem. Thanks! Let me know if you need to see more coding.


void openAccount(string acno,string fName,string lName,
char midInit,double initBal,string acNums[],
string fNames[],string lNames[],char midInitials[],
double balances[],int& numAccs)
{
int pos=linearSearch(acno,acNums,numAccs);

for(int i= pos; i<MAX_CUSTOMERS; i++)
{
acNums[i]= acno;
fNames[i]= fName;
lNames[i]= lName;
midInitials[i]= midInit;
balances[i]= initBal;
}
numAccs++;
}

void closeAccount(string acno,string acNums[],string fNames[],
string lNames[],char midInitials[],
double balances[],int& numAccs)
{
int pos=linearSearch(acno,acNums,numAccs);

for(int i=pos; i<=MAX_CUSTOMERS ;i++)
{
acNums[i]= acNums[i+1];
fNames[i]= fNames[i+1];
lNames[i]= lNames[i+1];
midInitials[i]=midInitials[i+1];
balances[i]= balances[i+1];
}
numAccs--;
}

void deposit(string acno,double amount,string acNums[],
double balances[],int numAccs)
{
int pos=linearSearch(acno, acNums,numAccs);
balances[pos]= balances[pos]+ amount;
}

void withdraw(string acno,double amount,const string acNums[],
double balances[],int numAccs)
{
int pos=linearSearch(acno, acNums, numAccs);
if (amount>balances[pos])
balances[pos]= balances[pos]-amount-OVERDRAFT_FEE;
else
balances[pos]= balances[pos]-amount;
}

void inquiry(string acno,const string acNums[],const string fNames[],
const string lNames[],const char midInitials[],const double
balances[],int numAccs)
{
int pos=linearSearch(acno, acNums, numAccs);
cout<<"------------------------------"<<endl;
cout<<"customer: "<<fNames[pos]<<" "<<midInitials[pos]<<". "<<lNames[pos]<<endl;
cout<<"A/C #: "<<acNums[pos]<<endl;
cout<<"Balance: $"<<setprecision(2)<<fixed<<balances[pos]<<endl;
cout<<"------------------------------"<<endl;
}

void customersList(const string acNums[],const string fNames[],
const string lNames[],const double balances[],
int numAccs)
{
if(numAccs==0)
cout<<" *** THERE ARE NO ACTIVE ACCOUNTS ***"<<endl;
else
cout<<" Geaux Tiger Bank, Louisiana LTD"<<endl;
cout<<" Customers Listing"<<endl;
cout<<" 100 Les Miles Dr., Baton Rouge, Louisiana 70802"<<endl;
cout<<"==========================================================="<<endl;
cout<<setw(14)<<left<<"a/c # "<<setw(14)<<left<<"First Name "<<setw(14)<<left<<"Last Name "<<setw(14)<<"Balance"<<endl;
cout<<"-----------------------------------------------------------"<<endl;
for(int i=0; i<numAccs;i++)
{

cout<<setw(14)<<left<<acNums[i]<<setw(14)<<left<<fNames[i]<<setw(14)<<left<<lNames[i]<<setw(14)<<setprecision(2)<<fixed<<balances[i]<<endl;
}
cout<<"==========================================================="<<endl;
}

int main()
{ . . .

. . .
do
{
menu();
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;

int pos=linearSearch(acno, acNums, numAccs);
if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
if(pos>0)
cout<<acno<<" cannot be assigned to multiple customers."<<endl;
else
{
cout<<"Enter the first name>"<<endl;
cin>>fName;
cout<<"Enter the last name>"<<endl;
cin>>lName;
cout<<"Enter middle initial>"<<endl;
cin>>midInit;
cout<<"Enter initial deposit>"<<endl;
cin>>initBal;
}

if(initBal < 25.00)
cout<<"Insufficient funds to open account."<<endl;
else
openAccount(acno,fName,lName,midInit,initBal,acNums,fNames,lNames,
midInitials,balances,numAccs);
}
break;

case 2:
{
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;

if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
else if(linearSearch(acno,acNums,numAccs)<0)
cout<<"Account number does not exist"<<endl;
else
closeAccount(acno,acNums,fNames,lNames,
midInitials,balances,numAccs);
}
break;


case 3:
{
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;
if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
else if(numAccs<0)
cout<<"Account number does not exist"<<endl;
else
{
cout<<"Enter deposit amount>"<<endl;
cin>>amount;
}

if(amount<=0.00)
cout<<"Amount must be at least $0.01."<<endl;
else
deposit(acno,amount,acNums,balances,numAccs);
}
break;

case 4:
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;
if(acno.length()!=10)
cout<<"Account number must be ten digits.";
else if(numAccs<0)
cout<<"Account number does not exist"<<endl;
else
{
cout<<"Enter withdrawal amount>"<<endl;
cin>>amount;
}
if(amount<=0.00)
cout<<"Amount must be at least $0.01."<<endl;
else
withdraw(acno, amount, acNums,balances,numAccs);
break;

case 5:
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;
if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
else if(numAccs<0)
cout<<"Account number does not exist"<<endl;
else
inquiry(acno,acNums,fNames,lNames,midInitials,balances,numAccs);
break;

case 6:
customersList(acNums,fNames,lNames,balances,numAccs);
break;

case 0:
exit(1);
break;

default:
cout<<"Enter a valid choice."<<endl;
break;
}

}while(choice!=0);
Give me a break. Post something that compiles, and that is within tags.
Thanks for responding, but like I said, I only posted two chunks of the code. It does compile if you'd like the whole thing.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

const double OVERDRAFT_FEE = 35.00;
const int MAX_CUSTOMERS = 1000;

int linearSearch(string acno, const string acNums[], int numAccs)
{
int i;
for(i=0; i<numAccs; i++)
{
if(acno==acNums[i])
return i;
}
return -1;
}

void menu()
{
cout<<setw(22)<<"Geaux Tigers Bank ATM"<<endl;
cout<<setw(24)<<left;
cout<<"========================"<<endl;
cout<<"OPEN AN ACCOUNT......[1]"<<endl;
cout<<"CLOSE AN ACCOUNT.....[2]"<<endl;
cout<<"DEPOSIT..............[3]"<<endl;
cout<<"WITHDRAW.............[4]"<<endl;
cout<<"BALANCE INQUIRY......[5]"<<endl;
cout<<"CUSTOMERS LISTING....[6]"<<endl;
cout<<"EXIT.................[0]"<<endl;
}

void openAccount(string acno,string fName,string lName,
char midInit,double initBal,string acNums[],
string fNames[],string lNames[],char midInitials[],
double balances[],int& numAccs)
{
int pos=linearSearch(acno,acNums,numAccs);

for(int i= pos; i<MAX_CUSTOMERS; i++)
{
acNums[i]= acno;
fNames[i]= fName;
lNames[i]= lName;
midInitials[i]= midInit;
balances[i]= initBal;
}
numAccs++;
}

void closeAccount(string acno,string acNums[],string fNames[],
string lNames[],char midInitials[],
double balances[],int& numAccs)
{
int pos=linearSearch(acno,acNums,numAccs);

for(int i=pos; i<=MAX_CUSTOMERS ;i++)
{
acNums[i]= acNums[i+1];
fNames[i]= fNames[i+1];
lNames[i]= lNames[i+1];
midInitials[i]=midInitials[i+1];
balances[i]= balances[i+1];
}
numAccs--;
}

void deposit(string acno,double amount,string acNums[],
double balances[],int numAccs)
{
int pos=linearSearch(acno, acNums,numAccs);
balances[pos]= balances[pos]+ amount;
}

void withdraw(string acno,double amount,const string acNums[],
double balances[],int numAccs)
{
int pos=linearSearch(acno, acNums, numAccs);
if (amount>balances[pos])
balances[pos]= balances[pos]-amount-OVERDRAFT_FEE;
else
balances[pos]= balances[pos]-amount;
}

void inquiry(string acno,const string acNums[],const string fNames[],
const string lNames[],const char midInitials[],const double
balances[],int numAccs)
{
int pos=linearSearch(acno, acNums, numAccs);
cout<<"------------------------------"<<endl;
cout<<"customer: "<<fNames[pos]<<" "<<midInitials[pos]<<". "<<lNames[pos]<<endl;
cout<<"A/C #: "<<acNums[pos]<<endl;
cout<<"Balance: $"<<setprecision(2)<<fixed<<balances[pos]<<endl;
cout<<"------------------------------"<<endl;
}

void customersList(const string acNums[],const string fNames[],
const string lNames[],const double balances[],
int numAccs)
{
if(numAccs==0)
cout<<" *** THERE ARE NO ACTIVE ACCOUNTS ***"<<endl;
else
cout<<" Geaux Tiger Bank, Louisiana LTD"<<endl;
cout<<" Customers Listing"<<endl;
cout<<" 100 Les Miles Dr., Baton Rouge, Louisiana 70802"<<endl;
cout<<"==========================================================="<<endl;
cout<<setw(14)<<left<<"a/c # "<<setw(14)<<left<<"First Name "<<setw(14)<<left<<"Last Name "<<setw(14)<<"Balance"<<endl;
cout<<"-----------------------------------------------------------"<<endl;
for(int i=0; i<numAccs;i++)
{

cout<<setw(14)<<left<<acNums[i]<<setw(14)<<left<<fNames[i]<<setw(14)<<left<<lNames[i]<<setw(14)<<setprecision(2)<<fixed<<balances[i]<<endl;
}
cout<<"==========================================================="<<endl;
}

int main()
{
fstream inStrInfo;
fstream inStrBal;
fstream outStrInfo;
fstream outStrBal;
string fileNameInfo ="acinfo.dbf";
string fileNameBal = "acbal.dbf";
string acno,lName,fName;
double initBal,amount;
char midInit;
string acNums[MAX_CUSTOMERS];
string lNames[MAX_CUSTOMERS];
string fNames[MAX_CUSTOMERS];
char midInitials[MAX_CUSTOMERS];
double balances[MAX_CUSTOMERS];
int numAccs,choice;

inStrInfo.open(fileNameInfo.c_str(), ios::in);
inStrBal.open(fileNameBal.c_str(), ios::in);
numAccs=0;
if(!inStrInfo.fail())
{
inStrInfo>>acNums[numAccs];
while(!inStrInfo.eof())
{
inStrInfo>>lNames[numAccs];
inStrInfo>>fNames[numAccs];
inStrInfo>>midInitials[numAccs];
inStrBal>>acNums[numAccs];
inStrBal>>balances[numAccs];
numAccs++;
inStrInfo>>acNums[numAccs];
}

inStrInfo.close();
inStrBal.close();
}


do
{
menu();
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
{
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;

int pos=linearSearch(acno, acNums, numAccs);
if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
if(pos>0)
cout<<acno<<" cannot be assigned to multiple customers."<<endl;
else
{
cout<<"Enter the first name>"<<endl;
cin>>fName;
cout<<"Enter the last name>"<<endl;
cin>>lName;
cout<<"Enter middle initial>"<<endl;
cin>>midInit;
cout<<"Enter initial deposit>"<<endl;
cin>>initBal;
}

if(initBal < 25.00)
cout<<"Insufficient funds to open account."<<endl;
else
openAccount(acno,fName,lName,midInit,initBal,acNums,fNames,lNames,
midInitials,balances,numAccs);
}
break;

case 2:
{
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;

if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
else if(linearSearch(acno,acNums,numAccs)<0)
cout<<"Account number does not exist"<<endl;
else
closeAccount(acno,acNums,fNames,lNames,
midInitials,balances,numAccs);
}
break;


case 3:
{
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;
if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
else if(numAccs<0)
cout<<"Account number does not exist"<<endl;
else
{
cout<<"Enter deposit amount>"<<endl;
cin>>amount;
}

if(amount<=0.00)
cout<<"Amount must be at least $0.01."<<endl;
else
deposit(acno,amount,acNums,balances,numAccs);
}
break;

case 4:
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;
if(acno.length()!=10)
cout<<"Account number must be ten digits.";
else if(numAccs<0)
cout<<"Account number does not exist"<<endl;
else
{
cout<<"Enter withdrawal amount>"<<endl;
cin>>amount;
}
if(amount<=0.00)
cout<<"Amount must be at least $0.01."<<endl;
else
withdraw(acno, amount, acNums,balances,numAccs);
break;

case 5:
cout<<"Enter a ten digit account number>"<<endl;
cin>>acno;
if(acno.length()!=10)
cout<<"Account number must be ten digits."<<endl;
else if(numAccs<0)
cout<<"Account number does not exist"<<endl;
else
inquiry(acno,acNums,fNames,lNames,midInitials,balances,numAccs);
break;

case 6:
customersList(acNums,fNames,lNames,balances,numAccs);
break;

case 0:
exit(1);
break;

default:
cout<<"Enter a valid choice."<<endl;
break;
}

}while(choice!=0);

outStrInfo.open(fileNameInfo.c_str(), ios::out);
outStrBal.open(fileNameBal.c_str(), ios::out);

for(int i=0;i>MAX_CUSTOMERS;i++)
{
outStrInfo<<acNums[i]<<" "<<lNames[i]<<" "<<fNames[i]<<" "<<midInitials[i]<<endl;
outStrBal<<acNums[i]<<" "<<balances[i]<<endl;
}

outStrInfo.close();
outStrBal.close();
return 0;
}
Topic archived. No new replies allowed.