Problem with while loop and if.

Hi. I have a problem processing some while and if statements.

I have created two lists (nodes) called stack and postfix.
I have two important methods:
(1) getLastName() which returns the last name in the list and
(2) deleteLastNode() which deletes the last node in the list.

What I am trying to do is to get the name of the last node using
stack1=stack->getLastName() and then make a comparison such as
if (stack1 !="(" || stack1 !="EMPTY") or while (stack1 !="(" || stack1 !="EMPTY").

However, my program returns the word "EMPTY" into stack1, but the program is not recognising its value as "EMPTY" (as specified in the if and while statement) and loops forever! I would be grateful for any help!

Below is sample code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool flag10=false;
while (flag10==false)
{
     stack1=stack->getLastName();
     cout<<"Stack1 before processing if is "<<stack1<<endl;
     if (stack1 !="(" || stack1 !="EMPTY")
     {
	postfix->addTownAtEnd(stack1);
	stack->deleteLastNode();
	stack1=stack->getLastName();
	cout<<"In IF statement for STUCK1!"<<endl;
	cout<<"Stack1 is "<<stack1<<endl;
     }
     if (stack1 !="(" || stack1 !="EMPTY")
     {
 	flag10=false;
	cout<<"Stack1 is "<<stack1<<"In stuck1 - 1st check flag10 remains  false"<<endl;
	cout<<"In stuck1 - 1st check flag10 remains false"<<endl;
     }
     else
     {
	flag10=true;
	cout<<"In stuck1 - 1st check flag10 is now TRUE"<<endl;
     }
}//end of while loop 

I am assuming stack1 is of type char*. If it is of this type, then you cannot compare it to "EMPTY" because it won't be equal. You will be comparing pointer addresses and not the content pointed to by the pointers, which would be the text.

In order to compare the text, you can use strcmp (http://www.cplusplus.com/reference/clibrary/cstring/strcmp/) and related functions.
Last edited on
Topic archived. No new replies allowed.