//I am trying to use the reverse function but only get garbage when trying to print s2. How do I properly assign s2 a value in order to print it in main?

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

class ch_stack {
public:
void reset() { top = EMPTY; }
void push(char c) { s[++top] = c; }
char pop() { return s[top--]; }
char top_of() const { return s[top]; }
bool empty() const { return (top == EMPTY); }
bool full() const { return (top == FULL); }

private:
enum { max_len = 100, EMPTY = -1, FULL = max_len - 1 };
char s[max_len];
int top;
};


int main()
{
void reverse(const char s1[], char s2[]);

char s1[40] = { "Reverse Me" };
char s2[40];

cout << "Original string: " << s1 << endl;
reverse(s1, s2);
cout << "\nReversed string: " << s2;
cout << endl;

system ("PAUSE");

}

void reverse(const char s1[], char s2[])
{
ch_stack s;
int i = 0;

s.reset();
if (s1 != '\0')
{
while (s1 && !s.full())
s.push(s1[i++]);
while (!s.empty())
s.pop();
}
}

so if I assign [s2 = s.pop();] the compiler gives me an error "invalid conversion from 'char' to 'char'

you would need to reset i to zero and then assign the return to s2 and increment i after each time.

Adding a '\0' to string s2 and revising the while loop to:

while (!s.empty())
      for(int i = 0; s1[i] != '\0'; i++)
      s2[i] = s.pop();
      return s2[i];

displays proper output. Thank you very much!

This question has already been answered. Start a new discussion instead.