[C++] Segmentation Fault {Novice C++ Programmer}
Hello folks.
In the following function, cleanSpace [which takes in a string such as
Code:
this is\ta\ta a test
. It cleans it up. I am recieving a segmentation fault on one line when I apply it to a second program [my second school assignment]. I do not understand what the problem is here.
Problem is on the bolded line.
Code:
int Account::cleanSpace(char c[])
{
int i=0,
j=0,
k=0,
m=0;
/* Ultimately this loop will scan for new lines and tabs and replace them
with spaces. */
for(i=0; c[i]; i++)
{
if(c[i] == '\n' || c[i] == '\t')
c[i] = ' ';
}
/* For loop finds character starting point. */
for(i=0; c[i] == ' '; i++)
{
c[m] = c[i+1];
}
/* For loop moves all characters next to the first found character. */
for(i++; c[i]; i++)
{
c[++m] = c[i];
}
/* For loop removes trailing spaces. */
for(i = strlen(c) - 1; c[i] == ' '; i--)
{
c[i] = '\0';
}
/*For loop removes excess spaces. */
for(i = 0; c[i]; i++)
{
if(c[i] == ' ' && c[i+1] == ' ')
{
j = i;
while(c[j] == ' ')
{
j++;
}
for(k = i + 1; c[k]; k++, j++)
{
c[k] = c[j];
}
j=0;
}
}
return strlen(c);
}
The problem is on the bolded line.
Here is my header file, if its needed here.
Code:
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
private:
char customer[251];
char accountNumber[16];
int balance;
public:
Account(){init(",,,;","000000000000000",0);} //Default constructor
Account(char c[], char num[], int b){init(c, num, b);} //Overloaded constructor
void init(char c[], char num[], int b); //Initialization function
int cleanSpace(char c[]); //Cleanspace function
int changeCustomerInfo(char c[]); //Change customer info
void changeAccountNumber(char num[]){strcpy(this->accountNumber, num);}
void changeBalance(int b){this->balance = b;}
void getLastName(char st[]); //Retrieves the last name
void getFirstName(char st[]); //Retrieves the first name
void getCity(char st[]); //Retrieves the city name
void getPhoneNumber(char st[]); //Retrieves the phone number
const char* getCustomer() const{return customer;}
const char* getAccountNumber() const{return accountNumber;}
int getBalance()const{return balance;}
~Account(){} //Destructor
};
#endif
I would appreciate any help, and thanks in advance.