0

just my practice. //read a sentence with "()" in it and replace everything in () with "(deleted)".

1)read the sentence to a vector.

2) stack the vector into a stack >.
3) find the positions of both "(" and ")"
4) remove the elements and replace with "(deleted)" in vector
5) output the vector.

ie. input "this is (a) duck", output should be "this is (deleted) duck". thanks in advance.

#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main ()
{
    cout<<"please enter a series of words with parenthes"<<endl;
    vector<string> svec;
    string word;
    while (cin>>word)
        svec.push_back(word);
    stack<string, vector<string> > strStack(svec);
    vector<string>::iterator pos1 ,pos2;
    for (vector<string>::iterator counter=svec.end(); strStack.empty()==false; --counter)
    {
        if(strStack.top()==")") pos1=counter-1;
        if(strStack.top()=="(") { pos2=counter-1; break; }
        strStack.pop();     
    }
    svec.insert(svec.erase(pos2,pos1),"(deleted)");
    for(vector<string>::iterator iter=svec.begin(); iter!=svec.end(); )
        cout<<*iter++<<" "<<flush;
return 0;
}

sorry. output is: I think the problem is stack > strStack(svec); cause I couldn't cout stack; the codes above this should be fine (i cout'ed them);

please enter a series of words with parenthes
this is (a) duck
Segmentation fault
5
  • 2
    You're using counter to move from svec.end() to...well, beyond the beginning of svec. Use rbegin() and stop iterating when you hit rend(). Commented Nov 11, 2011 at 16:32
  • @w00te: he said something about seg fault Commented Nov 11, 2011 at 16:38
  • I just updated the output @w00te Commented Nov 11, 2011 at 16:39
  • My bad, I didn't read thoroughly enough - appologies and +1 :) Commented Nov 11, 2011 at 16:46
  • What do you discover when you run the program inside a debugger? What line raises the segmentation fault? Commented Nov 11, 2011 at 16:54

1 Answer 1

2

Your code is simply broken: It only handles some very specific cases, namely when there are single words consisting of only a parentheses. Failing that, you never assign pos1 and pos2, leading to our beloved "undefined behaviour".

At the very least, make sure you never use uninitialized variables (#1). As a bonus, we check if there was a match (#2):

vector<string>::iterator pos1 = svec.end(), pos2 = pos1; // #1: never uninitialized

// ...

if (pos1 != pos2)   // #2: only if we found something
    svec.insert(svec.erase(pos2,pos1),"(deleted)");
Sign up to request clarification or add additional context in comments.

3 Comments

I just realized that I need to use stack<char> instead of <string>. thanks. I am waiting for #2. Even an advice on code format would help.
@ihm: Perhaps, though you'll have to modify the insert statement accordingly.
I think I need to modify every statement that has pos1 and pos2 in it. since they are most likely uninitialized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.