Stack/Push/Pop help!

I have an assignment where I have to take a sentence and show the revere of it using a user defined library of stack.

The header file contains:
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
#ifndef STACKLIB
#define STACKLIB

template <class T, int n>
class STACK
{
private: T a[n]; int counter;
public:
	void ClearStack()
	{
		counter = 0; 
	}
	bool EmptyStack()
	{
		if (counter == 0)
		{
			return true;
		}
		else
			return false;
	}
	bool FullStack()
	{
		return (counter == n) ? true : false;
	}
	void PushStack(T x)
	{
		a[counter] = x;
		counter++;
	}
	T PopStack()
	{
		counter--;
		return a[counter]; 
	}
};
#endif  


I am running into a serious run-time error, where when I enter the sentence I wish to show in reverse, the compiler outputs the "Unexpected Exception" error.

Here is the main function:
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
#include "STACKLIB.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	STACK <string, 100> S;
	STACK <int, 50> N;

	string sent, revsent;
	int numb, base, r;

	cout << "Enter a sentence: " << endl;
	getline(cin, sent);

	S.PushStack(sent); //push the string into stack

	revsent = S.PopStack(); //pop stack starting from last entry

	cout << revsent;

	cout << "Enter an integer <100: ";
	cin >> numb; 
	cout << endl;
	cout << "Enter a new base (2,6,16):";
	cin >> base;
	cout << endl;

	while (numb != 0 && numb < 100)
	{
		r = numb%base;
		N.PushStack(r); //push the remainder

		numb = numb / base;
	}

	while (!N.EmptyStack()) // as long as stack is not empty
	{
		r = N.PopStack();

		if (r <= 9)
		{
			cout << r;
		}
		else
		{
			cout << char(r) + 55; //if r is greater than 9, output the char equivalent 
		}
	}


	system("PAUSE");
	return 0;

}
In line 7 of STAKLIB.h, counter is not initialized. It contains a very large junk value.
In line 18 of the .cpp , PushStack is called which calls line 26 of the .h.

In line 28 of .h, a[counter] is out of range.

You should initialize the counter.
Ah yes, I made the mistake of not clearing each stack.

One last problem:

When I enter a string to show in reverse, lets say I enter "happy" and wish it to be reversed.

my program successfully reverses it, but then enters a bunch of junk after, as the array size is too big and I cannot find a way to change it to a dynamic array.

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
#include "STACKLIB.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	STACK <char, 100> S;
	STACK <int, 50> N;


	char sent[30];
	int numb, base, r, size;


	cout << "Enter a sentence: " << endl;
	cin.getline(sent, 30);
	
	S.ClearStack();
	size = strlen(sent);

	

	for (int i = 0; i < size; i++)
	{
		S.PushStack(sent[i]); //push the string into stack
	}

	for (int i = 0; i < size + 1; i--)
	{
		sent[i] = S.PopStack();
		cout << sent[i];
	}
Append '\0' to the reversed string.
What do you mean? Can you clarify please
The null character is the difference between an array of characters and a string.
1
2
3
4
5
6
char array1[5] = {'a','l','e','x','\0'}; // a string. the null char ends a string.
char array2[5] = {'a','l','e','x'};  //simple an array of chars

cout<<array1; //prints all elements of array1 till it meats the "null/terminating" char.
cout<<array2; //prints "alex" followed by junk coz there is no "null/terminating" character.
                       //so it'll read junk till the end of the array. 


Assume the string to revers is "alex". After popping the string to sent.. sent now contains
sent[30] = {'x','e','l','a',..........}// ... is junk.
To prevent the program from pinting junk, add '\0' after the last character popped.
sent[30] = {'x','e','l','a','\0',..........}// ... is junk.
Now when the string is printed, it will end when it meets '\0'
I tried making a condition where if the char = a null then break the program, however it still inputs junk


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
cout << "Enter a sentence: " << endl;
	cin.getline(sent, 30);
	
	S.ClearStack();
	size = strlen(sent);

	

	for (int i = 0; i < size; i++)
	{
		S.PushStack(sent[i]); //push the string into stack
	}

	for (int i = 0; i < size + 1; i--)
	{
		sent[i] = S.PopStack();
		if (sent[i] == '\0')
		{
			break;
		}
		else
		cout << sent[i];
	}
I have figured it out, I used a while loop where the condition was while the stack is not empty, thanks shadow you've been a big help!
Topic archived. No new replies allowed.