Well this is my assignment for uni. I've done all the main and difficult bits- and now I cannot believe that I'm stuck at the simplest task. Im told to make a menu that gives user three options to choose from. Choosing option 1 will ask to choose from 3 more options. What i want to do is when user choose option 1 of 1...then call the function twostacks(). But instead of asking for input...the console just prints out "Is palindrome". The function does its job if I just simply call it at the first line in my main function. Heres the main function code:

void twostacks()
{
    string s;
    StackType<char> stack1(40);
    StackType<char> stack2(40);
    char c1;
    int count=0;
    cout<<"enter string: press return."<<endl;
    getline(cin,s);

    while(!s.empty())
    {
      if(isalpha(s.at(0)))
      {
         stack1.Push(tolower(s.at(0)));
         s.erase(0,1);
         count++;
      }

      else if(!isalpha(s.at(0)))
      {
         s.erase(0,1);
      }
    }

    int a=count/2;
    for(int i=0; i<a; i++)
    {
        c1=stack1.Top();
        stack2.Push(c1);
        stack1.Pop();
    }

    if((count%2)==0)
    {
        while(!stack1.IsEmpty() && !stack2.IsEmpty())
        {
            if(stack1.Top()==stack2.Top())
              {
                  stack1.Pop();
                  stack2.Pop();
              }
            else break;
        }
    }
    else if((count%2)!=0)
    {
        stack1.Pop();
        while(!stack1.IsEmpty() && !stack2.IsEmpty())
        {
            if(stack1.Top()==stack2.Top())
              {
                  stack1.Pop();
                  stack2.Pop();
              }
            else break;
        }
    }

    if(stack1.IsEmpty() && stack2.IsEmpty())
      cout<<"Is Palindrome"<<endl;
    else
      cout<<"Not Palindrome"<<endl;

}

int main()
{

    cout<<"Choose your operation:\n"<<"[1]Palindrome\n"<<"[2]Match Parenthesis\n"<<"[3]Evaluate postfix expressions\n";
    cout<<endl<<endl;
    int c,p;
    cin>>c;

    if(c==1)
    {
        cout<<"Choose one of the following strategies:\n";
        cout<<"(1)Using two stacks\n";
        cout<<"(2)Using one stack and one queue\n";
        cout<<"(3)Using one stack\n";
        cin>>p;

        if(p==1)
        {
            twostacks();
        }

    }
    return 0;
}

Recommended Answers

All 2 Replies

line 81. After you enter a number and press <Enter> key, cin does not remove the enter key from the keyboard buffer. Then when getline() is executed at line 9 it sees '\n' in the keyboard buffer and removes it thinking that is the end of the text you just typed. To correct the problem, after entering an integer, float or double you have to remove the '\n' from the keyboard buffer. There are several ways to do it, the safest way is to flush the keyboard buffer of all keys. Read this thread about how to flush the keyboard buffer.
'

Thanks for your reply @ancientdragon. I've figured out the problem already after a lot of painful googling. You're absolutely right- the keyboard buffer needed to be flushed. So I tried a suggestion I read on another post and now using cin.ignore(25,'\n') before the function calls. Seems to do the trick!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.