soundex issues

I have a question as to how I can loop through my if statements and replace my array indexes with a variable (like i) and still output them to show the soundex code. Any ideas would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <iostream>
#include <string.h>

using namespace std;

int main () {

	char tmp, tmp1, tmp2, off;
	char word[100];
	
	while (1) {



		cin >> word;

		if (cin.eof()) break;

	
		tmp= toupper(word[0]);
		
		if (word[1]=='b', word[1]=='f', word[1]=='p', word[1]=='v')
		{
			tmp1= '1';
		}
		else if (word[1]=='c', word[1]=='g', word[1]=='j', word[1]=='k', word[1]=='q', word[1]=='s',word[1]=='x', word[1]=='z')
		{
			tmp1='2';
		}
		else if (word[1]=='d', word[1]=='t')
		{
			tmp1='3';
		}
		else if (word[1]=='l')
		{
			tmp1='4';
		}
		else if (word[1]=='m', word[1]=='n')
		{
			tmp1='5';
		}
		else if (word[1]=='r')
		{
			tmp1='6';
		}
		else tmp1='0';


		cout << tmp << tmp1 << tmp2 << endl;	
		}
	return 0;
	}

Last edited on
Excuse my ignorance, but what's "soundex"? What purpose does this program serve?

Anyway, you could create a simple 'for' loop around your if/else statements, starting from index zero and going until you reach the null-terminating control character. Check out the 'for loop' section at:

http://www.cplusplus.com/doc/tutorial/control/

Also, perhaps consider using a switch statement instead of a bunch of if/else statements.
Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English. The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling. The algorithm mainly encodes consonants; a vowel will not be encoded unless it is the first letter. Soundex is the most widely known of all phonetic algorithms, as it is a standard feature of MS SQL and Oracle, and is often used (incorrectly) as a synonym for "phonetic algorithm". The program is is just for fun as I had recently learned about soundex myself. I am new to C++ and decided to try to test my skills.

Thank you for your response.
Your if statements are incorrect -- a comma-separated expression list will always return the value of the last expression in the list. What you should have is

23
24
if (word[1]=='b' || word[1]=='f' || word[1]=='p' || word[1]=='v')
  ...

The next problem is that you are applying a condition that belongs to all non-vowel elements to only parts of the expression. What if word[2] is a vowel?


The way I solved it (I wrote a SOUNDEX program some time ago) was to apply a transform function to each element of the word except the first using the std::transform() algorithm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char soundex_code( char c )
  {
  // This is the function that does your code transform,
  // as you did above with the if statements.

  if (c == 'b' || c == 'f' || c == 'p' || c == 'v') return '1';
  ...
  return c;
  }

...

// Here is where we apply the above function to
// every character in the word except the first.
std::transform(
  word.begin() + 1,
  word.end(),
  word.begin() + 1,
  &soundex_code
  );


Finally, your overall algorithm needs a little help.

1. Check that the given word does not contain any non-alphabetic characters, as SOUNDEX does not accept them.

2. Convert the characters in your string to the same case. I chose UPPERCASE for potential internationalization considerations and because SOUNDEX is supposed to come out with the first letter in uppercase. Do not assume the input is properly formatted! (The std::transform() algorithm is useful for this one also.)

3. Apply your letter to SOUNDEX code transformer as above.

4. Collapse adjacent identical digits. (The std::unique() algorithm is useful for this.)

5. Remove all non-digits following the first letter. (The std::remove_if() algorithm is useful here.)

6. Since it is possible that your resulting string is now shorter than four digits, you must pad it out with zeros. My code looks something like this:
1
2
word += "000";
word.resize( 4 );
But, of course, that is not the only way to do it.

That's it. Good luck!
Topic archived. No new replies allowed.