C++, using stack.h read a string, then display it in reverse

Posted by user1675108 on Stack Overflow See other posts from Stack Overflow or by user1675108
Published on 2012-09-16T02:39:44Z Indexed on 2012/09/16 3:38 UTC
Read the original article Hit count: 319

Filed under:
|
|
|

For my current assignment, I have to use the following header file,

#ifndef STACK_H
#define STACK_H

template <class T, int n>
class STACK
{
  private:
    T a[n];
    int counter;

  public:
    void MakeStack() {
        counter = 0;
    }

    bool FullStack() {
        return (counter == n) ? true : false ;
    }

    bool EmptyStack() {
        return (counter == 0) ? true : false ;
    }

    void PushStack(T x) {
        a[counter] = x;
        counter++;
    }

    T PopStack() {
        counter--;
        return a[counter];
    }
};

#endif

To write a program that will take a sentence, store it into the "stack", and then display it in reverse, and I have to allow the user to repeat this process as much as they want. The thing is, I am NOT allowed to use arrays (otherwise I wouldn't need help with this), and am finding myself stumped.

To give an idea of what I am attempting, here is my code as of posting, which obviously does not work fully but is simply meant to give an idea of the assignment.

#include <iostream>
#include <cstring>
#include <ctime>
#include "STACK.h"
using namespace std;

int main(void)
{
    auto time_t a;
    auto STACK<char, 256> s;
    auto string curStr;
    auto int i;

    // Displays the current time and date
    time(&a);
    cout << "Today is " << ctime(&a) << endl;

    s.MakeStack();
    cin >> curStr;
    i = 0;
    do
    {
        s.PushStack(curStr[i]);
        i++;
    } while (s.FullStack() == false);

    do
    {
        cout << s.PopStack();
    } while (s.EmptyStack() == false);

    return 0;
} // end of "main"

**UPDATE This is my code currently

#include   <iostream>
#include   <string>
#include   <ctime>
#include   "STACK.h"
using namespace std;

time_t          a;
STACK<char, 256>    s;
string          curStr;
int         i;
int         n;

// Displays the current time and date
time(&a);
cout << "Today is " << ctime(&a) << endl;

s.MakeStack();
getline(cin, curStr);
i = 0;
n = curStr.size();
do
{
s.PushStack(curStr[i++]);
i++;
}while(i < n);

do
{
cout << s.PopStack();
}while( !(s.EmptyStack()) );    

return 0;

© Stack Overflow or respective owner

Related posts about c++

Related posts about string