/*I've actually located the segmentation fault location, I believe it is because I am trying to declare one array memory location equal to another when they are in seperate memory segments. However, if this is the case I have no idea on how to correct it. Here is my code so far. I went through and commented out chunks at a time to isolate where the segmentation fault occurs (line 70) I will comment the line below causing it.*/
#include<iostream>
using namespace std;
// INPUT - a char array
// OUTPUT - none
// RETURN - a char array
char * soundex(const char word[],char sound[]);
int main()
{
char sound[40];//so the house isn't fire bombed
char word[40];//user inputs their word here
//Prompt user for an input
cout<<"Please input 10 words you would like to know the soundex code of"<<endl;
for(int c=1;c<=10;++c)
{
cin>>word;
cout<<word<<" "<<soundex(word,sound)<<endl;
}
return 0;
}
char * soundex(const char word[],char sound[])
{
char word2[40];// used to puttz around with
for(int b=0;b<40;++b)
//copy word[] to word2[]
{
word2[b]=word[b];
if (word[b]=='\0')
break;
}
sound="Z0000"; // initialize sound so any 0's are contained
word2[0]=toupper(word[0]); //used to make the loop nicer
for(int i=1;i<40;++i)
{
if(word[i]=='\0')// if you reach a null character stop
break;
word2[i]=toupper(word[i]);//convert everything to a standard
switch(word2[i])
/* convert word[] to the digits 0-6 depending on
their soundex coding - note because of the for loop this
only applies to word[1+] */
{
case 'B': case 'P': case 'F': case 'V':
word2[i]='1';break;
case 'C': case 'S': case 'K': case 'G': case 'J': case 'Q':
case 'X': case 'Z':
word2[i]='2';break;
case 'D': case 'T':
word2[i]='3';break;
case 'L':
word2[i]='4';break;
case 'M':case'N':
word2[i]='5';break;
case 'R':
word2[i]='6';break;
case '\0': //even though null wont make it this far...
word2[i]='\0';/*I did this because I felt it might fix
the segmentation fault. But it didn't*/
default:
word2[i]='0';break;
}
}
sound[0]=word2[0]; // set sound[0] the same as word2[0]
/* The above line is the first instance of the segmentation fault occuring, it also happens below in the while loop, most likely for the same reason. However, as far as I know this is syntactically correct (as I used it in the above code to copy word[] into word2[]) and it is logically what I need to do.*/
int count = 1; // used for the while loop as a counter
int position = 1; // used for the index
while(count<=5)
{
/* As long as the char is not the same as the previous and is
not a zero, assign it to sound. If a null is reached end. Also
copies nulls before ending*/
if(word2[position]!=word2[position-1] && word2[position]!='0')
{
sound[count]=word2[position];
/* The segmentation fault most likely occurs here as well*/
++count;
}
++position;
if(word[position]=='\0')
break;
}
return sound;
}
/* Any help would be greatly appreciated! Right now I am lost as to how to fix this, I have tried a lot of different approaches including declaring a char x = word2[0] then sound[0] = x
but everything I did had the same segmentation fault. I am hoping that it is not because I am being limited to too few memory spaces am exceeding that... if that is the case I'm hoping there is a way around it*/
Arctic 0 Newbie Poster
Agni 370 Practically a Master Poster Featured Poster
vijayan121 1,152 Posting Virtuoso
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
Arctic 0 Newbie Poster
vijayan121 1,152 Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.