Vigenere!

i need some help with these codes. please help me

i won't run, the output are emoticons.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  #include <iostream>
using namespace std;

string message;
string mappedKey;

void messageAndKey(){
	string msg;
	cout << "Enter plain text: ";
	getline (cin, msg);
	cin.ignore();
	
	//message to uppercase
	for (int i = 0; i < msg.length(); i++){	
		msg [i] = toupper (msg [i]);
	}
	
	string key;
	cout << "Enter key: ";
	getline (cin, key);
	cin.ignore();
	
	//key to uppercase
	for (int i = 0; i < key.length(); i++){
		key [i] = toupper (key[i]);
	}
	
	//mapping key to message
	string keyMap = "";
	for (int i = 0, j = 0; i < msg.length(); i++){
		if (msg [i] == 32){
			keyMap += 32;
		} else {
			if (j < key.length()){
				keyMap += key [j];
				j++;
			} else {
				j = 0;
				keyMap += key[j];
				j++;
			}
		} // if - else
	} // for
//	cout << msg << "\n" <<keyMap;
	message = msg;
	mappedKey = keyMap;	
}

int tableArr [26][26];	
void createVigenereTable(){
	for (int i = 0; i < 26; i++){
		for (int j = 0; j < 26; j++){
			int temp;
			if ((i + 65)+ j > 90){
				temp = ((i + 65) + j) - 26;
				//adding ASCII of alphabet letter in the table index position
				tableArr [i][j] = temp;
			} else {
				temp = (i + 65) + j;
				//adding ASCII of alphabet letter in the table index position
				tableArr [i][j] = temp;
			}
		}// for j loop
	}// for i loop
	
	//testing table
//	for (int i = 0; i < 26; i++){
//		for (int j = 0; j < 26; j++){
//			cout << (char) tableArr [i][j] << " ";
//		}
//		cout << endl;
//	}
}

void cipherEncryption (string message, string mappedKey){
	createVigenereTable();
	string encryptedText = "";
	for (int i = 0; i < message.length(); i++){
		if (message [i] = 32 && mappedKey [i] == 32){
			encryptedText += "";
		} else {
			int x = (int) message [i] - 65;
			int y = (int) mappedKey [i] - 65;
			encryptedText += (char) tableArr [x][y];
		}
	}
	
	cout << "Encrypted Text: " <<encryptedText;
}

int itrCount (int key, int msg){
	int tigIhap = 0;
	string result = "";
	
	//starting from ASCII of letter of Key and ending at letter of message
	// to get full 26 letters of alphabet
	for (int i = 0; i < 26; i++){
		if (key + i > 90){
			result += (char) (key+(i-26));
		} else {
			result += (char) (key+i);
		}
	}//for 
	
	for (int i = 0; i < result.length(); i++){
		if (result [i] == msg){
			break;
		} else {
			tigIhap++;
		}
	}
	return tigIhap;
}

void cipherDecryption (string message, string mappedKey){
	string decryptedText = "";
	for (int i = 0; i < message.length(); i++){
		if (message [i] == 32 && mappedKey [i] == 32){
			decryptedText += " ";
		} else {
			int temp = itrCount ((int) mappedKey [i], (int) message [i]);
			decryptedText += (char) (65 + temp);
		}
	}
}

int main(){
	int choice;
	
	cout << "***** ENIGMA *****" <<endl;
	cout << "[1] Encrypt" <<endl;
	cout << "[2] Decrypt" <<endl;
	cout << "[5] Exit" <<endl;
	cout << "Enter your choice: ";
	cin  >> choice;
	cin.ignore();
	
	if (choice == 1){
		cout << "***** ENCRYPTION *****" <<endl;
		messageAndKey();
		cipherEncryption (message, mappedKey);
	} else if (choice == 2) {
		cout << "***** DECRYPTION *****" <<endl;
		cipherDecryption (message, mappedKey);
	} else {
		cout << "WRONG CHOICE!" <<endl;
	}
}	
it ran fine for me, but its wonky.
your I/O for message and key forces me to enter the data, then enter a bogus line, then enter the key... etc

I think you are misusing the cin.ignore(). Take another look at that.

you don't need a loop to toupper, look at std::transform(). not important, just a little cleaner. you can do it with cast too, but my personal opinion casting should not modify, so I don't use that way.
Last edited on
Topic archived. No new replies allowed.