I have written a program that gets a string from the user and puts the characters on a stack, when the NULL terminator is reached it starts to pop the characters off the stack and display them therfore reversing the string. I am having one problem with this program though; whenever I run the program I get an error message that says: error, popping an empty stack. Here is the code:

#include <iostream.h>
#include "stack.h"

int main()
{
	stack <char> MyStack;
	char string[50], value;
	int counter = 0;

    cout << "Enter a word or name ";
	cin.get(string, 20);
	cin.ignore(80, '\n');

	do
	{
		MyStack.push(string[counter]);
		counter++;
	}while (string[counter] != NULL);
	
	if(string[counter] == NULL)
	{
		do
		{
	     value = MyStack.pop();
	     cout << value;
		 counter --;
		}while(string[counter] != -1);
	}
	else
	{
		cout << "an error occured\n";
	}
	return 0;
}

Shouldn't this:

}while(string[counter] != -1);

be this:

} while (counter != -1);

?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.