letters to numbers

i want to build a program that gets two strings from the user converts every letter of both strings to a number and adds each number to its corresponding one of the second string and prints out the sum of each letter. Any help is appreciated
So, where's the code you've written so far?

We can offer hints and suggestions on code you've written, we won't write all the code for you.
ok so essentially what im making is an encryption thingy that one of my friends thought of.
you get the message and the key (they work like a vigenere cipher ) and the you do what i described in the first post
my best attempt at this is(no idea what im doing just trying radnom stuff i kinda know of) :
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
 
#include <iostream>
using namespace std;

int a='a';
int i;
int j;
int k;
string msg;
string key;
string message[i];
//string mapmsg[i];
string mappedKey[j];
//string mapkey[j];
int ifvar;//variable for if we want encyrption or decryption
int letterToNum;
int lettertonumkey;
int tablearray[2][52];
int sum;




void encrypt()
{
    cout << "Encryption:" << endl;
    cout << "Enter message and key" << endl;
    cin >> msg;
    cin >> key;
    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++;
            }
        } 
    } 


    message = msg;
    mappedKey = keyMap;
    
    cout << message<< endl;
    cout << mappedKey << endl;
    for(int k=0; k<message;k++ ){
        for (int i = 0; i < message[i]; i++){
            if ((char)message[i] == 'a'-96) {
                letterToNum = 1;
            } else if ((char)message[i] == 'b'-96) {
                letterToNum = 2;
            } else if ((char)message[i] == 'c'-96) {
                letterToNum = 3;
            } else if ((char)message[i] == 'd'-96) {
                letterToNum = 4;
            } else if ((char)message[i] == 'e'-96) {
                letterToNum = 5;
            } else if ((char)message[i] == 'f'-96) {
                letterToNum = 6;
            } else if ((char)message[i] == 'g'-96) {
                letterToNum = 7;
            } else if ((char)message[i] == 'h'-96) {
                letterToNum = 8;
            } else if ((char)message[i] == 'i'-96) {
                letterToNum = 9;
            } else if ((char)message[i] == 'j'-96) {
                letterToNum = 10;
            } else if ((char)message[i] == 'k'-96) {
                letterToNum = 11;
            } else if ((char)message[i] == 'l'-96) {
                letterToNum = 12;
            } else if ((char)message[i] == 'm'-96) {
                letterToNum = 13;
            } else if ((char)message[i] == 'n'-96) {
                letterToNum = 14;
            } else if ((char)message[i] == 'o'-96) {
                letterToNum = 15;
            } else if ((char)message[i] == 'p'-96) {
                letterToNum = 16;
            } else if ((char)message[i] == 'q'-96) {
                letterToNum = 17;
            } else if ((char)message[i] == 'r'-96) {
                letterToNum = 18;
            } else if ((char)message[i] == 's'-96) {
                letterToNum = 19;
            } else if ((char)message[i] == 't'-96) {
                letterToNum = 20;
            } else if ((char)message[i] == 'u'-96) {
                letterToNum = 21;
            } else if ((char)message[i] == 'v'-96) {
                letterToNum = 22;
            } else if ((char)message[i] == 'w'-96) {
                letterToNum = 23;
            } else if ((char)message[i] == 'x'-96) {
                letterToNum = 24;
            } else if ((char)message[i] == 'y'-96) {
                letterToNum = 25;
            } else if ((char)message[i] == 'z'-96) {
                letterToNum = 26;
            }
    }

        //---------------------key from down here
        for(int j=0; j<mappedKey[j]; j++) {
            if ((char)mappedKey[j] == 'a'-96) {
                lettertonumkey = 1;
            } else if ((char)mappedKey[j] == 'b'-96) {
                lettertonumkey = 2;
            } else if ((char)mappedKey[j] == 'c'-96) {
                lettertonumkey = 3;
            } else if ((char)mappedKey[j] == 'd'-96) {
                lettertonumkey = 4;
            } else if ((char)mappedKey[j] == 'e'-96) {
                lettertonumkey = 5;
            } else if ((char)mappedKey[j] == 'f'-96) {
                lettertonumkey = 6;
            } else if ((char)mappedKey[j] == 'g'-96) {
                lettertonumkey = 7;
            } else if ((char)mappedKey[j] == 'h'-96) {
                lettertonumkey = 8;
            } else if ((char)mappedKey[j] == 'i'-96) {
                lettertonumkey = 9;
            } else if ((char)mappedKey[j] == 'j'-96) {
                lettertonumkey = 10;
            } else if ((char)mappedKey[j] == 'k'-96) {
                lettertonumkey = 11;
            } else if ((char)mappedKey[j] == 'l'-96) {
                lettertonumkey = 12;
            } else if ((char)mappedKey[j] == 'm'-96) {
                lettertonumkey = 13;
            } else if ((char)mappedKey[j] == 'n'-96) {
                lettertonumkey = 14;
            } else if ((char)mappedKey[j] == 'o'-96) {
                lettertonumkey = 15;
            } else if ((char)mappedKey[j] == 'p'-96) {
                lettertonumkey = 16;
            } else if ((char)mappedKey[j] == 'q'-96) {
                lettertonumkey = 17;
            } else if ((char)mappedKey[j] == 'r'-96) {
                lettertonumkey = 18;
            } else if ((char)mappedKey[j] == 's'-96) {
                lettertonumkey = 19;
            } else if ((char)mappedKey[j] == 't'-96) {
                lettertonumkey = 20;
            } else if ((char)mappedKey[j] == 'u'-96) {
                lettertonumkey = 21;
            } else if ((char)mappedKey[j] == 'v'-96) {
                lettertonumkey = 22;
            } else if ((char)mappedKey[j] == 'w'-96) {
                lettertonumkey = 23;
            } else if ((char)mappedKey[j] == 'x'-96) {
                lettertonumkey = 24;
            } else if ((char)mappedKey[j] == 'y'-96) {
                lettertonumkey = 25;
            } else if ((char)mappedKey[j] == 'z'-96) {
                lettertonumkey = 26;
            }
        }
        sum=letterToNum+lettertonumkey;
        cout << sum << " ";


    }
   
}






int main (){
    cout << "1. Encryption, 2. Decryption" << endl;
    cin >> ifvar;
    while(ifvar!=1 && ifvar!=2){
        cout << "Wrong give a correct number" << endl;
        cout << "1. Encryption, 2. Decryption" << endl;
        cin >> ifvar;
    }
    if(ifvar==1){
        encrypt();
    }//else{
      //  decrypt();
    //}
   
    

}







Last edited on
PLEASE learn to use code tags, they make reading and commenting on code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add code tags.

Characters in C and C++ are numbers, ASCII code numbers.

http://www.cplusplus.com/doc/ascii/

Add two characters together and assign the addition results to an integer.

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
#include <iostream>
#include <string>

int main()
{
   // create two sample strings
   std::string one = "Hello World!";
   std::string two = "Test String";

   // determine which has the smallest size
   size_t size = (one.size() > two.size() ? two.size() : one.size());

   // loop to the smallest string size
   for (size_t i { }; i < size; i++)
   {
      // add each strings elements, assign to int
      int num = one[i] + two[i];

      // get the number value of each string element
      int num_one = one[i];
      int num_two = two[i];

      std::cout << one[i] << " (" << num_one << ")\t+ "
                << two[i] << " (" << num_two << ") = " << num << '\n';
   }
}

H (72)  + T (84) = 156
e (101) + e (101) = 202
l (108) + s (115) = 223
l (108) + t (116) = 224
o (111) +   (32) = 143
  (32)  + S (83) = 115
W (87)  + t (116) = 203
o (111) + r (114) = 225
r (114) + i (105) = 219
l (108) + n (110) = 218
d (100) + g (103) = 203

Last edited on
thank you, ill try to build the whole thing.
So this is what I ended up with:

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
#include <iostream>
#include <string>
using namespace std;


int num;
int choice;
int num_message;
int num_key;
string message;
string key;
string keyMap;
string mappedKey;
string msg;

size_t size = (message.size() == mappedKey.size() ? mappedKey.size() : message.size());

int main (){
    
    cout << "give message" << endl;
    cin >> message;
    cout << "give key" << endl;
    cin >> key;
    
    for (int k = 0,j = 0; k <message.size();k++){
        if(message[k] ==32){
            keyMap += 32;
        } else {
            if(j<key.size()){
                keyMap += key[j];
                j++;
            } else {
                j = 0;
                keyMap += key[j];
                j++;
            }
        } 
    } 

    cout << message << "\n" << keyMap;
    mappedKey = keyMap;
    cout << endl;
    cout << "1. Encryption, 2.Decryption" << endl << "Choose:" << endl;
    cin >> choice;
    
    if(choice==1){
        
        
        for(size_t i { }; i < size; i++){
        
            num = message[i]+mappedKey[i];
            int num_message = message[i];
            int num_key = mappedKey[i];
            num = num - 96;
            cout << message[i] << " (" << num_message << ")\t+ "
            << mappedKey[i] << " (" << num_key << ") = " << num << " " << (char)num << '\n';
            
            num = 0;
        }
        
    } else if(choice==2){
        
        for(size_t i { }; i < size; i++){
            num = message[i]-mappedKey[i];
            int num_message = message[i];
            int num_key = mappedKey[i];
            num = num + 96;
            
            cout << message[i] << " (" << num_message << ")\t- "<< mappedKey[i] 
            << " (" << num_key << ") = " << num << " "<< (char)num << '\n';
            
            num = 0;
        }
        
    } else {
        cout << "Wrong choice" << endl;
    }
    
}

Topic archived. No new replies allowed.