Can anyone help me with this program?? Professor asked me to create a program that permits a user to enter a maximum of 50 characters into a stack Object, then have the program sort the stack contents into increasing order from A to Z.

Is there any way i can modify the program below to producethe program above?? I dont know much about stack. We are doing C# but he made us start using C++ due to difficulties with the C# program. The code is below

// "Stack of Integers" program
// Classwork #1
#include <iostream>
using namespace std;
const int MAXSTACK = 100;
const int TRUE = 1;
const int FALSE = 0;

// Class declaration
class Stack
{
private:
    int top;
    int num [MAXSTACK];
public:
    Stack(); // constructor
    void push(int);
    int pop();
    int isempty();
    int isfull();
};

// implementation section

Stack::Stack()
{
    top = -1; //initialize the top-of-stack position current position
}
void Stack::push(int value)
{
    top++; // increment the index in top
    num[top] = value; // store the value
}

int Stack::pop()
{
    int topval;
    topval = num[top]; // retrieve the top element
    top--; // decrement the index stored in top
    return (topval);
}

int Stack::isempty()
{
    if(top == -1)
        return TRUE;
    else 
        return FALSE;
}

int Stack::isfull()
{
    if (top == MAXSTACK -1)
        return TRUE;
    else
        return FALSE;
}

int main()
{
    Stack digits;
    int newnum;
    cout << "Enter as many digits as you wish, one per line" 
        << "\nTo stop entering digits, enter a number greater than 0 \n";

    while (1)
    {
        cout << "Enter a digit:";
        cin >> newnum;
        if (newnum > 0)
            break;
        if (digits.isfull()) // check overflow
        {
            cout << "\nNo more storage allocation space"
                << "\nThe last digits has not been entered on the stack" << endl;
            break;
        }
        else 
            digits.push (newnum); // push value onto the stack
    }
    // pop and display digits from the stack
    cout << "\nThe values popped from the stack are:\n";
    while(!digits.isempty()) // check the underflow
    {
        cout << digits.pop() << endl;
    }
    return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Sorting..

void Stack::Swap(int x,int y)
{
   int t=num[x];
   num[x]=num[y];
   num[y]=t;
}

void Stack::Sort()
{
   int s="size now";
   for(int i=s-1;i>0;i--)
      for(int j=0;j<i;j++)
         if(num[j]>num[(j)+1])
            Swap(j,(j)+1);
}
This article has been dead for over six months. Start a new discussion instead.