LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I'm having trouble with my soundex code. (https://www.linuxquestions.org/questions/programming-9/im-having-trouble-with-my-soundex-code-4175625171/)

Linux_Fan9 03-07-2018 07:53 PM

I'm having trouble with my soundex code.
 
(Please note that this is not a homework assignment.) I'm trying to do a soundex code where it writes out something like this: C160, but it comes out like this: C000. How do I fix this? Please Help me.
// ReverseName.cpp
// By Gage Haldey
// Copyright 2018
// Licensed under the terms of the GPL 3
// To compile: g++ -o ./ReverseName.bin ./ReverseName.cpp
// To run: ./ReverseName.bin
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
# include <iostream>
# include <string.h>
using namespace std;

//Delaire Variables
int X,Y,NameLength;
char Name[40],Number[40],Soundex[4];

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void shift()
{
for (Y=X;Y<NameLength;Y++)
Number[Y]=Number[Y+1];
return;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
//Input
cout<<"What is your last name? ";
cin>>Name;
//Calculations
NameLength=strlen(Name);
for (X=0;X<NameLength;X++) Name[X]=toupper(Name[X]);
for (X=0;X<NameLength;X++)
switch(Name[X])
{
case 'B':
case 'F':
case 'P':
case 'V':Number[X]='1'; break;
case 'C':
case 'G':
case 'K':
case 'J':
case 'Q':
case 'S':
case 'X':
case 'Z':Number[X]='2'; break;
case 'D':
case 'T':Number[X]='3'; break;
case 'L':Number[X]='4'; break;
case 'M':
case 'N':Number[X]='5'; break;
case 'R':Number[X]='6'; break;
case 'H':
case 'W':Number[X]='7'; break;
default: Number[X]='0';
}
for (X=0;X<4;X++) Soundex[X]='0';
Soundex[0]=Name[0];
//Output
cout<<"Your last name converted is ";
for (X=0;X<NameLength;X++) cout<<Number[X];
cout<<".\n";
for (X=0;X<NameLength;X++)
if(Number[X]==Number[X+1])
shift();
cout<<"Your last name shifted is ";
for (X=0;X<NameLength;X++) cout<<Number[X];
cout<<".\n";


cout<<"Your Soundex code is: ";
for (X=0;X<4;X++) cout<<Soundex[X];
cout<<".";
cin.get();
cin.get();
return 0;
}

BW-userx 03-07-2018 08:50 PM

I am not completely sure of what you're doing, only that your logic has to be off. Adding cout's to your code you can see what it is doing, or use a debugger.

I didn't get a chance to finish this, I got a run and catch that last bus out, sorry,
but if you look at the comments and the code changes I'd made so far in your code i here. Hopefully it will spark something you.



Code:

/*(Please note that this is not a homework assignment.)
I'm trying to do a soundex code where it writes out something like this: C160,
*  but it comes out like this: C000. How do I fix this? Please Help me.
*/
// ReverseName.cpp
// By Gage Haldey
// Copyright 2018
// Licensed under the terms of the GPL 3
// To compile: g++ -o ./ReverseName.bin ./ReverseName.cpp
// To run: ./ReverseName.bin
/////////////////////////////////////////////////////////////////////////
# include <iostream>
# include <string.h>
using namespace std;


//Delaire Variables
int X,Y,NameLength;
char Name[40],Number[40],Soundex[4];

/////////////////////////////////////////////////////////////////////////
void shift()
{
        cout<<"in shift"<<endl;
for (Y=X;Y<NameLength;Y++)
Number[Y]=Number[Y+1];


}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{

//insiating values to zero.
memset(Name, 0, sizeof(Name));
memset(Number, 0, sizeof(Number));
memset(Soundex, 0, sizeof(Soundex));

//Input
 
cout<<"What is your last name? ";
cin>>Name;
//Calculations
 
NameLength=strlen(Name);
 

for (X=0;X<NameLength;X++)
{
        Name[X]=toupper(Name[X]);
        cout<<Name[X]<<endl;
}

for (X=0;X<NameLength;X++)

switch(Name[X])
{
case 'B':
case 'F':
case 'P':
case 'V':Number[X]='1'; break;
case 'C':
case 'G':
case 'K':
case 'J':
case 'Q':
case 'S':
case 'X':
case 'Z':Number[X]='2'; break;
case 'D':
case 'T':Number[X]='3'; break;
case 'L':Number[X]='4'; break;
case 'M':
case 'N':Number[X]='5'; break;
case 'R':Number[X]='6'; break;
case 'H':
case 'W':Number[X]='7'; break;
default: Number[X]='0';
}

// putting all zeros in your array here.
for (X=0;X<4;X++)
        Soundex[X]='0';
       
// bad assignment
//Soundex[0]=Name[0];

//changed it to this
memcpy(Soundex, Name, sizeof(Name));
cout<<"Soundx = "<<Soundex<<endl;

for (int g = 0; g < NameLength ; g++)
        cout<<Soundex[g];

//Output
cout<<"Your last name converted is ";
for (X=0;X<NameLength;X++) cout<<Number[X];
cout<<".\n";
for (X=0;X<NameLength;X++)

cout<<"Number[x] = "<<Number[X]<<endl;
cout<<"Number[x+1] = "<<Number[X+1]<<endl;

        if(Number[X]==Number[X+1])
                shift();

cout<<"Your last name shifted is ";
for (X=0;X<NameLength;X++) cout<<Number[X];
cout<<".\n";

// here all you're doing is priniting out what is
//inside of if which is zeros.
cout<<"Your Soundex code is: ";
for (X=0;X<4;X++)
        cout<<Soundex[X];
cout<<".";
 

cout<<endl;
return 0;
}

When you're calling your shift () function the two numbers in series have to be one more then the first number. If you look at the numbers being gotten then check your condition again, you'll see it never returns true, so your shift function never gets called.



results
Code:

$ ./a.out
What is your last name? bob
3Your last name converted is 101.
Number[x] = 1
Number[x] = 0
Number[x] = 1
Number[x+1] =
in shift
Your last name shifted is 101.
Your Soundex code is: B000.
userx@void_ssd:~/scripts/C++
$ ./a.out
What is your last name? sally
5Your last name converted is 20440.
Number[x] = 2
Number[x] = 0
Number[x] = 4
Number[x] = 4
Number[x] = 0
Number[x+1] =
in shift
Your last name shifted is 20440.
Your Soundex code is: S000.

My last set of Results to the code posted.
Code:

userx@void_ssd:~/scripts/C++
$ ./a.out
What is your last name? sally
S
A
L
L
Y
Soundx = SALLY
SALLYYour last name converted is 20440.
Number[x] = 2
Number[x] = 0
Number[x] = 4
Number[x] = 4
Number[x] = 0
Number[x+1] =
in shift
Your last name shifted is 20440.
Your Soundex code is: SALL.
userx@void_ssd:~/scripts/C++


astrogeek 03-07-2018 11:28 PM

Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

BW-userx 03-08-2018 10:18 AM

no brackets and / or spaces, make it hard to read to understand what you're really trying to do here.
Code:

for (X=0;X<NameLength;X++)
if(Number[X]==Number[X+1])
shift();

even though the for loop fires off the if conditional which if true it fires off the sift function.


Code:

//using string comparison for numbers.
/*
for (X=0;X<NLen;X++)
    if(Number[X]==Number[X+1])
        shift();
*/

for (X=0; X < NLen; X++)
    if (Number.compare(X,Number.size(),Number,(X+1),Number.size()) == 0)
        shift();

if they never match you'll never get a shift function call

Well, I got too confused by your code and trying to fix it, so I looked up "soundex Code" to see if that was a real thing. Which I found it is. Look at this example to try and get a better Idea of what you're suppose to be doing, and I am goig to post my changes, so you can hopefully beef up on your C++, by trying to use C++ in your C++ code and not C. ;)

Using C++ string arrays, and not C char arrays.
Code:

/*(Please note that this is not a homework assignment.)
I'm trying to do a soundex code where it writes out something like this: C160,
*  but it comes out like this: C000. How do I fix this? Please Help me.
*/
// ReverseName.cpp
// By Gage Haldey
// Copyright 2018
// Licensed under the terms of the GPL 3
// To compile: g++ -o ./ReverseName.bin ./ReverseName.cpp
// To run: ./ReverseName.bin
/////////////////////////////////////////////////////////////////////////
# include <iostream>
# include <string>
using namespace std;


//Delaire Variables
    unsigned int X,Y,NLen;
//char Name[40],Number[40],Soundex[4];
    string Name,Number,Soundex;

/////////////////////////////////////////////////////////////////////////
void shift()

 
    cout<<"in Shift"<<endl;
    for (Y=0;Y<Name.size();Y++) 
           
        Number[Y]=Number[Y+1];
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{

 
//Input
 
cout<<"What is your last name? ";
cin>>Name;
//Converting to upper case
for (X=0;X<Name.size();X++)
    Name[X]=toupper(Name[X]);
 
//get length
NLen = Name.size();

for (X=0;X<Name.size();X++)
{

    switch(Name[X])
    {
        case 'B':
        case 'F':
        case 'P':
        case 'V':Number += '1'; break;
        case 'C':
        case 'G':
        case 'K':
        case 'J':
        case 'Q':
        case 'S':
        case 'X':
        case 'Z':Number += '2'; break;
        case 'D':
        case 'T':Number += '3'; break;
        case 'L':Number += '4'; break;
        case 'M':
        case 'N':Number += '5'; break;
        case 'R':Number += '6'; break;
        case 'H':
        case 'W':Number += '7'; break;
        default: Number += '0'; break;
    }

}
//Output

/*
for (X=0;X<4;X++)
    Soundex[X]='0';

Soundex[0]=Name[0];
*/

    Soundex = Name;
    cout<<"Soundex "<<Soundex<<" Name "<<Name<<endl;

cout<<"Your last name converted is ";

/*
for (X=0;X<NLen;X++) cout<<Number[X];

cout<<endl;
*/

//can be written
cout<<Number<<endl;


//using string comparison for numbers.
/*
for (X=0;X<NLen;X++)
    if(Number[X]==Number[X+1])
        shift();
*/

for (X=0; X < NLen; X++)
{
    cout<<Number[X]<<" "<<Number[X+1]<<endl;
    if (Number.compare(X,Number.size(),Number,(X+1),Number.size()) == 0)
        shift();
    }
       
cout<<"Your last name shifted is "<<Number<<endl;
cout<<"Your Soundex code is: "<<Soundex<<"."<<endl<<endl;
return 0;
}

your shift function seems to be wrong. I do not think that will fix the everything you're trying to do here, so anyways, this is what you're trying to do.
http://www.genealogyintime.com/Genea...ork_page1.html

this is an example of how to do it.
http://www.cplusplus.com/forum/beginner/1999/

good luck, remember, brackets where needed and indentation of your code for readability. and use the forums code blocks to place your code and special information into a different format for easier reading as well. It's simple html ' ish click "go advanced" then click the # sign to get them, then place your code and special information between the two.

dogpatch 03-08-2018 03:03 PM

You probably want to add something like
Code:

for (X=Y=1;X<NameLength;X++) {
  if (Number[X] > '0')
  Soundex[Y++]=Number[X];
  }

before the final SoundEx display to eliminate the embedded 0s (vowels)


All times are GMT -5. The time now is 07:42 PM.