Thread: Help with HTML tag checking

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    Help with HTML tag checking

    I have to write a program to check for balanced tags in a HTML code. I have written the code below so far but it is not working right it was brought to my attention that my coding is wrong and that it needs to be fixed.
    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);
    }
    This is the part where my code is wrong I was told that you can not do string find the way i have it done here. What i want it to do but cant seem to figure out is when it sees <...> pushp and then when it sees a </...> pop. So i need help on the right way to write this portion of code. Thanks in advance.






    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    #define MAX 300
    
                                        //My stack Structure that i used i can handle integers. 
    
    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 ()
    {
                                               //File input  and string declaring
        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 declaring
     STACK stack;
      stack.Top = 0;
      
      
      pos1= 0;
      pos = 0; 
                                                      // This below code is were my problem is I understand
                                                      // That you cant use find the way i am doing it and
                                                      // would like some help with the right way.
    
      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);
    }
    
    
     
                                                           //Checking if stack is full or empty. If stack is empty                                                  
                                                           // The code is balanced otherwise it is not.
     if (stack.Top == 0)
     { 
                cout << "Legal" << endl;
                }
       else
       {
             cout << "Illegal" << endl;
    
    }
    system("pause>nul");
      return 0;
    }
    Thanks again for any help in advance

  2. #2
    Registered User blackcoder41's Avatar
    Join Date
    Jan 2010
    Location
    Philippines
    Posts
    14

    Wink

    Code:
    "<" && ">"
    I think that will return a boolean which is always true of course. When you pass that to string::find, I think it will be casted to char. Then your if statement evaluates to true since every value except zero is true including string::npos.

    So basically your program will always evaluate to legal. Hope that helps

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Yes, that helps alot any suggestions on a better way write that part of the code would be appreciated and also thanks again for the help.

    I will try to think of a better way to write as well, but any help would be good too.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. HTML parser that crashes with longer strings
    By almos in forum C Programming
    Replies: 3
    Last Post: 03-05-2010, 09:04 AM
  3. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  4. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM