Need assistance with my display of my output?

How can I get my output to display the message on single lines rather than in a huge chunk? I want to display the encrypted message one by on, but I am getting a paragraph when I run it. Please help, thanks.

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

using namespace std;

void get_input(string& message, int& key);
vector<char> encrypt(string message, int key);
void display(vector<char> encrypted);


int main ()
{
	string message;
	int key;
	get_input(message, key);
	vector<char> encrypted = encrypt(message, key);
	display(encrypted);

	return 0;
}

void get_input(string& message, int& key)
{
	cout << "Enter an encrypted message: \n" << endl;
	getline(cin, message);
	//cout << "Please enter a key between 1 and 100 for use in encrypting your message: ";
	//cin >> key;
}

vector<char> encrypt(string message, int key)
{
	vector<char> encrypted;

	for( key = 1; key <= 100; key++)
    {

        for (int i = 0; i < message.length(); i++)
            {
                if(int(message[i]) + key > 126)
                {
                    encrypted.push_back(char(32 + ((int(message[i]) + key) - 127)));
                }
                else
                {
                    encrypted.push_back(char(int(message[i]) + key));
                }
            }

        }
        cout << endl;
        return encrypted;



}

void display(vector<char> encrypted)
{
	cout << "Decrypted message is:" << endl;

	for (int i = 0; i < encrypted.size(); i++)
        {
            cout << encrypted[i];
        }
	cout << endl;
}
Also, can someone please tell me why this isn't working? I want to display the encrypted message and the key number I used. Thanks.

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

#include<iostream>
#include<string>
#include<vector>

using namespace std;

void get_input(string& message, int& key);
vector<char> encrypt(string message, int key);
void display(vector<char> encrypted);


int main ()
{
	string message;
	int key;
	get_input(message, key);
	vector<char> encrypted = encrypt(message, key);
	display(encrypted);

	return 0;
}

void get_input(string& message, int& key)
{
	cout << "Enter an encrypted message: \n" << endl;
	getline(cin, message);
	//cout << "Please enter a key between 1 and 100 for use in encrypting your message: ";
	//cin >> key;
}

vector<char> encrypt(string message, int key)
{
	vector<char> encrypted;

	for( key = 1; key <= 100; key++)
    {

        for (int i = 0; i < message.length(); i++)
            {
                if(int(message[i]) + key > 126)
                {
                    encrypted.push_back(char(32 + ((int(message[i]) + key) - 127)));
                }
                else
                {
                    encrypted.push_back(char(int(message[i]) + key));
                }
            }
            cout << endl;
            cout << "The decrypted message using Key:" << key << ":";
            cout << encrypted << endl;    //I get compiler error here.

        }
        



}


Topic archived. No new replies allowed.