help with my code results are off for this array or string function

The task that i am in need to do is write a program to encrypt and decrypt message. The following is the encrypt mechanism:

Each character in the message will be replaced by an index of the corresponding
character appear in the email. The index is generated by this way: start with the first character C0 in the message, the program searches the first occurrence of C0 in the email start from position 0, the first character in the email. When the character is found, it outputs/records the index and turn to search the first occurrence of second character C1 in the message. The searching will be started from the next character from the current position, i.e. index to character mapping is unique. If the character cannot be found till the end of the email, the program will continue search the character from the beginning of the email until the whole email is searched. Although the searching starts from the position 0 again, the index is not reset. Therefore, for an email with 100 characters, index 0, 100, 200, 300 are all referred to the character at position 0. For a message with any character cannot be found in the email, the program will print out error message.



SO basically i have written this for the decrypt mechanism yet the results is soo off.


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


using namespace std;

int main(){
    const string data =
"The objective of this assignment is to give student handon\
practice on computer programming, particular on array\
access. Students are asked to apply the material learnt\
in the lectures to solve a real-life problem and complete\
the assignment on their own. Students may also experience\
the pressure of finishing a bug-free program under tight\
schedule.";

    char C;
    const char decrypt = 'd';
    const char encrypt = 'e';


    while (cin >> C && (C == decrypt || C == encrypt)){
        if (C == decrypt){
          
            cin >>ws;

													 // read in the line of text that contains the code to decrypt.
            string line;
            getline(cin, line);

													// convert the line into a stream:
            istringstream code_stream(line);

            int code;
            
            while (code_stream >> code)
               cout << data[code];							 // output the decrypted character.

        }
        else if (C == encrypt){


		}
           

        cout << endl;
    }
}




I have also written this yet this barely works at all.

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

using namespace std;
#define N 1000

void main(){
char array1[]={"The objective of this assignment is to give student handon\
practice on computer programming, particular on array\
access. Students are asked to apply the material learnt\
in the lectures to solve a real-life problem and complete\
the assignment on their own. Students may also experience\
the pressure of finishing a bug-free program under tight\
schedule."};

int S[N], a, Q=0, i;
char e, d;
int z = 0;
cin>>a;

if(a=='d'){
		while( z >= 0 && z < 256){
    cin >> z;
	cout << static_cast<unsigned char>(z);
}
cin>>z;
}}



And my first code completely was this and didn't work either

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


using namespace std;
#define N 1000

void main(){

char array1[]={"The objective of this assignment is to give student handon\
practice on computer programming, particular on array\
access. Students are asked to apply the material learnt\
in the lectures to solve a real-life problem and complete\
the assignment on their own. Students may also experience\
the pressure of finishing a bug-free program under tight\
schedule."};

int S[N];
int z;
int i, a, Q=0;
char e;
char d;
cin>>z;
while(z=='d'){
	cin>>S[i];
	for(i=0;i<=array1[N];i++)
		cin>>S[a];
		cout << i<<endl;
for(i=0;i<array1[N];i++)
	cout << i<<endl;
}


}



Any suggestions please i am lost. andyes i'm a beginner and all so my skills are kinda lacking. Any help useful help i can understand as beginner please or even suggestion? thank you in advcance.


results should look like this

INPUT:
d
8 22 53 62 86 95 102 108 111


OUTPUT:
aaaaaaaa



and it is preferred for me to use array. yet another method is fine.
Last edited on
Please anyone help?
Last edited on
Topic archived. No new replies allowed.