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;
}

Recommended Answers

All 11 Replies

And what is if (pos=str.find("<" && ">",pos+1)) supposed to do?

You said "My problem is it is not working right" which is fine but tells us absolutely nothing. So I waited with bated breath for what it was doing wrong. All I got was "it seems to be here". Please, when asking for help, also explain the problem.

Ok sorry if (pos=str.find("<" && ">",pos+1)) is supposed to find where a tag is in the string i.e. <...> like a html tag but when i run the program nothing happens.

So my question would be are you able to do if (pos=str.find("<" && ">",pos+1)) the way i have it done? and if you are not what would be a better way to do this?

Find the '<', then find the '>'. Extract (copy) the substring between them.

Quick question, What is the data that you are trying to store in the stack ? 1's or the content b/w the < .... > tags ?

If you going to store just 1's then your program will break for

<html>
  <a>
       My text
  </b>
</xml>

Storing the content b/w the < .., > tags will require you to modify your data structure

Hey abhimanipal not trying to store the content between the < ... > just trying to make it push for < ... > and pop for </ ... > when ever come across one of them when it reads through the sting. Then at the end of the program if the stack.Top == 0 meaning there are a correct number of each tags it will print the result. But it is not working correctly.

thanks in advance for the help.

What is the error that you are getting ? Did you try to debug ?

Not getting a error it complies fine but as Waltp said my code is wrong because you cant do

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);
}

the way i have it written. Any suggestions on how to fix it would be appreciated.

What is the if statement supposed to do ?

The if statement us supposed to check if there are a set of tags like<> or </> and then push or pop like kit adds one to the stack if it see s <> and then it pop s one if it sees </>

Is the if statement working correctly ?

Print the results that you get after using the find . Also I see that you have a single = sign in the if block . Is this what you want ?

Not getting a error it complies fine but as Waltp said my code is wrong because you cant do...
[snip]
the way i have it written. Any suggestions on how to fix it would be appreciated.

This should help

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.