Segmentation Fault with Switch Statements

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);
You often get a Segmentation fault when you try to access memory you are not supposed to. Like indexing an array out of bounds. Are you sure you want <= instead of < inside closeAccount?
Topic archived. No new replies allowed.