-
name encryption
G'day
This is my second post, also i am new to C++ programming so if i make a mistake or something please correct me ;P.
Today i started this program that converts a persons name to code:
Code:
#include <iostream>
using namespace std;
int main()
{
// Database
char cLetters[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char cEncryption[] = {'9', 'x', 'j', '4', 't', '5', 'p', 'y', 'c', '3', 'v', 'b', '2', 'e', 'n', 'w', 'o', 'm', 'i', '1', 'r', '1', '7', 'k', 'u', '8'};
// Input users name
char cMiddleInitial[1];
char cFirstname[13];
char cLastname[13];
cout << "Enter Firstname: ";
cin >> cFirstname;
cout << "Enter Middlename Initial: ";
cin >> cMiddleInitial;
cout << "Enter Lastname: ";
cin >> cLastname;
char cCode[27];
// Converts name to code
int i;
int a;
for(i = 0; i < 26; i++)
{
// firstname
for(a = 0; a < 13; a++)
{
if(cLetter[i] == cFirstname[a])
{
cCode[] = cEncryption[i];
}
}
}
// Output
cout << "Fullname: " << cFirstname << " " << cMiddleInitial << ". " << cLastname << endl;
cout << "Code: " << cCode << endl;
system("Pause");
return 0;
}
I need help with making an array of the code (cCode[]) by going through cLetters[] and getting each char. I tryed using For statements but i cant get it right.
Cheer in advance.
-
Code:
if(cLetter[i] == cFirstname[a])
{
cCode[] = cEncryption[i];
}
}
You have an extra closing brace there.
Basically, you should have it like this:
Code:
string str;
string encoded_str;
input -> str;
for each char "c" in str
for each letter "l" in cletters
if c == l //we've found the code for this letter
add the corresponding letter in cencryption to encoded_str
break //optional, saves some time since we've already found what we wanted
//now encoded_str holds the encoded representation of str
Also, instead of the clumsy
Code:
{'f', 'o', 'o', 'b', 'a', 'r'}
you could just use
That is, if there are no zero characters in your array.
-
Code:
char cMiddleInitial[1];
You should probably either use a single character:
Code:
char cMiddleInitial;
Or leave space for a NULL:
Code:
char cMiddleInitial[2];
-
thanks jafet and dkws for your replies.
I followed jafets outline and got it working.