I have to write a program to check for balanced HTML tags here is what i have so far. My problem is it is not working right. My stack implementation is fine, i believe my problem is when i am looping through the string ( lines58-70 I marked with comments where i think the problem is any help would be greatly appreciated.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define MAX 300
typedef struct stack
{
int item[MAX];
int Top;
}STACK;
void push(STACK *ps, int x)
{
if (ps->Top == MAX) {
fputs("Error: stack overflow\n", stderr);
abort();
} else
ps->item[ps->Top++] = x;
}
int pop(STACK *ps)
{
if (ps->Top == 0){
fputs("Error: stack underflow\n", stderr);
abort();
} else
return ps->item[--ps->Top];
}
int main ()
{
size_t pos1;
size_t pos;
string str, tmp;
std::string filename;
cout <<"Please enter the name of the file" << endl;
cin >> filename;
ifstream myfile(filename.c_str());
while(getline(myfile, tmp))
{
str += tmp;
}
STACK stack;
stack.Top = 0;
pos1= 0;
pos = 0;
// I believe my problem is with the below code
while (pos != string::npos)
{
if (pos=str.find("<" && ">",pos+1))
push(&stack, 1);
}
while (pos1 != string::npos)
{
if (pos1=str.find("</" && ">",pos1+1))
pop(&stack);
}
if (stack.Top == 0)
{
cout << "Legal" << endl;
}
else
{
cout << "Illegal" << endl;
}
system("pause>nul");
return 0;
}