Comparing Characters in an Array

Sep 15, 2011 at 5:26pm
Okay, I am trying to create a cipher. This cipher must be available for different alphabets. The user is prompted to enter the number of characters in their alphabet, then they are asked to enter the alphabet which is stored in a char array. Would I'm trying to do is then have them input a word to be encrypted which moves each letter down the alphabet 3 letters( i.e. WORD becomes ZRUG). I am stuck trying to figure out how to get the word to access the alphabet array and increment from there. What I'm getting is the following: (example uses the tahitian alphabet)


Please enter the number of letters in your alphabet: 13
Please enter the letters of your alphabet, in order, followed by the enter key.
aefhimnoprtuv
You have entered the following: 
a, e, f, h, i, m, n, o, p, r, t, u, v, 
If this is correct press 0, if it's incorrect press 1: 0
Please enter the length of the word you'd like encrypted: 6
Please enter the word to be encrypted followed by the enter key.
manava
You have entered the following: 
manava
If this is correct press 0, if it's incorrect press 1. 0
Encrypt Message: 
avaman


Here is the code:
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>

using namespace std;

//PrintAlphabet function template
template<class Cypher>
void PrintAlphabet(Cypher array[], int value);
//PrintWord function template
template<class Cypher>
void PrintWord(Cypher word[], int value);
//Encrypt function template
template<class Cypher>
void Encrypt(Cypher array[], int value);

int main()
{
	int value;
	int length;
	int correct;
	int i;
	int j;
	char x;
	char array[50];
	char word [50];
	
	
	cout<<"Please enter the number of letters in your alphabet: ";
	cin>>value;
	do
	{
		cout<<"Please enter the letters of your alphabet, in order, followed by the enter key.\n";

		for( i=0; i < value; i++)
		{
			cin>> x;
			array[i]=x;
		}
		
		cout<<"You have entered the following: \n";
		PrintAlphabet(array, value);
	
		cout<<"\nIf this is correct press 0, if it's incorrect press 1: ";
		cin>> correct;
	}while(correct != 0);
	
	
	do
	{
		cout<<"Please enter the length of the word you'd like encrypted: ";
		cin>> length;
		
		cout<<"Please enter the word to be encrypted followed by the enter key.\n";
		for( j=0;j < length; j++)
		{
			cin>> x;
			word[j]=x;
			
		}
		
		cout<<"You have entered the following: \n";
		PrintWord(word, length);
	
		cout<<"\nIf this is correct press 0, if it's incorrect press 1.\n";
		cin>> correct;
	}while(correct != 0);
	
	cout<<"\nEncrypt Message: \n";

	Encrypt(word, length);
	return 0;
}

template <class Cypher>
void PrintAlphabet(Cypher array[], int value)
{
   int i=0;
   for(i=0;i<value;i++)
   	cout<<array[i]<<", ";
}//end PrintAlphabet4


template <class Cypher>
void PrintWord(Cypher word[], int length)
{
	int i=0;
   for(i=0;i<length;i++)
   	cout<<word[i];
}
	
 
template <class Cypher>
void Encrypt(Cypher array[], int value)
{
	int i=0;
	for(i=0; i<value; i++)
	{
		cout<<array[(i + 3) % value];
	}
}
		

//f(p)=(p+3) mod m : m = number of letters in alphabet 


Any help would be greatly appreciated!
Sep 15, 2011 at 5:29pm
Your encrypt function needs 2 things:

1) the alphabet.
2) the word to encrypt.

You're only giving it the word.
Sep 17, 2011 at 11:02pm
How can I pass it the alphabet as well?
Sep 17, 2011 at 11:11pm
You could pass a string, or an array of characters.
Sep 20, 2011 at 3:20pm
I'm still having a hard time here, could anybody give me a little nudge as to what the code might look like in the Encrypt Method? I could really use the help.
Sep 21, 2011 at 1:49am
simply put the abstraction is this:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <string>
#include <iostream>

using namespace std;

string BuildCypherAlphabet()
{
       string CypherAlpha;
       int nCount = 0;
       char Letter;
      
       cout<<"Please enter the number of letters in your alphabet: ";
       cin>>nCount;

       if(cin.good())
       {
              for(int nIndex  = 1; nIndex == nCount; nIndex++)
              {
                      	cout<<"Please enter the letters of your alphabet, in order, followed by the enter key.\n";
                        cin >> Letter;
                        if(cin.good())
                        {
                                CypherAlpha+=Letter;
                        }
                        else
                        {
                                cout << "You didn't enter a Letter.  Please try again." << endl;
                         }
               }
        }
        return CypherAlpha;
}

string EncryptWord(string BaseWord, string CypherAlpha)
{
         string EncryptedWord;

         for(int nIndex= 0; nIndex < BaseWord.length(); nIndex++)
         {
                // encryption math ... my stuff isn't yours...
                int result = (int)BaseWord[nIndex] % CypherAlpha.length();
                if(result < CypherAlpha.length())
                {
                       EncryptedWord+= CypherAlpha[result];
                }
                else
                {
                       // not sure what I am going to do here, not sure what your math will result in.
                 }
        }
        return EncryptedWord;

}

int main()
{
           string CypherAlphabet = BuildCypherAlphabet();
           
           string BaseWord;

           cout << "Please enter a word to Encrypt :";
           cin >> BaseWord;

           // do verification if you need to here of the word...
           
           string Encrypted = EncryptWord(BaseWord, CypherAlphabet);
          
           cout << BaseWord << " Encrypts into: " << Encrypted << endl;

}


This is a crude thought process but I hope it points you in the right Direction. I don't like doing homework for people. I didn't have homework when I learned to program, nor sites like this to turn to. Just books. I really think they need to go into using classes before the teach you how to construct them.
Topic archived. No new replies allowed.