#include<iostream>
#include<cstring>
#include<stack>
usingnamespace std;
void GetData(char key1[5], char key2[5], char phrase[100])
{
cout << "Please enter the first four-character key: ";
cin.getline(key1, 5);
cout << "\nPlease enter the second four-character key: ";
cin.getline(key2, 5);
cout << "\nPlease enter the phrase you would like to encrypt: ";
cin.getline(phrase, 100);
return;
}
int main()
{
char key1[5],
key2[5],
phraseIn[100];
stack<char> myStack;
GetData(key1, key2, phraseIn);
cout << "\n" << key1 << "\t" << key2 << "\t" << phraseIn << endl;
cout << "\nThe encrypted message is: ";
for(int i = 0; i < strlen(phraseIn); i++)
{
myStack.push(phraseIn[i]);
if((myStack.top() == key1[0]) || (myStack.top() == key1[1]) ||
(myStack.top() == key1[2]) || (myStack.top() == key1[3]))
{
while((myStack.top() != key2[0]) && (myStack.top() != key2[1]) &&
(myStack.top() != key2[2]) && (myStack.top() != key2[3]) && (!myStack.empty()))
{
cout << myStack.top();
myStack.pop();
}
}
}
while(!myStack.empty())
{
cout << myStack.top();
myStack.pop();
}
return 0;
}
Please enter the first four-character key: GOOD
Please enter the second four-character key: LUCK
Please enter the phrase you would like to encrypt: SOUNDS SIMPLE TO ME
Segmentation fault
I just took at look at yours we had the exact same thing except I had an extra pair of parenthesis. I am assuming it was causing some sort of short-circuiting.
It is working now though. Thanks for all of your help.