Hello,
I am having some difficulty making soundex rules work in my code. I am able to get the first character and some of the following digits but am having difficulty getting them to work the way they should. For instance appending zero's to the end of the soundex code when it is less than 4 digits long (T50 for ten instead of T5000). This project requires the conversion take place in a function and the soundex code should be the first letter followed by 4 numerals. I am posting the code so please if you have any suggestions please post.
The SOUNDEX conversion won't work properly if you try to do more than one step at a time. (Again I refer you to the Wikipedia article.)
Example:
Higgins
Step 1: SOUNDEX consonant lookup
This is where you use your function to modify every letter (except the first) to have either its numeric value or something invalid. (I just responded to your other thread with an example.)
HI22I52
Step 2: Collapse adjacent identical digits.
HI2I52
Step 3: Remove all non-digits (except for the first letter)
H252
Step 4.1: Append zeros if needed (or just do it anyway)
H252000
Step 4.2: Return the first four characters (one letter + three digits):
H252
If I were you, I would make each step a function. You can have it work directly on a given character array if you want.
1 2 3 4 5
// somewhere in main()
char s[ 100 ];
strcpy( s, input_names[ n ] );
soundex( s );
strcpy( output_codes[ n ], s );
1 2 3 4 5 6
void soundex( char* s )
{
step_one( s );
step_two( s );
...
}
1 2 3 4 5
void step_one( char* s )
{
for (; *s; s++)
*s = soundex_function( toupper( *s ) );
}
Etc.
Good luck!
[edit] Oh yeah, watch that line #65 in your soundex_function(). Invalid inputs should not return a valid output --remember, keep to one step at a time! :-)
This makes sense, I have begun implementing this in the code but when it compiles I get an error saying line 71 "could not convert 'toupper(int)((*s))' 'to char&'. I am thinking that if I change the soundex_function to (char* this would fix it. Does that make sense?
Sorry, I missed that soundex_function takes a reference and returns nothing. You can not take a reference to a function result... but that call to toupper() really belongs inside the function, not as part of its argument list...
Thanks Duoas, that helped a great deal. I now have another issue with the functions that do the conversions. Everything was working great until I added the step_three function and then too many characters are being removed. Any suggestions (any suggestions to other areas of improvement also please)? Thanks again. I guess what I am asking is what would the function look like that would remove the characters which are not numbers?
Take lines 9 and 10 and stick them inside main() (after line 13). Then figure out how to fix step_two() and step_three() to work.
Remember, only do the tiniest thing you can do at any one time. By breaking a big problem down into a bunch of little problems, you no longer have to think about the big problem at all (let alone all at once); you can now focus on solving just one little problem at a time without thinking or caring about any part of the big problem.