First off there is a header file that goes with this source code so i'll post that first.
--------------------------------------------------------------------------
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
|
#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
|
--------------------------------------------------------------------------
Now for the output for the source code is supposed to look as follows:
Enter a sentence: Computer Science
In reverse order: ecneicS retupmoC
CONTINUE(y/n)? y
Enter a sentence: A Toyota
In reverse order: atoyoT A
My Source code
--------------------------------------------------------------------------
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
|
#include <iostream>
#include <ctime>
#include "STACK.h"
using namespace std;
int main()
{
STACK<char, 80> sent,temp;
char c,x;
time_t a;
time(&a);
cout<<"Today is: "<<ctime(&a)<<endl;
sent.MakeStack();
temp.MakeStack();
cout<<"Enter a sentence: ";
while (cin.get(c), c != '\n')
{
sent.PushStack(c);
}
cout<<"In reverse order: ";
while (! sent.EmptyStack())
{
c = sent.PopStack();
cout<<c;
}
cout<<endl;
return 0;
}
|
--------------------------------------------------------------------------
I've tried a do while loop, a while loop, if else. The same problems seems to be happening. After I enter the y to continue it will skip over the sentence input from the user and prints the "In reverse order:" on the same line as "Enter a sentence:" then it goes straight back to the continue question.