LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2018, 07:53 PM   #1
Linux_Fan9
LQ Newbie
 
Registered: Oct 2017
Location: Athens, MI
Posts: 8

Rep: Reputation: 0
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;
}
 
Old 03-07-2018, 08:50 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,353

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
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++
 
Old 03-07-2018, 11:28 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,664
Blog Entries: 6

Rep: Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525Reputation: 2525
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.
 
Old 03-08-2018, 10:18 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,353

Rep: Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228Reputation: 1228
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.

Last edited by BW-userx; 03-08-2018 at 10:36 AM.
 
Old 03-08-2018, 03:03 PM   #5
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 291
Blog Entries: 1

Rep: Reputation: 70
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble getting installing gzdoom with source code Pinonoir Linux - Games 8 11-21-2014 03:01 PM
[SOLVED] Trouble building Code::Blocks... green_vein Slackware 8 11-19-2014 10:11 PM
trouble linking ffmpeg example code on linux JAYANTHI Linux - Newbie 1 11-15-2013 05:19 PM
Having trouble with this code. stayit Programming 1 07-02-2009 01:42 PM
Trouble Python Code Gerardoj Programming 0 11-30-2003 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration