Basically the user puts in a linear list like "1234" and a permutation such as "2431". My program works on the example above but my instructor pointed out that the program gives the wrong answer for the entry "1234" and "3241". I put in an extra check which should fix the program but the program doesnt execute properly with that check. i have it commented out below so it is easy to spot. can anyone help point out why it is behaving this way?
Side note: the "cout<<test...." lines where in for me to identify the problem areas.

#include <iostream>
#include <stack>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
    stack<string> permStack;
    string stackPerm;
    string origStackPerm;
    string stackNums;
    string permCheck;
    string operationString;
    int count=0;
    cout << "What is your linear list?:";
    cin>>stackNums;
    cout<<"What is your stack permutation?:";
    cin>>stackPerm;
    origStackPerm=stackPerm;
    cout<<endl<<endl;
    bool stackBool;
    bool done =false;
    while(done != true)
    {   
        if(stackNums.substr(0,1)==stackPerm.substr(0,1))
        {
            if(stackNums.length()==0)
            {
                stackNums="";
            }
            else
            {
                cout<<"test =="<<endl;
                permCheck+=stackNums.substr(0,1);
                count+=1;
                if(permStack.top()==stackPerm.substr(0,1))
                {
                    cout<<"*stack test*"<<endl;
                    permCheck+=permStack.top();
                    permStack.pop();
                    count+=1;
                }
                if(stackNums.length()==1)
                {
                    operationString+="push("+stackNums.substr(0,1)+"), pop(), ";
                    stackNums="";
                }
                else
                {
                    operationString+="push(" + stackNums.substr(0,1)+ "), pop(), ";
                    stackNums=stackNums.substr(1);
                }
                if(stackPerm.length()==1)
                {
                    stackPerm="";
                }
                else
                {
                    stackPerm=stackPerm.substr(1);
                }
            }
        }
        if(stackNums.substr(0,1)!=stackPerm.substr(0,1))
        {
            cout<<"test != 1"<<endl;
            /**if(permStack.top()==stackPerm.substr(0,1))
            {
                cout<<"*stack test*"<<endl;
                permCheck+=permStack.top();
                permStack.pop();
                count+=1;
            }*/
            if(stackNums.length()==0)
            {
                cout<<"test != 2"<<endl;
                stackNums="";
            }
            else if(stackNums.length()==1)
            {
                cout<<"test != 3"<<endl;
                permStack.push(stackNums.substr(0,1));
                stackNums="";
                operationString+="push("+stackNums.substr(0,1)+"), ";
            }
            else
            {
                cout<<"test != 4"<<endl;
                permStack.push(stackNums.substr(0,1));
                operationString+="push("+stackNums.substr(0,1)+"), ";
                stackNums=stackNums.substr(1);
            }
        }
        if(stackNums=="")
        {
            for(int i= 0; i < permStack.size()+1; i++)
            {   cout<<"Test pop"<<endl;
                permCheck += permStack.top();
                permStack.pop();
                operationString+="pop(), ";
            }
            cout<<permCheck<<endl<<origStackPerm;
            if(permCheck==origStackPerm)
            {
                stackBool = true;
            }
            else
            {
                stackBool = false;
            }
            done = true;
        }
    }
    if(stackBool==true)
    {
        cout<<"The permutation is a stack permutation."<<endl<<endl;
        cout<<"The operation sequence is: "<<endl<<operationString<<endl<<endl;
    }
    else
    {
        cout<<"The permutation is not a stack permutation."<<endl<<endl;
    }
    return 0;
}

Thanks for those who looked lol. I figured it out. the program had a problem with me comparing an empty stack to a string. i just added a second condition to this if statement.

if(permStack.top()==stackPerm.substr(0,1))
            {
                cout<<"*stack test*"<<endl;
                permCheck+=permStack.top();
                permStack.pop();
                stackPerm=stackPerm.substr(1);
                count+=1;
            }

and changed it into this.

if(!permStack.empty()&&permStack.top()==stackPerm.substr(0,1))
            {
                cout<<"*stack test*"<<endl;
                permCheck+=permStack.top();
                permStack.pop();
                stackPerm=stackPerm.substr(1);
                count+=1;
            }
This article has been dead for over six months. Start a new discussion instead.