Deleting elements in a stack(C++)

Hi! I wanted to delete data in a stack if the student code is the particular code. First I copied object student to temp. Then I emptied the object student

Main class:
studentStack temp;
temp.copyStack(student);
student.initializeStack();

while (!temp.isEmptyStack()){
if (!temp.checkStudentCode(code)) //return true if it is the particular code
student.push(temp.top());

temp.pop();
}



class studentStack:

bool studentStack::checkStudentCode (string code){
if (current->data.getCode () == code)
return true;
return false;
}

I have error running this program. Can anyone tell me why and how should I do instead?
What error do you have running this program?
The program just stopped working and closed by windows
There isn't enough here to tell. Please post a program that we can compile and run, along with the input that causes the error.

BTW, I wouldn't have stidentStack::checkStudentCode() at all. Instead use top() to access the current element. Something like:
1
2
3
if (temp.top().getCode() == code) {
    student.push(temp.top());
}


First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/


We cannot see entire compilable program and therefore we cannot see the possible errors. For example, we see only one member of the class studentStack

Why do you have class studentStack at all. Why not std::stack?


Note that
1
2
3
4
5
6
bool studentStack::checkStudentCode (string code){
  if (current->data.getCode() == code)
    return true;

  return false;
}

can be reduced to
1
2
3
bool studentStack::checkStudentCode (string code){
  return (current->data.getCode() == code);
}


However, is that member really necessary? Is the studentStack::current->data same value that is returned by the studentStack::top()? If yes, then
1
2
3
4
5
6
while ( !temp.isEmptyStack() ) {
  if ( !temp.checkStudentCode(code) )
    student.push( temp.top() );

  temp.pop();
}

could be written
1
2
3
4
5
6
7
while ( !temp.isEmptyStack() ) {
  auto stud = temp.top();
  if ( stud.getCode() != code )
    student.push( stud );

  temp.pop();
}
Topic archived. No new replies allowed.