Hi,

I'm new to C++. plzz help me with this error.

#include <cstdlib>
#include <iostream>


#define clauselength 85
//we will use index number to find out the clause number.
//there are 88 clauses. as we start using index from 1 we have to add one to 89.

using namespace std;
string clause_variable_list[clauselength]=
{  
    "","ATTACK_FOUND","","","",
       "IP_SPOOFING_ATTACK","","","",
       "IS_SPOOFING_REFERS","","","",
       "IS_CALLER_ID_ATTACKS","","","",
       "IS_EMAIL_ADDRESS_REDIRECTING","","","",
       "SNIFFING_ATTACK","","","",
       "MATCH_IDENTIFICATION","","","",
       "ENCRYPT_EMAILS","","","",
       "MALWARE_ATTACK","","","",
       "IS_ANTIVIRUS_RUNNING","","","",
       "INSTALL_VIRUS_SCANNER","","","",
       "IS_ID_ON","","","",
       "TROJAN_ATTACK","","","",
       "IS_ATTACK_MALLICIOUS","","","",
       "IS_SYSTEM_PERFORMANCE_SLOW","","","",
       "IS_IT_DOS_ATTACK","","","",
       "IS_IT_PING_FLOOD","","","",
       "IS_IT_PING_DEATH","","","",
       "IS_SMURFING","","","",
       "IS_FRAGGLE","","","",
       "IS_IT_SYN_FLOOD","","",""
};

int getClauseNumber(string str, int afterindex=0)
{
     for(int i=afterindex+1; i<=84; i++)
     {
             if(str==clause_variable_list[i])
             {
                       return i;
             }
     }

     return -999;

}


class variable_list
{
      string name;
      string value;
      string inst;
      public:
             variable_list(string name="", string value="", string inst="NI")
             {
                                  this->name=name;
                                  this->value=value;
                                  this->inst=inst;
             }
             string getName()
             {
                    return name;
             }

             string getValue()
             {
                    return value;
             }
             string getInst()
             {
                    return inst;
             }
              void setName(string name)
              {
                   this->name=name;
              }
              void setValue(string value)
              {
                   this->value=value;
              }
              void setInst(string inst)
              {
                   this->inst=inst;
              }

};
variable_list var_list[37];

bool initilizeVariableList()
{
     string var[]={
                 "",
"IS_ATTACK_IDENTIFIES",
"ATTACK_FOUND",
"IP_SPOOFING_ATTACK",
"IDENTIFY_IP_ADDRESS",
"IS_SPOOFING_REFERS",
"RECHECK_HEADER_HTTP",
"IS_CALLER_ID_ATTACKS",
"CHECK_FRAUD_CALLS",
"IS_EMAIL_ADDRESS_REDIRECTING",
"CHECK_RETURN_ADDRESS",
"SNIFFING_ATTACK",
"IDENTIFY_SENDER_RECEIVER_HOST",
"MATCH_IDENTIFICATION",
"ENCRYPT_EMAILS",
"IDENTIFY_SENDER_RECEIVER_EMAILS",
"MALWARE_ATTACK",
"ON_ACCESS_SCAN",
"IS_ANTIVIRUS_RUNNING",
"TURN_ON_ANTIVIRUS",
"INSTALL_VIRUS_SCANNER",
"RUN_VIRUS_SCANNER",
"IS_ID_ON",
"TURN_ON_FIREWALLS",
"TROJAN_ATTACK",
"CHECK_SECURITY_SOFTWARE_ON_OR_OFF",
"IS_ATTACK_MALLICIOUS",
"CHECK_THE_MOVEMENT_OF_THE_DATA",
"IS_SYSTEM_PERFORMANCE_SLOW",
"RESET_THE_COMPUTER",
"IS_IT_DOS_ATTACK",
"IS_IT_PING_FLOOD",
"ON_ACCESS_SCAN",
"IS_SMURFING",
"IS_IT_PING_DEATH",
"IS_FRAGGLE",
"IS_IT_SYN_FLOOD"
                 };

     for(int i=1; i<38; i++)
     {
             var_list[i].setName(var[i]);
             var_list[i].setValue("");
             var_list[i].setInst("NI");
     }
}

void printVariable_List()
{
     cout<<endl;

     cout<<"Variable List"<<endl;
    for(int i=1; i<38; i++)
     {
             cout<<"("<<i<<")\t";
             cout<<var_list[i].getName();
             cout<<"\t\t\t"<<var_list[i].getValue();
             cout<<"\t\t\t"<<var_list[i].getInst()<<endl;

     }
}

int getVariableIndex(string var)
{
    for(int i=1; i<38; i++)
     {
             if(var_list[i].getName()==var)
             {
                          return i;
             }

     }
     return -999;
}




class conclusion_variable_queue
{

      string var[50];
      int head, tail;
      public:
             conclusion_variable_queue()
             {
                                        cout<<"   1if";
                    head=tail=-1;
                    for(int i=0; i<50; i++)
                    {
                            var[i]="";
                    }
                    cout<<"   1if";
             }
             bool push(string var)
             {
                  cout<<"   1if";
                  if(head==-1)
                  {
                                head=0;

                  }


                  else 
                  {
                      for(int i=0; i<=tail; i++)
                      {
                              if(this->var[i]==var)
                              {
                                       return false;
                              }
                      }
                  }               

                  ++tail;

                  //cout<<"\nInto push Tail: "<<tail<<"  var:"<<var<<endl;
                  this->var[tail]=var;
                  return true;
             }
             string pop()
             {
                    string str;
                    if(head==-1)
                    {
                               str="";   
                    }           
                    else{
                         str=var[head];
                         if(head==tail)
                         {
                               tail++;
                         }
                         head++;
                    }
                    return str;
             }
             string getHead()
             {
                    return var[head];
             }
             string getTail()
             {
                    return var[tail];
             }
             bool isEmpty()
             {
                  if(head==-1){
                          //cout<<endl<<"   1if"<<endl;
                      return true;
                  }       

                  if(var[head]=="" && head==tail)//queue is empty if head value is null
                  {
                      //cout<<endl<<var[head]<<"   if"<<endl;
                      return true;
                  }
                  else
                  {
                      //cout<<endl<<var[head]<<"   else"<<endl;
                      return false;
                  }
             }
             void print()
             {
                  cout<<"\nQueue content: "<<endl;
                  if(head<0 || var[head]=="")
                  {
                            cout<<"Error: The queue is empty";
                            return;
                  }
                  for(int i=head; i<=tail; i++)
                  {
                          cout<<endl<<"  "<<i<<" "<<var[i];
                  }
                  cout<<endl;
             }

};








//treatment part begins from here//
//////////////////////////////////////////////////
///////////////forward chaining////////////////////
///////////////////////////////////////////////////
void forwardchaining(string str)
{

          cout<<"\nType of cancer: "<<str<<endl;
          system("pause");
          conclusion_variable_queue q;
          int clause_number=-999;
          int var_index;
          int rule=-999;

          if(!initilizeVariableList())
          {
              cout<<"\nError: variable list could not be initilized"<<endl;
          }

          //printVariable_List();

          q.push(str);
          var_index=getVariableIndex(str);
          var_list[var_index].setValue("YES");
          var_list[var_index].setInst("I");
          //cout<<endl<<"Before while loop"<<endl;
          //q.print();
          //system("pause");

          string var1="";
          string value1="";
          string var2="";
          string value2="";
          int afterindex=0;

          while(!q.isEmpty())
          {
                             string variable=q.getHead();

                             //cout<<"\nVariable: "<<variable<<endl;
                             afterindex=0;
                             while(afterindex!=-999)
                             {
                                     //cout<<"\nVariable: "<<variable<<"   afterindex: "<<afterindex<<endl;               
                                     afterindex=clause_number=getClauseNumber(variable,afterindex);
                                     //cout<<"\nVariable: "<<variable<<"   afterindex: "<<afterindex<<endl;

                                     if(afterindex<1)
                                     {
                                                     //cout<<"\nNo more clauses at this level"<<endl;
                                                     //system("pause");
                                                     //afterindex=50000;
                                                   break;
                                     }
                                     rule=(int(clause_number/4)+1)*10;
                                     cout<<"\nClause number: "<<clause_number<<"  Rule number: "<<rule<<endl;
                                     //system("pause");

                                     switch(rule){
                                                 case 10:

                                                                 var1="IS_ATTACK_IDENTIFIES";
                                                                 value1="NO";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="ATTACK_FOUND";
                                                                                   value2="NO";


                                                                 }

                                                                  break;
                                                     case 20:

                                                                 var1="IS_SPOOFING_ATTACK";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="IDENTIFY_IP_ADDRESS";
                                                                                   value2="YES";

                                                                 }
                                                                 break;
                                                     case 30:

                                                                 var1="IS_SPOOFING_REFERS";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="RECHECK_HEADER_HTTP";
                                                                                   value2="YES";
                                                                 }           
                                                                 break;
                                                     case 40:

                                                                 var1="IS_CALLER_ID_ATTACKS";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="CHECK_FRAUD_CALLS";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 50:
                                                                 var1="IS_EMAIL_ADDRESS_REDIRECTING";
                                                                value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="CHECK_RETURN_ADDRESS";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 60:
                                                                 var1="SNIFFING_ATTACK";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="IDENTIFY_SENDER_RECEIVER_HOST";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 70:
                                                                 var1="MATCH_IDENTIFICATION";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="IDENTIFY_SENDER_RECEIVER_HOST";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 80:
                                                                 var1="ENCRYPT_EMAILS";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="IDENTIFY_SENDER_RECEIVER_EMAILS";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 90:
                                                                 var1="MALWARE_ATTACK";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="ON_ACCESS_SCAN";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 100:
                                                                 var1="IS_ANTIVIRUS_RUNNING";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="TURN_ON_ANTIVIRUS";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 110:
                                                                 var1="INSTALL_VIRUS_SCANNER";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="RUN_VIRUSSCANNER";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 120:
                                                                 var1="IS_ID_ON";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="TURN_ON_FIREWALLS";
                                                                                   value2="YES";
                                                                 }
                                                                 break;
                                                     case 130:
                                                                 var1="TROJAN_ATTACK ";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="CHECK_SECURITY_SOFTWARE_ON_OR_OFF";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                     case 140:
                                                                 var1="IS_ATTACK_MALLICIOUS";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="CHECK_THE_MOVEMENT_OF_DATA";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                     case 150:
                                                                 var1="IS_SYSTEM_PERFORMANCE_SLOW";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="RESET_THE_COMPUTER";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                     case 160:
                                                                 var1="IS_IT_DOS_ATTACK";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="FILTER_TARGET";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                     case 170:
                                                                 var1="IS_IT_PING_FLOOD";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="ON_ACCESS_SCAN";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                      case 180:
                                                                 var1="IS_IT_PING_DEATH";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="ON_ACCESS_SCAN";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                     case 190:
                                                                 var1="IS_SMURFING";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="ON_ACCESS_SCAN";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                     case 200:
                                                                 var1="IS_FRAGGLE";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="ON_ACCESS_SCAN";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                                     case 210:
                                                                 var1="IS_IT_SYN_FLOOD";
                                                                 value1="YES";

                                                                 if(var1==variable && var_list[var_index].getValue()==value1)
                                                                 {
                                                                                   var2="ON_ACCESS_SCAN";
                                                                                   value2="YES";
                                                                 }
                                                     break;
                                     }
                                     //////////////gettting some error here please check
                               //      cout<<"\n\nbefore push"<<endl;
                               //cout<<endl<<var2<<"---------\n";
                                     if(q.push(var2))
                                     {
                                             //cout<<"\n\after push"<<endl;
                                             //system("pause");
                                             //q.print();
                                             var_index=getVariableIndex(var2);
                                             //cout<<"\nVariable index of "<<var2<<"   index: "<<var_index;
                                             //system("pause");
                                             if(var_index<1)
                                             {
                                                            cout<<"ERROR: VARIABLE: "<<var2<<" NOT FOUND IN THE VARIABLE LIST";
                                                            system("pause");
                                             }
                                             var_list[var_index].setValue("YES");
                                             var_list[var_index].setInst("I");
                                  //           printVariable_List();
                                     }




                             }
                             q.pop();
                             //cout<<"\nNext Level        Poped: "<<q.pop()<<endl;
                             //system("pause");
                             /*if(!q.isEmpty())
                             {
                                                  cout<<"\n\n\nend of loop\n\n\n";
                             }
                             */
                             //system("pause");
                             //return;

          }

//          cout<<"\ndone"<<endl;
//          printVariable_List();

          cout<<"\n\nThe recommended treatment for "<<str<<" are(is): "<<endl;

          int k=1;
          for(int x=1; x<22; x++)
          {
                  if(var_list[x].getInst()=="I" && var_list[x].getName()!=str)
                  {
                                  cout<<k++<<". "<<var_list[x].getName()<<endl;
                  }
          }

          cout<<endl<<endl;

//          q.print();

          system("pause");


}





int main(int argc, char *argv[])
{

    string attack[]={

                 "MALWARE_ATTACK"

                 }; 

     for(int i=0; i<1; i++)
     {                                              
              forwardchaining(attack[i]);
              //system("pause");
     }
    //attack is a string. this string specifies type of attack.





    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 3 Replies

I assume you are using *nix os, so if you get a core dump then run gdb or another debugger that will tell you where the problem is assuming you compile your program for debug.

You have 2 questionable sections.

All those strings are defined at least twice. You should make a master list of the strings and use when you need one, use an index into the master list array.

By carefully designing the list, you may (not guaranteed) be able to make your switch much shorter by combining cases and using the index rather than the strings themselves.

Also, read this about formatting. It's a good try, but much to wide.

You need a header file for all these string return type and data type. Just include :

# include <string>

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.