I got a job for my C ++ class last week. I think some of you will find this interesting! I managed to get most of the code, but I got stuck and can't figure it out for me to live ... The following are the guidelines for the encryption process that I have to enter into the code:
The sender of the message enters the four-letter word, UDP and another alphabetic word, XXXX .
Then the sender of the message enters the message for encryption.
The program scans one char message at a time, and each char is pushed onto the stack until either the scanned character is in the word UDP or the end of the message is encountered.
When the scanned character is one of the characters in the UDP , print char and continue to print and embroider the characters at the top of the stack until the stack is empty or the char at the top of the stack is one of the characters in XXXX . When the end of the message occurs, print a character at the top of the stack and continue pop and print from the top of the stack until the stack is empty.
Here's a tip: " GOOD " " LUCK ", this is " SOUNDS SIMPLY TO ME ", or in the form your program will say: " OSDNOT EEM LPMIS SU "
So this is the actual destination.
I am having problems with the last bit:
When the end of the message occurs, print a character at the top of the stack and continue pop and print from the top of the stack until the stack is empty.
now here is the code i have so far:
#include <string> #include <iostream> using namespace std; class Stack { private: char Chars[50]; int top; public: int push(char); char pop(); bool isEmpty(); bool isFull(); Stack() { top = 0; } }; int main() { Stack theStack; char word1[4]; char word2[4]; for(int i=0; i < 4; i++){ word1[i] = ' '; word2[i] = ' '; } char message[500]; cout << "Please enter a 4 letter word: "; cin >> word1; while(word1[4] || !word1[3]) { cout << "Word must be 4 chars long. Try again: "; cin >> word1; } cout << "Please enter another 4 letter word: "; cin >> word2; while(word2[4] || !word2[3]) { cout << "Word must be 4 chars long. Try again: "; cin >> word2; } cout << "Please enter the phrase to be encrypted (50 chars max): "; cin.ignore(1000, '\n'); cin.getline(message,500); int length = strlen(message); int count = 0; char finalMsg[length]; //scanner for(int i = 0; i < length; i++) { if(message[i] == word1[0] || message[i] == word1[1] || message[i] == word1[2] || message[i] == word1[3]) { finalMsg[count] = message[i]; count++; if(message[i-1] != word2[0] || message[i-1] != word2[1] || message[i-1] != word2[2] || message[i-1] != word2[3]) { finalMsg[count] = message[i-1]; count++; } } else { theStack.push(message[i]); } } cout << finalMsg << endl; return 0; } int Stack::push(char data) { Chars[top] = data; top++; return top; } char Stack::pop() { char ret = Chars[top-1]; top--; return ret; } bool Stack::isEmpty() { if(top <= 0) return true; else return false; } bool Stack::isFull() { if(top >= 50) return true; else return false; }
When compiling, the final output gives me " OSDNOT ", which is given in the example provided by my professor, so I know that I'm heading to the right track .. Any help would be great, I donβt even know where to start learning the code.
source share