C++ help for beginner!

Hi, so i'm new to programming. This is my first course! So i'm new and still learning. I feel like I'm heading the right direction but I could be wrong. I don't know how to encode and decode. I need to encode it with the key from user, such as add an int 1-35. My assignment instructions are

"Write a C++ program that encodes the user’s string so that he can send a “secret message” to his friends. In order to encode the string, we will use an integer offset value known as the codeInteger or encryption key. The encryption key must be between 1 and 35, inclusive. It is added to each character’s ASCII (decimal) value thus making it a new ASCII character. The letter “A” is ASCII 65. If we add 10 to it, it becomes ASCII 75,which is the letter K. Remember that spaces are also characters and should be encoded. Refer to an ASCII Character Code set to see a complete set of decimal/character values.

Secret Message: MEET ME AT 8
Each character M E E T M E A T 8
ASCII values 77 69 69 84 32 77 69 32 65 84 32 56
Key = 10, add 87 79 79 94 42 87 79 42 75 94 42 66
Encoded message: W O O ^ * W O * K ^ * B


In your main function, show your header, seed the rand function and begin a do while loop. Here is the flow:
• Call GetCodeInteger, which returns the int. The function gives the user the option to input a codeInteger or have it randomly determined by the program. If the user inputs an integer, the program checks that it is in the valid range. If not, display a message and loop back to input a new key.
• Ask the user whether he/she wants to Encode or Decode a secret message. Hint: To encode/decode you’ll need to access each character in the string, adjust it, and then either return the changed string or use a stringstream object to obtain the new string.
Encode,
Call AskForString, which returns the secret message to be encoded. Require that all letters be upper case, and that no punctuation be used.
Call Encoder, passing the codeInteger and secret message, and returning the encrypted message.
Display the message, the encrypted message, and the codeInteger, or encryption key.

Decode,
Call AskForEncryptedMessage, which returns the encrypted message to be decoded.
Call Decoded, passing the codeInteger and encrypted message, and returning the decoded message.
Display the encrypted message, the decoded message and the codeInteger, or encryption key.
• Ask the user if he/she wants to try another secret message
• When the user has finished, display a good-bye message"


my code is

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

using namespace std;
string doAnother;
string AskForString;
string Encoder(int key, string message);
string Decoder(int key, string message);
string AskForEncrypted; 
int GetCodeInteger, choice, ascChar;

int main()
{
	//Seed random 
	srand(time(NULL));

	do
	{
		//Ask user if they have a code integer or want program to determine one for them
		cout << "Enter your code integer 1-35 or enter 0 to have program determine a random one: ";
		cin >> GetCodeInteger; 

		//if out of range
		if (GetCodeInteger < 0 || GetCodeInteger > 35)
		{
			cout << "Out of range!" << endl;
			
		}

	
		else
		{
			//give user random number
			if (GetCodeInteger == 0)
			{
			GetCodeInteger = rand() % 35 + 1;
			cout << "Your given code integer is: " << GetCodeInteger << endl;
			}

			//Ask if user wants to encode or decode
			cout << "Would you like to Encode or Decode? 1 for Encode or 2 for Decode: ";
			cin >> choice;
                        cin.ignore();

			//Encode
			if (choice == 1)
			{
				cout << "Please enter your message, all upper case & no punctuation: ";
				getline(cin, AskForString);
                                for (int i = 0; i < AskForString.length(); i++)
				{
					ascChar = AskForString[i];
					cout << ascChar << endl;
				}

			}


			//Decode
			else if (choice == 2)
			{

			}
		}
			// loop back if do another
			cout << "Would you like to do another one? yes/no: ";
			cin >> doAnother;
		
	} while (doAnother == "yes");



	cout << "Goodbye!" << endl;

	return 0;
}
Last edited on
Hello,

After I added two includes, the provided code runs and when you choose encrypt it outputs the ascii-values of the entered text.

What exactly is the problem you encounter?

Also did you consider:
1. It is impossible to decrypt if GetCodeInteger contains a random generated value. I think you should change that order.
2. The template that you received implies that the encryption and decryption should be done in separate functions.

1
2
#include <time.h>   // Added missing include
#include <cstdlib>  // Added missing include 


Kind regards, Nico
Hello miguelminaj,

Adding to what Nico started with.

For "<time.h>" you should use the C+++ "<ctime>".

For "srand" I have found this to work the best: srand(static_cast<unsigned int>(time(nullptr)));

You have this:
1
2
3
4
5
6
7
string doAnother;
string AskForString;
string Encoder(int key, string message);
string Decoder(int key, string message);
string AskForEncrypted;

int GetCodeInteger, choice, ascChar;

I am not sure if lines 1, 2, 4 and 5 are unfinished prototypes or global variables. I do see that line 7 is a global variable. Try to avoid global variables as any line of code that follows can change them then you spend a lot of time trying to track down where it went wrong. Also I get the idea that variables should be passed to functions which means that need to be defined in "main".

Lines 20 and 21 need to be in a function not part of "main".

The best advice I can give you is work in small steps.I would comment lines 23 - 64 and work on the first function until it returns what you need.

Work on this part first:

• Call GetCodeInteger, which returns the int. The function gives the user the option to input a codeInteger or
have it randomly determined by the program. If the user inputs an integer, the program checks that it is in the
valid range. If not, display a message and loop back to input a new key.

• Ask the user whether he/she wants to Encode or Decode a secret message. Hint: To encode/decode you’ll need to
access each character in the string, adjust it, and then either return the changed string or use a stringstream
object to obtain the new string.

Once you have that part working the rest will be easier to code.

Andy
Consider:

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

using namespace std;

string Encoder(int key, const string& message)
{
	string enc;

	for (const auto& ch : message)
		enc += static_cast<char>(ch + key);

	return enc;
}

string Decoder(int key, const string& message)
{
	string dec;

	for (const auto& ch : message)
		dec += static_cast<char>(ch - key);

	return dec;
}

int GetInteger(const std::string& prm, int minv, int maxv)
{
	do {
		std::cout << prm << " (" << minv << " - " << maxv << "): ";

		int i {};

		if ((std::cin >> i) && (i >= minv) && (i <= maxv) && (std::cin.peek() == '\n')) {
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
			return i;
		}

		if (!std::cin || (std::cin.peek() != '\n'))
			std::cout << "Not an integer\n";
		else
			std::cout << "Input not in range\n";

		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	} while (true);
}

string AskForString()
{
	string text, mess;

	cout << "Please enter your message to encrypt, all upper case & no punctuation: ";
	getline(cin, text);

	for (const auto& t : text)
		if (isupper(t) || isspace(t) || isdigit(t))
			mess += t;
		else
			cout << "Invalid character '" << t << "'. Ignored\n";

	return mess;
}

string AskForEncryptedMessage()
{
	string text;

	cout << "Please enter your message to decrypt: ";
	getline(cin, text);

	return text;
}

int main()
{
	srand(static_cast<unsigned int>(time(nullptr)));

	do {
		auto code {GetInteger("Enter your code integer 1 - 35 or enter 0 to have program determine a random one: ", 0, 35)};

		if (code == 0) {
			code = rand() % 35 + 1;
			cout << "Your given code integer is: " << code << '\n';
		}

		const auto choice {GetInteger("Would you like to Encode (1) or Decode (2)", 1, 2)};

		if (choice == 1) {
			const string text {AskForString()};

			cout << "The original message: " << text << '\n';
			cout << "The encoded message is: " << Encoder(code, text) << '\n';
			cout << "The encryption key used is: " << code << '\n';
		} else {
			const string text {AskForEncryptedMessage()};

			cout << "The encrypted message is: " << text << '\n';
			cout << "The decoded message is: " << Decoder(code, text) << '\n';
			cout << "The decryption key used is: " << code << '\n';
		}

	} while (GetInteger("\nWould you like to do another one (1 - yes, 0 - no)", 0, 1));

	cout << "Goodbye!\n";
}



Enter your code integer 1 - 35 or enter 0 to have program determine a random one:  (0 - 35): 10
Would you like to Encode (1) or Decode (2) (1 - 2): 1
Please enter your message to encrypt, all upper case & no punctuation: MEET ME AT 8
The original message: MEET ME AT 8
The encoded message is: WOO^*WO*K^*B
The encryption key used is: 10

Would you like to do another one (1 - yes, 0 - no) (0 - 1): 1
Enter your code integer 1 - 35 or enter 0 to have program determine a random one:  (0 - 35): 10
Would you like to Encode (1) or Decode (2) (1 - 2): 2
Please enter your message to decrypt: WOO^*WO*K^*B
The encrypted message is: WOO^*WO*K^*B
The decoded message is: MEET ME AT 8
The decryption key used is: 10

Would you like to do another one (1 - yes, 0 - no) (0 - 1): 0
Goodbye!

Topic archived. No new replies allowed.