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; } |
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.) results Code:
$ ./a.out Code:
userx@void_ssd:~/scripts/C++ |
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.
|
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++) Code:
//using string comparison for numbers. 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.) 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. |
You probably want to add something like
Code:
for (X=Y=1;X<NameLength;X++) { |
All times are GMT -5. The time now is 07:42 PM. |