Problem with Stack Requirements

Does anyone else think the requirements and example outcome of the assignment below make absolutely no sense? I don't see how the example output can be achieved by coding based on the assignments requirements.


The goal of the program is to encode input messages using the following encryption strategy:

The message sender inputs a four letter word, C C C C , and another four letter word, X X X X . The message sender then inputs the message to be sent. The program scans the message one character at a time and each character is pushed in the stack until either the scanned character is in the word C C C C or the end of message is encountered. When the scanned character is one of the characters in C C C C , print that character and continue to print and pop the characters at the top of the stack until either the stack is empty or the character at the top of the stack is one of the characters in X X X X . When the end of the message is encountered, print the character at the top of the stack and continue to pop and print the stack top until the stack is empty.

Well, all I can say to you is "GOOD" "LUCK", it "SOUNDS SIMPLE TO ME", or as your program would say, "OSDNOT EEM LPMIS SU".
closed account (z05DSL3A)
First letter S not in "GOOD" push onto stack
Second letter O is in "GOOD" print the letter then pop the stack, S is not in "LUCK"so you can pop and print. (Current output= OS).
Third letter U not in "GOOD" push onto stack.
Forth letter N not in "GOOD" push onto stack.
Fith letter D is in "GOOD" print the letter then pop the stack (Current output= OSD).
From the stack N is not in "LUCK"so you can pop and print. (Current output= OSDN).
From the stack U is in "LUCK" so leave it there and go back to your input
....
That's an interesting scheme. Instead of changing the values of the input, it changes its order.
My guess is the decryption algorithm is the same, but with the words swapped.
The requirements seem quite reasonable to me, assuming "the stack" isn't the system stack. std::stack<char> would work well for such a need (or maybe the point of the assignment is to get you thinking about how to make a stack efficient).
closed account (z05DSL3A)
maybe the point of the assignment is to get you thinking about how to make a stack efficient

I was thinking the OP is learning about State Machines.
@Grey Wolf, your example helped, thanks. Here is what I have so far, but I cannot figure out why there is an extra space thrown into the output. It seems to occur when I start popping the stack through a for loop. Does anyone see the problem?

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
// stack.h

// stack class implementation provides push and pop functions

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;

class Stack
{
private:
	char *stackArray;
	int stackSize;
	int top;

public:
	int getTop();
	Stack( int );
	~Stack()
	{
		delete[]stackArray;
	}

	void push( char );
	char pop();
	bool isFull();
	bool isEmpty();
}; // end of Stack class

// constructor
Stack::Stack( int size )
{
	stackArray = new char[size];
	stackSize = size;
	top = -1;
}

// function to push integer onto stack
void Stack::push( char letter )
{
	if ( !isFull() )
	{
		top++;
		stackArray[top] = letter;
	}
	else
	{
		//cout << "The stack is full." << endl;
	}
}

// function to pop the top value off the stack and copies it into the variables pass as an argument
char Stack::pop()
{
	char letter;
	if ( !isEmpty() )
	{
		letter = stackArray[top];
		top--;
		cout << letter;
		return letter;
	}
	else
	{
		//cout << "Stack is empty." << endl;
	}
	//return letter;
}

// check to see if stack is full
bool Stack::isFull()
{
	if ( top == stackSize - 1 )
		return true;
	else
		return false;
}

// check to see if stack is empty
bool Stack::isEmpty()
{
	if ( top == -1 )
		return true;
	else
		return false;
}

#endif 



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

// Program Description

#include <iostream>
#include <string>
using namespace std;

#include "stack.h"

int main()
{
	// local (automatic) variable declerations
	string word1;	// 4 letter word
	string word2;	// 4 letter word
	string message;	// message
	int i = -1;		// index counter
	int m = 1;
	bool word1MatchFound = false;
	bool word2MatchFound = false;
	
	do // prompt for user input for word1
	{
		cout << "Enter the first four letter word: ";
		getline( cin, word1 );

		if ( word1.length() < 4 || word1.length() > 4 )
		{
			cout << "The word must be exactly 4 characters in length.  Please reenter." << endl;
		}
	} while ( word1.length() < 4 || word1.length() > 4 );
	
	do // prompt for user input for word2
	{
		cout << "Enter the second four letter word: ";
		getline( cin, word2 );

		if ( word2.length() < 4 || word2.length() > 4 )
		{
			cout << "The word must be exactly 4 characters in length.  Please reenter." << endl;
		}
	} while ( word2.length() < 4 || word2.length() > 4 );

	// prompt for message entry
	cout << "Enter a message to encrypt: ";
	getline( cin, message );
	
	Stack myStack( message.length() );

	do // main program loop
	{
		i++; // increment i to advance forward and process new character
		word1MatchFound = false;
		word2MatchFound = false;

		for ( int j = 0; j < word1.length(); j++ )
		{
			// if the letter is found in word1, flag word1MatchFound = true and break out of for loop
			if ( message[i] == word1[j] )
			{
				word1MatchFound = true;
				break;
			}
		}
		
		// if the is not found in word1, push char to stack and increment i to process next letter
		if ( word1MatchFound == false )
		{
			myStack.push( message[i] );
		}
		else // if word1MatchFound == true
		{
			cout << message[i];
			m = i; // problem here if m = 0
			while( word2MatchFound == false && m > 0 )
			{
				// if letter is found in word2, set word2MatchFound = true and break out of loop
				m--;
				//word2MatchFound = false;
				for ( int k = 0; k < word2.length(); k++ )
				{
					if ( message[m] == word2[k] )
					{
						word2MatchFound = true;
						break;
					}
				}

				if ( word2MatchFound == false )
				{
					myStack.pop();
				}
			} 
		}
	} while( i < message.length() );
	
	for ( int count = 0; count < message.length(); count++ )
	{
		myStack.pop();
	}

	system("pause");
	return 0;
}
closed account (z05DSL3A)
It looks like the last charecter you are pushing on to the stak is the null character, this will be the first poped (cout will represent the null char with a space).
How do I stop that from happening?

Edit: Ok so I looked at this more and ran through every iteration of the program loops and cannot find how or where I am pushing the null character onto the stack. Even if i never popped the stack, I'm only pushing on characters up to the length of the string, which is 19. This error is driving me nuts.
Last edited on
closed account (z05DSL3A)
Check the value of the char before pushing it on to the stack, if it equals '\0' don't push.
Thank you Grey Wolf, that did the trick! I'm curious though, any idea at what point in the program I pushed in the '\0'?
Last edited on
Topic archived. No new replies allowed.