/*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*/

dude i just executed your code .. n it was as smooth as fine scotch on rocks ... no segmentation faults... wish i could help...

char* soundex( const char word[], char sound[] )
{
   // ...
   sound="Z0000"; // *** trouble ****
   // ...
}

a literal string (eg "Z0000") consists of non-modifiable chars. if you had enabled all warnings, the compiler would have alerted you that something was amiss here.
eg. warning: deprecated conversion from string constant to 'char*' (g++)
the array name sound decays to a pointer (char*) when it is passed to a function and after this assignment points to non-modifiable chars.

strcpy( sound, "Z0000" ) ;

should fix it.

If you're using a C-lib that supports strcpy_s() (like microsoft) it's better to use it. It takes an extra parameter describing the size of the destination buffer and returns an error on overflow, where strcpy() would just cause a segmentationfault. Strcpy_s() isn't standard (yet?), so it may not be supported by your compiler.

Niek

Thanks a ton guys! I changed it and there is no segmentation fault.

Although I was running it with g++32 and -Wall it didn't catch it when that was the only error for me. Even if it did I'm a novice enough to not know what that means. Well now I know, thanks again!

> I was running it with g++32 and -Wall it didn't catch it

yes, earlier versions of g++ (eg. g++ 3.4.6) don't give a warning for this;
even with -Wall -std=c++98 -pedantic

it would be a good idea to compile your code with the switches
-Wall -std=c++98 -pedantic -Werror to get all the warnings.
-Werror is just to make sure that the warnings are not ignored.

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.