Skip to main content
deleted 3077 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

skipping Skipping comment line from keyboard (stdin)via stdin

I have to write a code in c++ where theThe requirement is to skip a line beginning from '#'# sign. So basically Basically, we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code i, I have used multiple lines to skip a commented line which is in red colored text. But i

I wanted to know if it could be improved. As in, just one line of code to skip comment line. 

Please note: The comment line could be given anywhere from the keyboard.

 #include
    #include
    #include
    #include
    
    using namespace std;
    
    unsigned long int m;
    unsigned long int n;
    int res;
    unsigned long int i, j, k, ctr, maxIndex;
    long int l,la,lb;
    string command, key,comment;
    string indexS;
    long int index, lo=0, hi,mid;
    char key1[500];
    char keyd[500];
    char keym[500];
    char data1[10000][500], maskS[10000][500];
    char maskey[10000];
    char maskSt[10000][500];
    char data[10000][500];
    
    void *runner(void *numbers);
    void *runnera(void *numbersa);
    void *runnerStar(void *numbers);
    void *runnerStara(void *numbersa);
    int readKey(string s); /* A function that returns 1 when '*' is found and returns 0 if '*' not found in key*/
    void checkKey(string str);
    void checkIndex(string strInd);
    char StoC(string input);
    void mErr(string eKey, unsigned long int mCheck);
    long int search(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    long int searchA(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    /*---Structure for threads--*/
    struct arg_struct {
   char numData[5000][500];
  char numData[5000][500];numKey[500];
  unsigned long int numM;
  charunsigned numKey[500];long int numN;
  unsigned long int numLo;
  unsigned long int numM;numMid;
};

struct arg_structa {
  char numaData[5000][500];
  char numaKey[500];
  unsigned long int numN;numaM;
  unsigned long int numaN;
  unsigned long int numLo;numaMid;
  unsigned long int numaHi;
};

int unsignedmain()
{
 long 
 int numMid;cout << "Enter the command:"<< endl;
  cout << "Enter input  of the form:\n";
    cout << "(a) for insert(i) and replace(r) : i \n";
    cout << "(b) for delete(d)                : d \n";
    cout << "(c) for searching(s)             : s \n";
 
  cin >> command;
  const char *comnta = command.c_str();
 
  if(*comnta =='#') { /*Skipping comment line part*/
    cin.clear();
    getline(cin,comment);
    cin>>command;
    const char *comntb = command.c_str();
  
    if(*comntb =='#') { /*Skipping comment line part*/
      cin.clear();
      getline(cin,comment);
      cin>>command;
    }
    const char *comntc = command.c_str();
  
    if(*comntc =='#') { /*Skipping comment line part*/
      cin.clear();
      getline(cin,comment);
    struct arg_structa cin>>command;
    }
  }
 
  try {
    if(command == "d") {
      cin >> indexS;
      const char numaData[5000][500];*indexComm = indexS.c_str();

      if(*indexComm == '#') { /*Skipping comment line part*/
    cin.clear();
    getline(cin,comment);
    cin>>indexS;
    index = atoi(indexS.c_str());
    const char numaKey[500];*indexComma = indexS.c_str();
    
   unsigned longif(*indexComma int== numaM;'#') { /*Skipping comment line part*/
      unsignedindex long= int0;
 numaN;     cin.clear();
      unsignedgetline(cin,comment);
 long int numaMid;   cin>>indexS;
      unsignedindex long= intatoi(indexS.c_str());
 numaHi;   }
      }
      checkIndex(indexS);
      index  = atoi(indexS.c_str());
    }
   
    else if(command =="s") {
      cin >> key;
      const char *keyComm = key.c_str();
    
    int main if(*keyComm == '#') { /*Skipping comment line part*/
    cin.clear();
    getline(cin,comment);
    cin>> key;
    const char *keyComma = key.c_str();
   
    if(*keyComma == '#') { /*Skipping comment line part*/
      cin.clear();
      getline(cin,comment);
      cin>> key;
    }
      }
      checkKey(key);
    }
     
      cout << "Enter the command:"<< endl;
      cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) andelse replaceif(r) : i \n";
      command == cout"i" <<|| "(b)command for== delete(d"r")                : d \n";{
        cout << "(c) for searching(s)             :cin s>> \n";indexS;
     
      const char *indexCommc = indexS.c_str();
      if(*indexCommc == '#') { /*Skipping comment line part*/
    cin.clear();
 >> command;  getline(cin,comment);
    cin>>indexS;
    index = atoi(indexS.c_str());
    const char *comnta*indexCommd = commandindexS.c_str();
   
    if(*indexCommd == '#') {
      index = 0;
      cin.clear();
      getline(cin,comment);
      cin>>indexS;
      index = atoi(indexS.c_str());
    }
      }
      checkIndex(indexS);
      index  = atoi(indexS.c_str());
      ctr = index;
     
      if(*comnta =='#') { /*Skipping commentmaxIndex line< part*/ctr)
      maxIndex = cin.clear();ctr;
        getline(cin,comment);
      cin >> cin>>command;key;
        const char *comntb*keyCommb = commandkey.c_str();
      
        if(*comntb*keyCommb =='#'== '#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>>command;
       cin>> }key;
        const char *comntc*keyCommc = commandkey.c_str();
      
        if(*comntc*keyCommc =='#'== '#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
         cin>> cin>>command;key;
        }
      }
      checkKey(key);
    }
   
    else
      throw(command);  
  }
 
  catch(string err) {
    cout << "The input must be of the form:\n";
    cout << "(a) for insert(i) and replace(r) : i \n";
    cout << "(b) for delete(d)                : d \n";
    cout << "(c) for searching(s)             : s \n";
    exit(0);
  }
 
  m = key.size();  /* Bit size of each entry of TCAM*/
  n = 10000;                     /*Number of entries in TCAM*/
 
  /*--------------------------------------------------------------------------------------*/
  int loop = 0;
  for(;;) {
    cout << "Enter input  of the form:\n";
    cout << "(a) for insert(i) and replace(r) : i \n";
    cout << "(b) for delete(d)                : d \n";
    cout << "(c) for searching(s)             : s \n";
    if(loop == 1) {
      cout << "Enter the command:\n";
      if (!(cin >> command))
    break;
      const char *comntd = command.c_str();
      if(*comntd =='#') {
    cin.clear();
    getline(cin,comment);
    cin>>command;
    const char *comntd = command.c_str();
   
    if(*comntd =='#') {
      cin.clear();
      getline(cin,comment);
      cin>>command;
    }
    const char *comnte = command.c_str();
   
    if(*comnte =='#') {
      cin.clear();
      getline(cin,comment);
      cin>>command;
    }
      }
     
      try {
        if(command == "d") {
          cin >> indexS;
          const char *indexComm = indexS.c_str();
    
          if(*indexComm == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexComma =>> indexS.c_str();indexS;
       
        if(*indexComma == '#') {const /*Skippingchar comment*indexCommd line= part*/indexS.c_str();
         if(*indexCommd index== ='#') 0;{
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }  const char *indexComme = indexS.c_str();
         
          if(*indexComme == '#') {
        index = 0;
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
        }
        checkIndex(indexS);
        index  = atoi(indexS.c_str());
      }
    else if(command =="s") {
          cin >> key;
          const char *keyComm*keyCommd = key.c_str();
        
          if(*keyComm*keyCommd == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyComma*keyComme = key.c_str();
       
        if(*keyComma*keyComme == '#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
      }
      checkKey(key);
      mErr(key, m);
    }
   
    else if(command == "i" || command == "r") {
          cin >> indexS;
         
          const char *indexCommc*indexCommf = indexS.c_str();
      
      if(*indexCommc*indexCommf == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexCommd*indexCommg = indexS.c_str();
       
        if(*indexCommd*indexCommg == '#') {
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
          ctr = index;
         
          if(maxIndex < ctr)
        maxIndex = ctr;
         
          cin >> key;
          const char *keyCommb*keyCommf = key.c_str();
         
          if(*keyCommb*keyCommf == '#') {
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyCommc*keyCommg = key.c_str();
       
        if(*keyCommc*keyCommg == '#') {
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
       
        else
          throw(command);  
      }
      checkKey(key);
      mErr(key, m);
    }  

    else
      throw(command);
      }
     
      catch(string err) {
    {
    cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
      }
    }
      loop =1;
      /*-------------Without '*' bits in the search key i.e res=0-------------------------------*/
      /*-------------------------------Begin searching part of the code------------------------*/
     
      m = key.sizereadKey(key);  /* Bit size of each entry of TCAM*/
      n = 10000;                     /*Number of entries in TCAM*/
     
      /*--------------------------------------------------------------------------------------*/
      int loop = 0;
      for(;;) {
        cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        if(loop == 1) {
          cout << "Enter the command:\n";
          if (!(cin >> command))
        break;
          const char *comntd = command.c_str();
          if(*comntd =='#') {
        cin.clear();
        getline(cin,comment);
        cin>>command;
        const== char"s" *comntd&& =res command.c_str(==0);
       
        if(*comntd =='#') {
          cin.clear();
          getlineStoC(cin,commentkey);
          cin>>command;
        }
   /* returns key1 which is consta char *comnte = command.c_str();
       
        if(*comnte =='#') {type*/
          cin.clear();
         pthread_t getline(cintid,comment);
          cin>>command;
        }
          }
         
          try {
        if(command == "d")
          {
            cin >> indexS;
           
            const char *indexCommd = indexS.c_str();tida;
            if(*indexCommd == '#')pthread_attr_t {attr;
              cin.clearpthread_attr_init(&attr);
              getline(cin,comment);
            struct arg_struct cin>>indexS;args;
              indexstruct =arg_structa atoi(indexS.c_str());argsa;
              const char *indexComme = indexS.c_str();
     /*----Spliting up the indices of the TCAM for parallelism-----*/
              if(*indexComme == '#')lo {=0;
            indexhi = 0;
            cin.clear();
            getline(cin,comment);
            cin>>indexS;maxIndex;
            indexmid = atoi(indexS.c_str()lo+hi);/2;
              }
       /*-----Assigning values to the data }structure-------*/
            checkIndexfor(indexS);
          unsigned long indexint o=lo; =o<=mid; atoi(indexS.c_str()o++);
          }
        else iffor(command =="s") {
          cin >> key;
          const char *keyCommdunsigned =long key.c_str();
int r=0; r        if(*keyCommd == '#') {
            cinargs.clear();
            getline(cin,comment);
            cin>> key;
            const char *keyCommenumData[o][r] = key.c_str();
            if(*keyComme == '#') {data[o][r];
              cinargs.clear();
              getline(cin,comment);
             numKey[r] cin>>= key;key1[r];
            }
          }
      for(unsigned long int x=mid+1; checkKey(keyx<=hi; x++);
        for(unsigned long mErr(key,int m);u=0; u           {
        }       argsa.numaData[x][u] = data[x][u];
               argsa.numaKey[u] = key1[u];
       
        else if(command == "i" || command == "r") {
          cin >> indexS;
          const char *indexCommf = indexS.c_str();
         
          if(*indexCommf == '#') {
            cin.clear();
            getline(cin,comment);
            cin>>indexS;
            index = atoi(indexS.c_str());
            const char *indexCommg = indexS.c_str();
           
            if(*indexCommg == '#') {
              index = 0;
              cin.clear();
              getline(cin,comment);
              cin>>indexS;
              index = atoi(indexS.c_str());
            }
          }
          checkIndex(indexS);
          index args.numM = atoi(indexS.c_str());m;
          ctrargs.numN = index;n;
        args.numLo = lo;
        args.numMid = if(maxIndexmid;

 < ctr)
            maxIndex argsa.numaM = ctr;m;
         
 argsa.numaN = n;
        cinargsa.numaMid >>= key;mid;
          const char *keyCommfargsa.numaHi = key.c_str();hi;
          if(*keyCommf == '#') {
        /*----Creating 2 threads to cin.clear();
be executed------*/
        pthread_create(&tid, &attr, runner, getline(cin,commentvoid *)&args);
            cin>> key;
     pthread_create(&tida, &attr, runnera, (void *)&argsa); /* thread constfor charsecond *keyCommghalf =of key.c_str();data*/
       
            if(*keyCommg == '#') {
              cin.clear();
              getline(cin,comment);
              cin>> key;
            }
          }
          checkKey(key);
          mErr(key, m);
        }  
    
        else
          throw(command);
          }
         
          catch(string err)    {
        cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
          }
        }
          loop =1;
          /*-------------Without '*'Waiting bitsfor inboth the search key i.e res=0-------------------------------*/
          /*-------------------------------Begin searching partthrads ofto thecomplete code-----------------execution-------*/
         
          readKeypthread_join(keytid, NULL);
         
          ifpthread_join(command == "s" && res ==0)
        {
         tida, StoC(keyNULL); /* returns key1 which is a char type*/
       
          pthread_t tid, tida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
          /*----Spliting up the indices of the TCAM for parallelism-----*/
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
         
          /*-----Assigning values to the data structure-------*/
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
    
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
           
              }
         
            args.numM = m;
            args.numN = n;
            args.numLo = lo;
            args.numMid = mid;
    
                   argsa.numaM = m;
            argsa.numaN = n;
            argsa.numaMid = mid;
            argsa.numaHi = hi;
              
            /*----Creating 2 threads to be executed------*/
            pthread_create(&tid, &attr, runner, (void *)&args);
                 pthread_create(&tida, &attr, runnera, (void *)&argsa); /* thread for second half of data*/
           
            /*-------Waiting for both the thrads to complete execution-------*/
            pthread_join(tid, NULL);
            pthread_join(tida, NULL);
           
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <        
            else if(la == -1 && lb >=0)
              cout<< lb <        
            else if(la >= 0 && lb ==-1)
              cout<< la <        
            else if(la>=0 && lb >=0)
              cout<< la <     }
         
         
          /*-----------------Search with '*' bits in the search key---------------------------------------------*/
         
          else if(command == "s" && res==1)
        {
          StoC(key);
         
          pthread_t thid, thida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
         
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
    
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
              }
         
          args.numM = m;
          args.numN = n;
          args.numLo = lo;
          args.numMid = mid;
         
          argsa.numaM = m;
          argsa.numaN = n;
          argsa.numaMid = mid;
          argsa.numaHi = hi;
         
          /*--------creating thread for program-----------------*/
          pthread_create(&thid, &attr, runnerStar, (void *)&args);
          pthread_create(&thida, &attr, runnerStara, (void *)&argsa); /* thread for second half of data*/
         
          /*------Wait for both the threads to complete execution--------*/
          pthread_join(thid, NULL);
          pthread_join(thida, NULL);
    
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <
    
            else if(la == -1 && lb >=0)
              cout<< lb <
           
            else if(la >= 0 && lb ==-1)
              cout<< la <
    
            else if(la>=0 && lb >=0)
              cout<< la <
        }
     
     
      /*-------------Inserting----Search datawith at'*' index--bits iin keythe 1011search key-----dataind[n+1][m]----------------------------------------*/
      
      else if(command =="i"== "s" && res==1)
    {
      StoC(key);
      
   {   pthread_t thid, thida;
      pthread_attr_t attr;
   StoC   pthread_attr_init(key&attr);
     
      struct arg_struct args;
      struct arg_structa argsa;
  if((data[index][0] == '\0')) {
      lo =0;
      hi =  maxIndex;
      mid = (lo+hi)/2;

      for(unsigned long int bit=0;o=lo; bito<=mid; o++)
        for(unsigned long data[index][bit]int r=0; r           {
        args.numData[o][r] = key1[bit];data[o][r];
        args.numKey[r] = key1[r];
          }
          else {
            unsigned long int g = 0;
            for(g=(index+1);g           if(data[g][0] =='\0')
            break;
            }
           
            if(maxIndex < g)
              maxIndex = g;
           
            for( unsigned long int e=g;x=mid+1; e>index;x<=hi; e--x++)
              for(unsigned long int biti=0;u=0; bitiu         data[e][biti] = data[e-1][biti];{
           
           argsa.numaData[x][u] for(= unsigneddata[x][u];
 long int bit=0; bit           data[index][bit]argsa.numaKey[u] = key1[bit];key1[u];
          }
        }
          /*------------- delete index---------------------------*/
          else if(command =="d")
        {  
          i=0;
          for(i=index; i<=maxIndex;i++)
            for(int bitd=0; bitd           data[i][bitd]= data[i+1][bitd];
         
          if(maxIndex >= index && maxIndex !=0)
            maxIndex = maxIndex-1;
        }
         
          /*------------- replace index key---------------------------*/
         
          else if(command == "r")
        {  
          StoC(key);
          for(unsigned long int bitr=0; bitr         data[index][bitr] = key1[bitr];
        }
           cout<<"\n\n\n";
      cout<<<"tcam table="" entries="" without="" star\n";   // line();
      cout<<<"index"<<<"data"<<<"mask"<
      //line();
      for(unsigned long int k=0;k<=10;k++)
        { 
          cout<<<<<
        }
      //line();
      cout<<<
    
      }
     
      returnargs.numM 0;= m;
      args.numN = n;
      args.numLo = lo;
      args.numMid = mid;
     
      argsa.numaM = m;
      argsa.numaN = n;
      argsa.numaMid = mid;
      argsa.numaHi = hi;
     
      /*--------creating thread for program-----------------*/
      pthread_create(&thid, &attr, runnerStar, (void *)&args);
      pthread_create(&thida, &attr, runnerStara, (void *)&argsa); /* thread for second half of data*/
     
      /*------Wait for both the threads to complete execution--------*/
      pthread_join(thid, NULL);
      pthread_join(thida, NULL);

        /*------Printing ouput values based on lowest index match----------*/
        if(la ==-1 && lb==-1)
          cout<< la <

        else if(la == -1 && lb >=0)
          cout<< lb <
       
        else if(la >= 0 && lb ==-1)
          cout<< la <

        else if(la>=0 && lb >=0)
          cout<< la <
    }
   
  
     /*-------------Inserting data at index-- i key 1011-----dataind[n+1][m]--Function definitions begin-----------------------*/
    int readKey else if(stringcommand s=="i")
    {
      const char *p = s.c_strStoC(key);
      
 while     if(*p(data[index][0] !=== '\0')) {
        if for(*punsigned ==long '*')int {
bit=0; bit         res  data[index][bit] = 1;key1[bit];
      }
      return(res);else {
        }unsigned long int g = 0;
        elsefor(g=(index+1);g {
          resif(data[g][0] ==='\0')
 0;       break;
        }
        
 *p++;       if(maxIndex < g)
          maxIndex = g;
       
        for( unsigned long int e=g; e>index; e--)
          for(unsigned long int biti=0; biti         data[e][biti] = data[e-1][biti];
       
        for( unsigned long int bit=0; bit           data[index][bit] = key1[bit];
      }
    }
      /*------------- delete index---------------------------*/
      else if(command =="d")
    {  
      i=0;
      for(i=index; i<=maxIndex;i++)
        for(int bitd=0; bitd           data[i][bitd]= data[i+1][bitd];
     
      if(maxIndex >= index && maxIndex !=0)
        maxIndex = maxIndex-1;
    }
     
      /*------------- replace index key---------------------------*/
     
      else if(command == "r")
    {  
      StoC(key);
      for(unsigned long int bitr=0; bitr         data[index][bitr] = key1[bitr];
    }
       cout<<"\n\n\n";
  cout<<<"tcam table="" entries="" without="" star\n";   // line();
  cout<<<"index"<<<"data"<<<"mask"<
  //line();
  for(unsigned long int k=0;k<=10;k++)
    { 
      cout<<<<<
    }
  //line();
  cout<<<

  }
 
  return 0;
}

/*----------------------Function definitions begin------------------*/
int readKey(string s)
{
  const char *p = s.c_str();
  while (*p != '\0') {
    if (*p == '*') {
      res = 1;
      return(res);
    }
    else {
      res = 0;
    }
    *p++;
  }
  return(res);
}

/*-------------------------------------------------*/
void checkKey(string str)
{
  const char *p = str.c_str();
  try {
    while (*p != '\0') {
      if(*p=='*' || *p=='0' || *p=='1')
    *p++;
      else
    throw(*p);
    }
  }
  catch( char error) {
    cout <<"ERROR: The input key must be in '1', '0', or '*'" << endl;
    exit(0);
  }    
}
/*-------------------------------------------------*/

void checkKeycheckIndex(string strstrInd)
{
  const char *ind = strInd.c_str();
  try {
    while (*ind != '\0') {
      if(*ind=='0'|| *ind=='1'|| *ind=='2'|| *ind=='3'|| *ind =='4'|| *ind=='5'|| *ind=='6'|| *ind=='7'|| *ind=='8'|| *ind=='9')
    *ind++;
      else
    throw(*ind);
    }
  }
  catch( char error) {
    cout <<"ERROR: The input index must be a number" << endl;
    exit(0);
  }
}


/*----------------------------------------------------*/
char StoC(string input)
{
  i = 0; 
  const char *p = input.c_str();
  while (*p != '\0')
    {
      const char *p = str.c_str();
      try {
        while (*pkey1[i] != '\0') {*p;
          if(*p=='*' || *p=='0' || *p=='1')i++;
        *p++;
          else
        throw(*p);
        }
      }
      catch( char error) {
        cout <<"ERROR: The input key must be in '1', '0', or '*'" << endl;
        exit(0);
      }    
    }
  return(*key1);
}

 /*-----------------ERROR in key length-------------------------*/

void mErr(string eKey, unsigned long int mCheck)
{
  if(eKey.size() != mCheck) {
    cout<< "ERROR: Key size must not vary"<
    exit(0);
  }
}

/*---------------------------Search without * key-----------------*/

long int search(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
{
  /*---------------------Generating mask of the TCAM entries----------------------------*/
 
  for(unsigned long int i=initial; i<=final; i++)
    voidfor(unsigned checkIndexlong int j=0; j       if(stringdataS[i][j] strInd== '*')
    maskS[i][j]='0';
      else
    {
      const char *inddata1[i][j] = strInd.c_str();dataS[i][j];
      trymaskS[i][j] {= '1';
    }
    while (*ind != 
 '\0') {
bool flag = true;
  for(la=initial; la<=final; la++) {
  if(*ind=='0'|| *ind=='1'|| *ind=='2'||flag *ind=='3'||= *indtrue;
 =='4'|| *ind=='5'|| *ind=='6'|| *ind=='7'||for(unsigned *ind=='8'||long *ind=='9')
int k=0;k       *ind++;
if ( maskS[la][k]== '1') {
    if (keyS[k] != elsedata1[la][k])
      {
  throw(*ind);
      flag = }false;
      }
      catch( char error) {
        cout <<"ERROR: The input index must be a number" << endl;
        exit(0);
      }
    }
    if (flag)
      {
    return(la);
      }
  }
  la = -1;
  return(la); 
}

/*---------------------------Search without * key2-----------------*/
long int searchA(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
{
  /*---------------------Generating mask of the TCAM entries----------------------------*/
  for(unsigned long charint StoCu=initial+1; u<=final; u++)
    for(stringunsigned inputlong int j=0; j       if(dataS[u][j] == '*')
    maskS[u][j]='0';
      else
    {
      idata1[u][j] = 0; dataS[u][j];
      const char *pmaskS[u][j] = input.c_str();'1';
    }  while (*p != '\0')
    
  bool flaga = {true;
   for(lb=initial+1; lb<=final; lb++) {
    key1[i]flaga = *p;true;
         for(unsigned i++;
long int k=0;k       if *p++;( maskS[lb][k]== '1') {
    if (keyS[k] != data1[lb][k]) }{
      return(*key1);flaga = false;
    }
    
     /*-----------------ERROR in key length-------------------------*/
    
    void mErr(string eKey, unsigned long int mCheck)
    {
      if(eKey.size() != mCheck) {
        cout<< "ERROR: Key size must not vary"<
        exit(0);
      }
    }
    
    /*---------------------------Search without * key-----------------*/
    
    long int search(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
     
      for(unsigned long int i=initial; i<=final; i++)
        for(unsigned long int j=0; j       if(dataS[i][j] == '*')
        maskS[i][j]='0';
          else
        {
          data1[i][j] = dataS[i][j];
          maskS[i][j] = '1';
        }
          
      bool flag = true;
      for(la=initial; la<=final; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskS[la][k]== '1') {
        if (keyS[k] != data1[la][k])
          {
            flag = false;
          }
          }
        }
        if (flagflaga)
          {
        return(lalb);
          }
      }
   }
  
  lalb = -1;
  return(lb);  
 
}
/*------------------------------Search with return* key-------------------------------------------------------*/
long int searchStar(lachar dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
{
 
  for(unsigned long int a=initialSt; a<=finalSt; a++)
    for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
      else
    {
      data1[a][b] = dataSt[a][b];
      maskSt[a][b] = '1';
    }
    
    /*---------------------------Search without * key2-----------------*/
    long int searchAfor(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)j=0;j
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
      for(unsigned long int u=initial+1; u<=final; u++)
        for(unsigned long int j=0; j       if(dataS[u][j] ==keySt[j] '*'=='*')
      keym[j] = maskS[u][j]='0';'0';
          else
        {
     
      data1[u][j]keyd[j] = dataS[u][j];keySt[j];
          maskS[u][j]keym[j] = '1';
        }       
    }
  bool flagaflag = true;
      for(lb=initial+1; lb<=final;la=initialSt; lb++la<=finalSt; la++) {
        flagaflag = true;
        for(unsigned long int k=0;k       if ( maskS[lb][k]==maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyS[k]keyd[k] != data1[lb][k]) {
          flaga = false;
        }
          }
        }
       data1[la][k] if (flaga)
          {
        return(lb);
        flag = }false;
      }
     
      lb = -1;
      return(lb); 
     }
    }
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
    {
     
      for(unsigned long int a=initialSt; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flag = true;
      for( la=initialSt; la<=finalSt; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flag = false;
          }
          }
        }
        if (flag)
          {
        return(la);
          }
      }
    }
 
  la = -1;
  return(la); =
}
/*---------------------------------------------------------------------------------*/
/*------------------------------Search with * key-1;------------------------------------------------------*/
long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned returnlong int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
{
  for(launsigned long int a=initialSt+1; a<=finalSt; a++);
    for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
      else
    {
      data1[a][b] = dataSt[a][b];
      maskSt[a][b] = '1';
    }
    /*---------------------------------------------------------------------------------*/
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStarAfor(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)j=0;j
    {
      for(unsigned long int a=initialSt+1; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
     
     
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
         
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flaga = true;
      for( lb=initialSt+1; lb<=finalSt; lb++) {
        flaga = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flaga = false;
          }
          }
        }
        if (flaga)
          {
        return(lb);
          }
      }
     
      lbelse
    { 
      keyd[j] = -1;keySt[j];
      return(lb);keym[j] = '1';
    }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runner(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      search(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
 bool flaga = /*-----------Thread2true;
 function for( searchinglb=initialSt+1; withoutlb<=finalSt; starlb++) key------------------*/{
    voidflaga *runnera(void= *numbersa)true;
    {
for(unsigned long int k=0;k   struct arg_structa *argsa = if (struct arg_structamaskSt[la][k]== *'1' && keym[k]=='1')numbersa;
 {
    if searchA(argsa->keyd[k] numaData,!= argsa->data1[la][k] numaKey, argsa->)
 numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);{
      pthread_exit(0);  flaga = false;
      }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runnerStar(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      searchStar(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
    void *runnerStara(void *numbersa)
    {
      struct arg_structa *argsa = (struct arg_structa *)numbersa;
      searchStarA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
      pthread_exit(0);
    }
    if (flaga)
      {
    return(lb);
      }
  }
 
  lb = -1;
  return(lb); 
}

/*-----------Thread1 function for searching without star key------------------*/
void *runner(void *numbers)
{
  struct arg_struct *args = (struct arg_struct *)numbers;
  search(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
  pthread_exit(0);
}

/*-----------Thread2 function for searching without star key------------------*/
void *runnera(void *numbersa)
{
  struct arg_structa *argsa = (struct arg_structa *)numbersa;
  searchA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
  pthread_exit(0);
  }

/*-----------Thread1 function for searching without star key------------------*/
void *runnerStar(void *numbers)
{
  struct arg_struct *args = (struct arg_struct *)numbers;
  searchStar(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
  pthread_exit(0);
}

void *runnerStara(void *numbersa)
{
  struct arg_structa *argsa = (struct arg_structa *)numbersa;
  searchStarA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
  pthread_exit(0);
}

skipping comment line from keyboard (stdin)

I have to write a code in c++ where the requirement is to skip a line beginning from '#' sign. So basically we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code i have used multiple lines to skip a commented line which is in red colored text. But i wanted to know if it could be improved. As in just one line of code to skip comment line. Please note: The comment line could be given anywhere from the keyboard.

 #include
    #include
    #include
    #include
    
    using namespace std;
    
    unsigned long int m;
    unsigned long int n;
    int res;
    unsigned long int i, j, k, ctr, maxIndex;
    long int l,la,lb;
    string command, key,comment;
    string indexS;
    long int index, lo=0, hi,mid;
    char key1[500];
    char keyd[500];
    char keym[500];
    char data1[10000][500], maskS[10000][500];
    char maskey[10000];
    char maskSt[10000][500];
    char data[10000][500];
    
    void *runner(void *numbers);
    void *runnera(void *numbersa);
    void *runnerStar(void *numbers);
    void *runnerStara(void *numbersa);
    int readKey(string s); /* A function that returns 1 when '*' is found and returns 0 if '*' not found in key*/
    void checkKey(string str);
    void checkIndex(string strInd);
    char StoC(string input);
    void mErr(string eKey, unsigned long int mCheck);
    long int search(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    long int searchA(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    /*---Structure for threads--*/
    struct arg_struct {
      char numData[5000][500];
      char numKey[500];
      unsigned long int numM;
      unsigned long int numN;
      unsigned long int numLo;
      unsigned long int numMid;
    };
    
    struct arg_structa {
      char numaData[5000][500];
      char numaKey[500];
      unsigned long int numaM;
      unsigned long int numaN;
      unsigned long int numaMid;
      unsigned long int numaHi;
    };
    
    int main()
    {
     
      cout << "Enter the command:"<< endl;
      cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
     
      cin >> command;
      const char *comnta = command.c_str();
     
      if(*comnta =='#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>command;
        const char *comntb = command.c_str();
      
        if(*comntb =='#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
        const char *comntc = command.c_str();
      
        if(*comntc =='#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
      }
     
      try {
        if(command == "d") {
          cin >> indexS;
          const char *indexComm = indexS.c_str();
    
          if(*indexComm == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexComma = indexS.c_str();
       
        if(*indexComma == '#') { /*Skipping comment line part*/
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
        }
       
        else if(command =="s") {
          cin >> key;
          const char *keyComm = key.c_str();
        
          if(*keyComm == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyComma = key.c_str();
       
        if(*keyComma == '#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
         
        else if(command == "i" || command == "r") {
          cin >> indexS;
         
          const char *indexCommc = indexS.c_str();
          if(*indexCommc == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexCommd = indexS.c_str();
       
        if(*indexCommd == '#') {
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
          ctr = index;
         
          if(maxIndex < ctr)
        maxIndex = ctr;
         
          cin >> key;
          const char *keyCommb = key.c_str();
         
          if(*keyCommb == '#') {
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyCommc = key.c_str();
       
        if(*keyCommc == '#') {
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
       
        else
          throw(command);  
      }
     
      catch(string err) {
        cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
      }
     
      m = key.size();  /* Bit size of each entry of TCAM*/
      n = 10000;                     /*Number of entries in TCAM*/
     
      /*--------------------------------------------------------------------------------------*/
      int loop = 0;
      for(;;) {
        cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        if(loop == 1) {
          cout << "Enter the command:\n";
          if (!(cin >> command))
        break;
          const char *comntd = command.c_str();
          if(*comntd =='#') {
        cin.clear();
        getline(cin,comment);
        cin>>command;
        const char *comntd = command.c_str();
       
        if(*comntd =='#') {
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
        const char *comnte = command.c_str();
       
        if(*comnte =='#') {
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
          }
         
          try {
        if(command == "d")
          {
            cin >> indexS;
           
            const char *indexCommd = indexS.c_str();
            if(*indexCommd == '#') {
              cin.clear();
              getline(cin,comment);
              cin>>indexS;
              index = atoi(indexS.c_str());
              const char *indexComme = indexS.c_str();
             
              if(*indexComme == '#') {
            index = 0;
            cin.clear();
            getline(cin,comment);
            cin>>indexS;
            index = atoi(indexS.c_str());
              }
            }
            checkIndex(indexS);
            index  = atoi(indexS.c_str());
          }
        else if(command =="s") {
          cin >> key;
          const char *keyCommd = key.c_str();
          if(*keyCommd == '#') {
            cin.clear();
            getline(cin,comment);
            cin>> key;
            const char *keyComme = key.c_str();
            if(*keyComme == '#') {
              cin.clear();
              getline(cin,comment);
              cin>> key;
            }
          }
          checkKey(key);
          mErr(key, m);
        }
       
        else if(command == "i" || command == "r") {
          cin >> indexS;
          const char *indexCommf = indexS.c_str();
         
          if(*indexCommf == '#') {
            cin.clear();
            getline(cin,comment);
            cin>>indexS;
            index = atoi(indexS.c_str());
            const char *indexCommg = indexS.c_str();
           
            if(*indexCommg == '#') {
              index = 0;
              cin.clear();
              getline(cin,comment);
              cin>>indexS;
              index = atoi(indexS.c_str());
            }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
          ctr = index;
         
          if(maxIndex < ctr)
            maxIndex = ctr;
         
           cin >> key;
          const char *keyCommf = key.c_str();
          if(*keyCommf == '#') {
            cin.clear();
            getline(cin,comment);
            cin>> key;
            const char *keyCommg = key.c_str();
       
            if(*keyCommg == '#') {
              cin.clear();
              getline(cin,comment);
              cin>> key;
            }
          }
          checkKey(key);
          mErr(key, m);
        }  
    
        else
          throw(command);
          }
         
          catch(string err)    {
        cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
          }
        }
          loop =1;
          /*-------------Without '*' bits in the search key i.e res=0-------------------------------*/
          /*-------------------------------Begin searching part of the code------------------------*/
         
          readKey(key);
         
          if(command == "s" && res ==0)
        {
          StoC(key); /* returns key1 which is a char type*/
       
          pthread_t tid, tida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
          /*----Spliting up the indices of the TCAM for parallelism-----*/
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
         
          /*-----Assigning values to the data structure-------*/
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
    
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
           
              }
         
            args.numM = m;
            args.numN = n;
            args.numLo = lo;
            args.numMid = mid;
    
                   argsa.numaM = m;
            argsa.numaN = n;
            argsa.numaMid = mid;
            argsa.numaHi = hi;
              
            /*----Creating 2 threads to be executed------*/
            pthread_create(&tid, &attr, runner, (void *)&args);
                 pthread_create(&tida, &attr, runnera, (void *)&argsa); /* thread for second half of data*/
           
            /*-------Waiting for both the thrads to complete execution-------*/
            pthread_join(tid, NULL);
            pthread_join(tida, NULL);
           
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <        
            else if(la == -1 && lb >=0)
              cout<< lb <        
            else if(la >= 0 && lb ==-1)
              cout<< la <        
            else if(la>=0 && lb >=0)
              cout<< la <     }
         
         
          /*-----------------Search with '*' bits in the search key---------------------------------------------*/
         
          else if(command == "s" && res==1)
        {
          StoC(key);
         
          pthread_t thid, thida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
         
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
    
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
              }
         
          args.numM = m;
          args.numN = n;
          args.numLo = lo;
          args.numMid = mid;
         
          argsa.numaM = m;
          argsa.numaN = n;
          argsa.numaMid = mid;
          argsa.numaHi = hi;
         
          /*--------creating thread for program-----------------*/
          pthread_create(&thid, &attr, runnerStar, (void *)&args);
          pthread_create(&thida, &attr, runnerStara, (void *)&argsa); /* thread for second half of data*/
         
          /*------Wait for both the threads to complete execution--------*/
          pthread_join(thid, NULL);
          pthread_join(thida, NULL);
    
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <
    
            else if(la == -1 && lb >=0)
              cout<< lb <
           
            else if(la >= 0 && lb ==-1)
              cout<< la <
    
            else if(la>=0 && lb >=0)
              cout<< la <
        }
     
          /*-------------Inserting data at index-- i key 1011-----dataind[n+1][m]-------------------------*/
          else if(command =="i")
        {
          StoC(key);
         
          if((data[index][0] == '\0')) {
            for(unsigned long int bit=0; bit           data[index][bit] = key1[bit];
          }
          else {
            unsigned long int g = 0;
            for(g=(index+1);g           if(data[g][0] =='\0')
            break;
            }
           
            if(maxIndex < g)
              maxIndex = g;
           
            for( unsigned long int e=g; e>index; e--)
              for(unsigned long int biti=0; biti         data[e][biti] = data[e-1][biti];
           
            for( unsigned long int bit=0; bit           data[index][bit] = key1[bit];
          }
        }
          /*------------- delete index---------------------------*/
          else if(command =="d")
        {  
          i=0;
          for(i=index; i<=maxIndex;i++)
            for(int bitd=0; bitd           data[i][bitd]= data[i+1][bitd];
         
          if(maxIndex >= index && maxIndex !=0)
            maxIndex = maxIndex-1;
        }
         
          /*------------- replace index key---------------------------*/
         
          else if(command == "r")
        {  
          StoC(key);
          for(unsigned long int bitr=0; bitr         data[index][bitr] = key1[bitr];
        }
           cout<<"\n\n\n";
      cout<<<"tcam table="" entries="" without="" star\n";   // line();
      cout<<<"index"<<<"data"<<<"mask"<
      //line();
      for(unsigned long int k=0;k<=10;k++)
        { 
          cout<<<<<
        }
      //line();
      cout<<<
    
      }
     
      return 0;
    }
    
     /*----------------------Function definitions begin------------------*/
    int readKey(string s)
    {
      const char *p = s.c_str();
      while (*p != '\0') {
        if (*p == '*') {
          res = 1;
          return(res);
        }
        else {
          res = 0;
        }
        *p++;
      }
      return(res);
    }
    
    /*-------------------------------------------------*/
    void checkKey(string str)
    {
      const char *p = str.c_str();
      try {
        while (*p != '\0') {
          if(*p=='*' || *p=='0' || *p=='1')
        *p++;
          else
        throw(*p);
        }
      }
      catch( char error) {
        cout <<"ERROR: The input key must be in '1', '0', or '*'" << endl;
        exit(0);
      }    
    }
    /*-------------------------------------------------*/
    
    void checkIndex(string strInd)
    {
      const char *ind = strInd.c_str();
      try {
        while (*ind != '\0') {
          if(*ind=='0'|| *ind=='1'|| *ind=='2'|| *ind=='3'|| *ind =='4'|| *ind=='5'|| *ind=='6'|| *ind=='7'|| *ind=='8'|| *ind=='9')
        *ind++;
          else
        throw(*ind);
        }
      }
      catch( char error) {
        cout <<"ERROR: The input index must be a number" << endl;
        exit(0);
      }
    }
    
    
    /*----------------------------------------------------*/
    char StoC(string input)
    {
      i = 0; 
      const char *p = input.c_str();
      while (*p != '\0')
        {
          key1[i] = *p;
          i++;
          *p++;   
        }
      return(*key1);
    }
    
     /*-----------------ERROR in key length-------------------------*/
    
    void mErr(string eKey, unsigned long int mCheck)
    {
      if(eKey.size() != mCheck) {
        cout<< "ERROR: Key size must not vary"<
        exit(0);
      }
    }
    
    /*---------------------------Search without * key-----------------*/
    
    long int search(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
     
      for(unsigned long int i=initial; i<=final; i++)
        for(unsigned long int j=0; j       if(dataS[i][j] == '*')
        maskS[i][j]='0';
          else
        {
          data1[i][j] = dataS[i][j];
          maskS[i][j] = '1';
        }
          
      bool flag = true;
      for(la=initial; la<=final; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskS[la][k]== '1') {
        if (keyS[k] != data1[la][k])
          {
            flag = false;
          }
          }
        }
        if (flag)
          {
        return(la);
          }
      }
      la = -1;
      return(la); 
    }
    
    /*---------------------------Search without * key2-----------------*/
    long int searchA(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
      for(unsigned long int u=initial+1; u<=final; u++)
        for(unsigned long int j=0; j       if(dataS[u][j] == '*')
        maskS[u][j]='0';
          else
        {
          data1[u][j] = dataS[u][j];
          maskS[u][j] = '1';
        }       
      bool flaga = true;
      for(lb=initial+1; lb<=final; lb++) {
        flaga = true;
        for(unsigned long int k=0;k       if ( maskS[lb][k]== '1') {
        if (keyS[k] != data1[lb][k]) {
          flaga = false;
        }
          }
        }
        if (flaga)
          {
        return(lb);
          }
      }
     
      lb = -1;
      return(lb); 
     
    }
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
    {
     
      for(unsigned long int a=initialSt; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flag = true;
      for( la=initialSt; la<=finalSt; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flag = false;
          }
          }
        }
        if (flag)
          {
        return(la);
          }
      }
     
      la = -1;
      return(la); 
    }
    /*---------------------------------------------------------------------------------*/
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
    {
      for(unsigned long int a=initialSt+1; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
     
     
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
         
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flaga = true;
      for( lb=initialSt+1; lb<=finalSt; lb++) {
        flaga = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flaga = false;
          }
          }
        }
        if (flaga)
          {
        return(lb);
          }
      }
     
      lb = -1;
      return(lb); 
    }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runner(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      search(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
    /*-----------Thread2 function for searching without star key------------------*/
    void *runnera(void *numbersa)
    {
      struct arg_structa *argsa = (struct arg_structa *)numbersa;
      searchA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
      pthread_exit(0);
      }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runnerStar(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      searchStar(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
    void *runnerStara(void *numbersa)
    {
      struct arg_structa *argsa = (struct arg_structa *)numbersa;
      searchStarA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
      pthread_exit(0);
    }

Skipping comment line from keyboard via stdin

The requirement is to skip a line beginning from # sign. Basically, we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code, I have used multiple lines to skip a commented line which is in red colored text.

I wanted to know if it could be improved. As in, just one line of code to skip comment line. 

Please note: The comment line could be given anywhere from the keyboard.

#include
#include
#include
#include

using namespace std;

unsigned long int m;
unsigned long int n;
int res;
unsigned long int i, j, k, ctr, maxIndex;
long int l,la,lb;
string command, key,comment;
string indexS;
long int index, lo=0, hi,mid;
char key1[500];
char keyd[500];
char keym[500];
char data1[10000][500], maskS[10000][500];
char maskey[10000];
char maskSt[10000][500];
char data[10000][500];

void *runner(void *numbers);
void *runnera(void *numbersa);
void *runnerStar(void *numbers);
void *runnerStara(void *numbersa);
int readKey(string s); /* A function that returns 1 when '*' is found and returns 0 if '*' not found in key*/
void checkKey(string str);
void checkIndex(string strInd);
char StoC(string input);
void mErr(string eKey, unsigned long int mCheck);
long int search(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);

long int searchA(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);

/*---Structure for threads--*/
struct arg_struct {
  char numData[5000][500];
  char numKey[500];
  unsigned long int numM;
  unsigned long int numN;
  unsigned long int numLo;
  unsigned long int numMid;
};

struct arg_structa {
  char numaData[5000][500];
  char numaKey[500];
  unsigned long int numaM;
  unsigned long int numaN;
  unsigned long int numaMid;
  unsigned long int numaHi;
};

int main()
{
  
  cout << "Enter the command:"<< endl;
  cout << "Enter input  of the form:\n";
    cout << "(a) for insert(i) and replace(r) : i \n";
    cout << "(b) for delete(d)                : d \n";
    cout << "(c) for searching(s)             : s \n";
 
  cin >> command;
  const char *comnta = command.c_str();
 
  if(*comnta =='#') { /*Skipping comment line part*/
    cin.clear();
    getline(cin,comment);
    cin>>command;
    const char *comntb = command.c_str();
  
    if(*comntb =='#') { /*Skipping comment line part*/
      cin.clear();
      getline(cin,comment);
      cin>>command;
    }
    const char *comntc = command.c_str();
  
    if(*comntc =='#') { /*Skipping comment line part*/
      cin.clear();
      getline(cin,comment);
      cin>>command;
    }
  }
 
  try {
    if(command == "d") {
      cin >> indexS;
      const char *indexComm = indexS.c_str();

      if(*indexComm == '#') { /*Skipping comment line part*/
    cin.clear();
    getline(cin,comment);
    cin>>indexS;
    index = atoi(indexS.c_str());
    const char *indexComma = indexS.c_str();
    
    if(*indexComma == '#') { /*Skipping comment line part*/
      index = 0;
      cin.clear();
      getline(cin,comment);
      cin>>indexS;
      index = atoi(indexS.c_str());
    }
      }
      checkIndex(indexS);
      index  = atoi(indexS.c_str());
    }
   
    else if(command =="s") {
      cin >> key;
      const char *keyComm = key.c_str();
    
      if(*keyComm == '#') { /*Skipping comment line part*/
    cin.clear();
    getline(cin,comment);
    cin>> key;
    const char *keyComma = key.c_str();
   
    if(*keyComma == '#') { /*Skipping comment line part*/
      cin.clear();
      getline(cin,comment);
      cin>> key;
    }
      }
      checkKey(key);
    }
     
    else if(command == "i" || command == "r") {
      cin >> indexS;
     
      const char *indexCommc = indexS.c_str();
      if(*indexCommc == '#') { /*Skipping comment line part*/
    cin.clear();
    getline(cin,comment);
    cin>>indexS;
    index = atoi(indexS.c_str());
    const char *indexCommd = indexS.c_str();
   
    if(*indexCommd == '#') {
      index = 0;
      cin.clear();
      getline(cin,comment);
      cin>>indexS;
      index = atoi(indexS.c_str());
    }
      }
      checkIndex(indexS);
      index  = atoi(indexS.c_str());
      ctr = index;
     
      if(maxIndex < ctr)
    maxIndex = ctr;
     
      cin >> key;
      const char *keyCommb = key.c_str();
     
      if(*keyCommb == '#') {
    cin.clear();
    getline(cin,comment);
    cin>> key;
    const char *keyCommc = key.c_str();
   
    if(*keyCommc == '#') {
      cin.clear();
      getline(cin,comment);
      cin>> key;
    }
      }
      checkKey(key);
    }
   
    else
      throw(command);  
  }
 
  catch(string err) {
    cout << "The input must be of the form:\n";
    cout << "(a) for insert(i) and replace(r) : i \n";
    cout << "(b) for delete(d)                : d \n";
    cout << "(c) for searching(s)             : s \n";
    exit(0);
  }
 
  m = key.size();  /* Bit size of each entry of TCAM*/
  n = 10000;                     /*Number of entries in TCAM*/
 
  /*--------------------------------------------------------------------------------------*/
  int loop = 0;
  for(;;) {
    cout << "Enter input  of the form:\n";
    cout << "(a) for insert(i) and replace(r) : i \n";
    cout << "(b) for delete(d)                : d \n";
    cout << "(c) for searching(s)             : s \n";
    if(loop == 1) {
      cout << "Enter the command:\n";
      if (!(cin >> command))
    break;
      const char *comntd = command.c_str();
      if(*comntd =='#') {
    cin.clear();
    getline(cin,comment);
    cin>>command;
    const char *comntd = command.c_str();
   
    if(*comntd =='#') {
      cin.clear();
      getline(cin,comment);
      cin>>command;
    }
    const char *comnte = command.c_str();
   
    if(*comnte =='#') {
      cin.clear();
      getline(cin,comment);
      cin>>command;
    }
      }
     
      try {
    if(command == "d")
      {
        cin >> indexS;
       
        const char *indexCommd = indexS.c_str();
        if(*indexCommd == '#') {
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
          const char *indexComme = indexS.c_str();
         
          if(*indexComme == '#') {
        index = 0;
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
          }
        }
        checkIndex(indexS);
        index  = atoi(indexS.c_str());
      }
    else if(command =="s") {
      cin >> key;
      const char *keyCommd = key.c_str();
      if(*keyCommd == '#') {
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyComme = key.c_str();
        if(*keyComme == '#') {
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
      }
      checkKey(key);
      mErr(key, m);
    }
   
    else if(command == "i" || command == "r") {
      cin >> indexS;
      const char *indexCommf = indexS.c_str();
      
      if(*indexCommf == '#') {
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexCommg = indexS.c_str();
       
        if(*indexCommg == '#') {
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
      }
      checkIndex(indexS);
      index  = atoi(indexS.c_str());
      ctr = index;
     
      if(maxIndex < ctr)
        maxIndex = ctr;
     
      cin >> key;
      const char *keyCommf = key.c_str();
      if(*keyCommf == '#') {
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyCommg = key.c_str();
   
        if(*keyCommg == '#') {
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
      }
      checkKey(key);
      mErr(key, m);
    }  

    else
      throw(command);
      }
     
      catch(string err)    {
    cout << "The input must be of the form:\n";
    cout << "(a) for insert(i) and replace(r) : i \n";
    cout << "(b) for delete(d)                : d \n";
    cout << "(c) for searching(s)             : s \n";
    exit(0);
      }
    }
      loop =1;
      /*-------------Without '*' bits in the search key i.e res=0-------------------------------*/
      /*-------------------------------Begin searching part of the code------------------------*/
     
      readKey(key);
     
      if(command == "s" && res ==0)
    {
      StoC(key); /* returns key1 which is a char type*/
   
      pthread_t tid, tida;
      pthread_attr_t attr;
      pthread_attr_init(&attr);
     
      struct arg_struct args;
      struct arg_structa argsa;
      /*----Spliting up the indices of the TCAM for parallelism-----*/
      lo =0;
      hi =  maxIndex;
      mid = (lo+hi)/2;
     
      /*-----Assigning values to the data structure-------*/
      for(unsigned long int o=lo; o<=mid; o++)
        for(unsigned long int r=0; r           {
        args.numData[o][r] = data[o][r];
        args.numKey[r] = key1[r];

          }
      for(unsigned long int x=mid+1; x<=hi; x++)
        for(unsigned long int u=0; u           {
               argsa.numaData[x][u] = data[x][u];
               argsa.numaKey[u] = key1[u];
       
          }
     
        args.numM = m;
        args.numN = n;
        args.numLo = lo;
        args.numMid = mid;

               argsa.numaM = m;
        argsa.numaN = n;
        argsa.numaMid = mid;
        argsa.numaHi = hi;
          
        /*----Creating 2 threads to be executed------*/
        pthread_create(&tid, &attr, runner, (void *)&args);
             pthread_create(&tida, &attr, runnera, (void *)&argsa); /* thread for second half of data*/
       
        /*-------Waiting for both the thrads to complete execution-------*/
        pthread_join(tid, NULL);
        pthread_join(tida, NULL);
       
        /*------Printing ouput values based on lowest index match----------*/
        if(la ==-1 && lb==-1)
          cout<< la <        
        else if(la == -1 && lb >=0)
          cout<< lb <        
        else if(la >= 0 && lb ==-1)
          cout<< la <        
        else if(la>=0 && lb >=0)
          cout<< la <     }
     
     
      /*-----------------Search with '*' bits in the search key---------------------------------------------*/
      
      else if(command == "s" && res==1)
    {
      StoC(key);
      
      pthread_t thid, thida;
      pthread_attr_t attr;
      pthread_attr_init(&attr);
     
      struct arg_struct args;
      struct arg_structa argsa;
     
      lo =0;
      hi =  maxIndex;
      mid = (lo+hi)/2;

      for(unsigned long int o=lo; o<=mid; o++)
        for(unsigned long int r=0; r           {
        args.numData[o][r] = data[o][r];
        args.numKey[r] = key1[r];
          }
      for(unsigned long int x=mid+1; x<=hi; x++)
        for(unsigned long int u=0; u           {
               argsa.numaData[x][u] = data[x][u];
               argsa.numaKey[u] = key1[u];
          }
     
      args.numM = m;
      args.numN = n;
      args.numLo = lo;
      args.numMid = mid;
     
      argsa.numaM = m;
      argsa.numaN = n;
      argsa.numaMid = mid;
      argsa.numaHi = hi;
     
      /*--------creating thread for program-----------------*/
      pthread_create(&thid, &attr, runnerStar, (void *)&args);
      pthread_create(&thida, &attr, runnerStara, (void *)&argsa); /* thread for second half of data*/
     
      /*------Wait for both the threads to complete execution--------*/
      pthread_join(thid, NULL);
      pthread_join(thida, NULL);

        /*------Printing ouput values based on lowest index match----------*/
        if(la ==-1 && lb==-1)
          cout<< la <

        else if(la == -1 && lb >=0)
          cout<< lb <
       
        else if(la >= 0 && lb ==-1)
          cout<< la <

        else if(la>=0 && lb >=0)
          cout<< la <
    }
  
      /*-------------Inserting data at index-- i key 1011-----dataind[n+1][m]-------------------------*/
      else if(command =="i")
    {
      StoC(key);
      
      if((data[index][0] == '\0')) {
        for(unsigned long int bit=0; bit           data[index][bit] = key1[bit];
      }
      else {
        unsigned long int g = 0;
        for(g=(index+1);g           if(data[g][0] =='\0')
        break;
        }
        
        if(maxIndex < g)
          maxIndex = g;
       
        for( unsigned long int e=g; e>index; e--)
          for(unsigned long int biti=0; biti         data[e][biti] = data[e-1][biti];
       
        for( unsigned long int bit=0; bit           data[index][bit] = key1[bit];
      }
    }
      /*------------- delete index---------------------------*/
      else if(command =="d")
    {  
      i=0;
      for(i=index; i<=maxIndex;i++)
        for(int bitd=0; bitd           data[i][bitd]= data[i+1][bitd];
     
      if(maxIndex >= index && maxIndex !=0)
        maxIndex = maxIndex-1;
    }
     
      /*------------- replace index key---------------------------*/
     
      else if(command == "r")
    {  
      StoC(key);
      for(unsigned long int bitr=0; bitr         data[index][bitr] = key1[bitr];
    }
       cout<<"\n\n\n";
  cout<<<"tcam table="" entries="" without="" star\n";   // line();
  cout<<<"index"<<<"data"<<<"mask"<
  //line();
  for(unsigned long int k=0;k<=10;k++)
    { 
      cout<<<<<
    }
  //line();
  cout<<<

  }
 
  return 0;
}

/*----------------------Function definitions begin------------------*/
int readKey(string s)
{
  const char *p = s.c_str();
  while (*p != '\0') {
    if (*p == '*') {
      res = 1;
      return(res);
    }
    else {
      res = 0;
    }
    *p++;
  }
  return(res);
}

/*-------------------------------------------------*/
void checkKey(string str)
{
  const char *p = str.c_str();
  try {
    while (*p != '\0') {
      if(*p=='*' || *p=='0' || *p=='1')
    *p++;
      else
    throw(*p);
    }
  }
  catch( char error) {
    cout <<"ERROR: The input key must be in '1', '0', or '*'" << endl;
    exit(0);
  }    
}
/*-------------------------------------------------*/

void checkIndex(string strInd)
{
  const char *ind = strInd.c_str();
  try {
    while (*ind != '\0') {
      if(*ind=='0'|| *ind=='1'|| *ind=='2'|| *ind=='3'|| *ind =='4'|| *ind=='5'|| *ind=='6'|| *ind=='7'|| *ind=='8'|| *ind=='9')
    *ind++;
      else
    throw(*ind);
    }
  }
  catch( char error) {
    cout <<"ERROR: The input index must be a number" << endl;
    exit(0);
  }
}


/*----------------------------------------------------*/
char StoC(string input)
{
  i = 0; 
  const char *p = input.c_str();
  while (*p != '\0')
    {
      key1[i] = *p;
      i++;
      *p++;   
    }
  return(*key1);
}

 /*-----------------ERROR in key length-------------------------*/

void mErr(string eKey, unsigned long int mCheck)
{
  if(eKey.size() != mCheck) {
    cout<< "ERROR: Key size must not vary"<
    exit(0);
  }
}

/*---------------------------Search without * key-----------------*/

long int search(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
{
  /*---------------------Generating mask of the TCAM entries----------------------------*/
 
  for(unsigned long int i=initial; i<=final; i++)
    for(unsigned long int j=0; j       if(dataS[i][j] == '*')
    maskS[i][j]='0';
      else
    {
      data1[i][j] = dataS[i][j];
      maskS[i][j] = '1';
    }
       
  bool flag = true;
  for(la=initial; la<=final; la++) {
    flag = true;
    for(unsigned long int k=0;k       if ( maskS[la][k]== '1') {
    if (keyS[k] != data1[la][k])
      {
        flag = false;
      }
      }
    }
    if (flag)
      {
    return(la);
      }
  }
  la = -1;
  return(la); 
}

/*---------------------------Search without * key2-----------------*/
long int searchA(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
{
  /*---------------------Generating mask of the TCAM entries----------------------------*/
  for(unsigned long int u=initial+1; u<=final; u++)
    for(unsigned long int j=0; j       if(dataS[u][j] == '*')
    maskS[u][j]='0';
      else
    {
      data1[u][j] = dataS[u][j];
      maskS[u][j] = '1';
    }        
  bool flaga = true;
  for(lb=initial+1; lb<=final; lb++) {
    flaga = true;
    for(unsigned long int k=0;k       if ( maskS[lb][k]== '1') {
    if (keyS[k] != data1[lb][k]) {
      flaga = false;
    }
      }
    }
    if (flaga)
      {
    return(lb);
      }
  }
  
  lb = -1;
  return(lb);  
 
}
/*------------------------------Search with * key-------------------------------------------------------*/
long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
{
 
  for(unsigned long int a=initialSt; a<=finalSt; a++)
    for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
      else
    {
      data1[a][b] = dataSt[a][b];
      maskSt[a][b] = '1';
    }
  for(unsigned long int j=0;j
    {
      if(keySt[j] =='*')
    keym[j] = '0';
      else
    {  
      keyd[j] = keySt[j];
      keym[j] = '1';
    }
    }
  bool flag = true;
  for( la=initialSt; la<=finalSt; la++) {
    flag = true;
    for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
    if (keyd[k] != data1[la][k]  )
      {
        flag = false;
      }
      }
    }
    if (flag)
      {
    return(la);
      }
  }
 
  la = -1;
  return(la); 
}
/*---------------------------------------------------------------------------------*/
/*------------------------------Search with * key-------------------------------------------------------*/
long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
{
  for(unsigned long int a=initialSt+1; a<=finalSt; a++)
    for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
      else
    {
      data1[a][b] = dataSt[a][b];
      maskSt[a][b] = '1';
    }
 
 
  for(unsigned long int j=0;j
    {
      if(keySt[j] =='*')
    keym[j] = '0';
     
      else
    { 
      keyd[j] = keySt[j];
      keym[j] = '1';
    }
    }
  bool flaga = true;
  for( lb=initialSt+1; lb<=finalSt; lb++) {
    flaga = true;
    for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
    if (keyd[k] != data1[la][k]  )
      {
        flaga = false;
      }
      }
    }
    if (flaga)
      {
    return(lb);
      }
  }
 
  lb = -1;
  return(lb); 
}

/*-----------Thread1 function for searching without star key------------------*/
void *runner(void *numbers)
{
  struct arg_struct *args = (struct arg_struct *)numbers;
  search(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
  pthread_exit(0);
}

/*-----------Thread2 function for searching without star key------------------*/
void *runnera(void *numbersa)
{
  struct arg_structa *argsa = (struct arg_structa *)numbersa;
  searchA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
  pthread_exit(0);
  }

/*-----------Thread1 function for searching without star key------------------*/
void *runnerStar(void *numbers)
{
  struct arg_struct *args = (struct arg_struct *)numbers;
  searchStar(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
  pthread_exit(0);
}

void *runnerStara(void *numbersa)
{
  struct arg_structa *argsa = (struct arg_structa *)numbersa;
  searchStarA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
  pthread_exit(0);
}
added 26313 characters in body
Source Link

I have to write a code in c++ where the requirement is to skip a line beginning from '#' sign. So basically we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code i have used multiple lines to skip a commented line which is in red colored text. But i wanted to know if it could be improved. As in just one line of code to skip comment line. Please note: The comment line could be given anywhere from the keyboard. http://blog.yahoo.com/_6XO7JDO5HSRDEARZWNYHDRZVWA/articles/1264864/index

I have to write a code in c++ where the requirement is to skip a line beginning from '#' sign. So basically we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code i have used multiple lines to skip a commented line which is in red colored text. But i wanted to know if it could be improved. As in just one line of code to skip comment line. Please note: The comment line could be given anywhere from the keyboard.

 #include
    #include
    #include
    #include
    
    using namespace std;
    
    unsigned long int m;
    unsigned long int n;
    int res;
    unsigned long int i, j, k, ctr, maxIndex;
    long int l,la,lb;
    string command, key,comment;
    string indexS;
    long int index, lo=0, hi,mid;
    char key1[500];
    char keyd[500];
    char keym[500];
    char data1[10000][500], maskS[10000][500];
    char maskey[10000];
    char maskSt[10000][500];
    char data[10000][500];
    
    void *runner(void *numbers);
    void *runnera(void *numbersa);
    void *runnerStar(void *numbers);
    void *runnerStara(void *numbersa);
    int readKey(string s); /* A function that returns 1 when '*' is found and returns 0 if '*' not found in key*/
    void checkKey(string str);
    void checkIndex(string strInd);
    char StoC(string input);
    void mErr(string eKey, unsigned long int mCheck);
    long int search(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    long int searchA(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    /*---Structure for threads--*/
    struct arg_struct {
      char numData[5000][500];
      char numKey[500];
      unsigned long int numM;
      unsigned long int numN;
      unsigned long int numLo;
      unsigned long int numMid;
    };
    
    struct arg_structa {
      char numaData[5000][500];
      char numaKey[500];
      unsigned long int numaM;
      unsigned long int numaN;
      unsigned long int numaMid;
      unsigned long int numaHi;
    };
    
    int main()
    {
     
      cout << "Enter the command:"<< endl;
      cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
     
      cin >> command;
      const char *comnta = command.c_str();
     
      if(*comnta =='#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>command;
        const char *comntb = command.c_str();
      
        if(*comntb =='#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
        const char *comntc = command.c_str();
      
        if(*comntc =='#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
      }
     
      try {
        if(command == "d") {
          cin >> indexS;
          const char *indexComm = indexS.c_str();
    
          if(*indexComm == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexComma = indexS.c_str();
       
        if(*indexComma == '#') { /*Skipping comment line part*/
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
        }
       
        else if(command =="s") {
          cin >> key;
          const char *keyComm = key.c_str();
        
          if(*keyComm == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyComma = key.c_str();
       
        if(*keyComma == '#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
         
        else if(command == "i" || command == "r") {
          cin >> indexS;
         
          const char *indexCommc = indexS.c_str();
          if(*indexCommc == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexCommd = indexS.c_str();
       
        if(*indexCommd == '#') {
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
          ctr = index;
         
          if(maxIndex < ctr)
        maxIndex = ctr;
         
          cin >> key;
          const char *keyCommb = key.c_str();
         
          if(*keyCommb == '#') {
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyCommc = key.c_str();
       
        if(*keyCommc == '#') {
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
       
        else
          throw(command);  
      }
     
      catch(string err) {
        cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
      }
     
      m = key.size();  /* Bit size of each entry of TCAM*/
      n = 10000;                     /*Number of entries in TCAM*/
     
      /*--------------------------------------------------------------------------------------*/
      int loop = 0;
      for(;;) {
        cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        if(loop == 1) {
          cout << "Enter the command:\n";
          if (!(cin >> command))
        break;
          const char *comntd = command.c_str();
          if(*comntd =='#') {
        cin.clear();
        getline(cin,comment);
        cin>>command;
        const char *comntd = command.c_str();
       
        if(*comntd =='#') {
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
        const char *comnte = command.c_str();
       
        if(*comnte =='#') {
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
          }
         
          try {
        if(command == "d")
          {
            cin >> indexS;
           
            const char *indexCommd = indexS.c_str();
            if(*indexCommd == '#') {
              cin.clear();
              getline(cin,comment);
              cin>>indexS;
              index = atoi(indexS.c_str());
              const char *indexComme = indexS.c_str();
             
              if(*indexComme == '#') {
            index = 0;
            cin.clear();
            getline(cin,comment);
            cin>>indexS;
            index = atoi(indexS.c_str());
              }
            }
            checkIndex(indexS);
            index  = atoi(indexS.c_str());
          }
        else if(command =="s") {
          cin >> key;
          const char *keyCommd = key.c_str();
          if(*keyCommd == '#') {
            cin.clear();
            getline(cin,comment);
            cin>> key;
            const char *keyComme = key.c_str();
            if(*keyComme == '#') {
              cin.clear();
              getline(cin,comment);
              cin>> key;
            }
          }
          checkKey(key);
          mErr(key, m);
        }
       
        else if(command == "i" || command == "r") {
          cin >> indexS;
          const char *indexCommf = indexS.c_str();
         
          if(*indexCommf == '#') {
            cin.clear();
            getline(cin,comment);
            cin>>indexS;
            index = atoi(indexS.c_str());
            const char *indexCommg = indexS.c_str();
           
            if(*indexCommg == '#') {
              index = 0;
              cin.clear();
              getline(cin,comment);
              cin>>indexS;
              index = atoi(indexS.c_str());
            }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
          ctr = index;
         
          if(maxIndex < ctr)
            maxIndex = ctr;
         
          cin >> key;
          const char *keyCommf = key.c_str();
          if(*keyCommf == '#') {
            cin.clear();
            getline(cin,comment);
            cin>> key;
            const char *keyCommg = key.c_str();
       
            if(*keyCommg == '#') {
              cin.clear();
              getline(cin,comment);
              cin>> key;
            }
          }
          checkKey(key);
          mErr(key, m);
        }  
    
        else
          throw(command);
          }
         
          catch(string err)    {
        cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
          }
        }
          loop =1;
          /*-------------Without '*' bits in the search key i.e res=0-------------------------------*/
          /*-------------------------------Begin searching part of the code------------------------*/
         
          readKey(key);
         
          if(command == "s" && res ==0)
        {
          StoC(key); /* returns key1 which is a char type*/
       
          pthread_t tid, tida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
          /*----Spliting up the indices of the TCAM for parallelism-----*/
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
         
          /*-----Assigning values to the data structure-------*/
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
    
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
           
              }
         
            args.numM = m;
            args.numN = n;
            args.numLo = lo;
            args.numMid = mid;
    
                   argsa.numaM = m;
            argsa.numaN = n;
            argsa.numaMid = mid;
            argsa.numaHi = hi;
              
            /*----Creating 2 threads to be executed------*/
            pthread_create(&tid, &attr, runner, (void *)&args);
                 pthread_create(&tida, &attr, runnera, (void *)&argsa); /* thread for second half of data*/
           
            /*-------Waiting for both the thrads to complete execution-------*/
            pthread_join(tid, NULL);
            pthread_join(tida, NULL);
           
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <        
            else if(la == -1 && lb >=0)
              cout<< lb <        
            else if(la >= 0 && lb ==-1)
              cout<< la <        
            else if(la>=0 && lb >=0)
              cout<< la <     }
         
         
          /*-----------------Search with '*' bits in the search key---------------------------------------------*/
         
          else if(command == "s" && res==1)
        {
          StoC(key);
         
          pthread_t thid, thida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
         
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
    
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
              }
         
          args.numM = m;
          args.numN = n;
          args.numLo = lo;
          args.numMid = mid;
         
          argsa.numaM = m;
          argsa.numaN = n;
          argsa.numaMid = mid;
          argsa.numaHi = hi;
         
          /*--------creating thread for program-----------------*/
          pthread_create(&thid, &attr, runnerStar, (void *)&args);
          pthread_create(&thida, &attr, runnerStara, (void *)&argsa); /* thread for second half of data*/
         
          /*------Wait for both the threads to complete execution--------*/
          pthread_join(thid, NULL);
          pthread_join(thida, NULL);
    
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <
    
            else if(la == -1 && lb >=0)
              cout<< lb <
           
            else if(la >= 0 && lb ==-1)
              cout<< la <
    
            else if(la>=0 && lb >=0)
              cout<< la <
        }
     
          /*-------------Inserting data at index-- i key 1011-----dataind[n+1][m]-------------------------*/
          else if(command =="i")
        {
          StoC(key);
         
          if((data[index][0] == '\0')) {
            for(unsigned long int bit=0; bit           data[index][bit] = key1[bit];
          }
          else {
            unsigned long int g = 0;
            for(g=(index+1);g           if(data[g][0] =='\0')
            break;
            }
           
            if(maxIndex < g)
              maxIndex = g;
           
            for( unsigned long int e=g; e>index; e--)
              for(unsigned long int biti=0; biti         data[e][biti] = data[e-1][biti];
           
            for( unsigned long int bit=0; bit           data[index][bit] = key1[bit];
          }
        }
          /*------------- delete index---------------------------*/
          else if(command =="d")
        {  
          i=0;
          for(i=index; i<=maxIndex;i++)
            for(int bitd=0; bitd           data[i][bitd]= data[i+1][bitd];
         
          if(maxIndex >= index && maxIndex !=0)
            maxIndex = maxIndex-1;
        }
         
          /*------------- replace index key---------------------------*/
         
          else if(command == "r")
        {  
          StoC(key);
          for(unsigned long int bitr=0; bitr         data[index][bitr] = key1[bitr];
        }
           cout<<"\n\n\n";
      cout<<<"tcam table="" entries="" without="" star\n";   // line();
      cout<<<"index"<<<"data"<<<"mask"<
      //line();
      for(unsigned long int k=0;k<=10;k++)
        { 
          cout<<<<<
        }
      //line();
      cout<<<
    
      }
     
      return 0;
    }
    
    /*----------------------Function definitions begin------------------*/
    int readKey(string s)
    {
      const char *p = s.c_str();
      while (*p != '\0') {
        if (*p == '*') {
          res = 1;
          return(res);
        }
        else {
          res = 0;
        }
        *p++;
      }
      return(res);
    }
    
    /*-------------------------------------------------*/
    void checkKey(string str)
    {
      const char *p = str.c_str();
      try {
        while (*p != '\0') {
          if(*p=='*' || *p=='0' || *p=='1')
        *p++;
          else
        throw(*p);
        }
      }
      catch( char error) {
        cout <<"ERROR: The input key must be in '1', '0', or '*'" << endl;
        exit(0);
      }    
    }
    /*-------------------------------------------------*/
    
    void checkIndex(string strInd)
    {
      const char *ind = strInd.c_str();
      try {
        while (*ind != '\0') {
          if(*ind=='0'|| *ind=='1'|| *ind=='2'|| *ind=='3'|| *ind =='4'|| *ind=='5'|| *ind=='6'|| *ind=='7'|| *ind=='8'|| *ind=='9')
        *ind++;
          else
        throw(*ind);
        }
      }
      catch( char error) {
        cout <<"ERROR: The input index must be a number" << endl;
        exit(0);
      }
    }
    
    
    /*----------------------------------------------------*/
    char StoC(string input)
    {
      i = 0; 
      const char *p = input.c_str();
      while (*p != '\0')
        {
          key1[i] = *p;
          i++;
          *p++;   
        }
      return(*key1);
    }
    
     /*-----------------ERROR in key length-------------------------*/
    
    void mErr(string eKey, unsigned long int mCheck)
    {
      if(eKey.size() != mCheck) {
        cout<< "ERROR: Key size must not vary"<
        exit(0);
      }
    }
    
    /*---------------------------Search without * key-----------------*/
    
    long int search(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
     
      for(unsigned long int i=initial; i<=final; i++)
        for(unsigned long int j=0; j       if(dataS[i][j] == '*')
        maskS[i][j]='0';
          else
        {
          data1[i][j] = dataS[i][j];
          maskS[i][j] = '1';
        }
          
      bool flag = true;
      for(la=initial; la<=final; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskS[la][k]== '1') {
        if (keyS[k] != data1[la][k])
          {
            flag = false;
          }
          }
        }
        if (flag)
          {
        return(la);
          }
      }
      la = -1;
      return(la); 
    }
    
    /*---------------------------Search without * key2-----------------*/
    long int searchA(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
      for(unsigned long int u=initial+1; u<=final; u++)
        for(unsigned long int j=0; j       if(dataS[u][j] == '*')
        maskS[u][j]='0';
          else
        {
          data1[u][j] = dataS[u][j];
          maskS[u][j] = '1';
        }       
      bool flaga = true;
      for(lb=initial+1; lb<=final; lb++) {
        flaga = true;
        for(unsigned long int k=0;k       if ( maskS[lb][k]== '1') {
        if (keyS[k] != data1[lb][k]) {
          flaga = false;
        }
          }
        }
        if (flaga)
          {
        return(lb);
          }
      }
     
      lb = -1;
      return(lb); 
     
    }
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
    {
     
      for(unsigned long int a=initialSt; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flag = true;
      for( la=initialSt; la<=finalSt; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flag = false;
          }
          }
        }
        if (flag)
          {
        return(la);
          }
      }
     
      la = -1;
      return(la); 
    }
    /*---------------------------------------------------------------------------------*/
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
    {
      for(unsigned long int a=initialSt+1; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
     
     
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
         
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flaga = true;
      for( lb=initialSt+1; lb<=finalSt; lb++) {
        flaga = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flaga = false;
          }
          }
        }
        if (flaga)
          {
        return(lb);
          }
      }
     
      lb = -1;
      return(lb); 
    }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runner(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      search(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
    /*-----------Thread2 function for searching without star key------------------*/
    void *runnera(void *numbersa)
    {
      struct arg_structa *argsa = (struct arg_structa *)numbersa;
      searchA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
      pthread_exit(0);
      }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runnerStar(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      searchStar(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
    void *runnerStara(void *numbersa)
    {
      struct arg_structa *argsa = (struct arg_structa *)numbersa;
      searchStarA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
      pthread_exit(0);
    }

I have to write a code in c++ where the requirement is to skip a line beginning from '#' sign. So basically we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code i have used multiple lines to skip a commented line which is in red colored text. But i wanted to know if it could be improved. As in just one line of code to skip comment line. Please note: The comment line could be given anywhere from the keyboard. http://blog.yahoo.com/_6XO7JDO5HSRDEARZWNYHDRZVWA/articles/1264864/index

I have to write a code in c++ where the requirement is to skip a line beginning from '#' sign. So basically we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code i have used multiple lines to skip a commented line which is in red colored text. But i wanted to know if it could be improved. As in just one line of code to skip comment line. Please note: The comment line could be given anywhere from the keyboard.

 #include
    #include
    #include
    #include
    
    using namespace std;
    
    unsigned long int m;
    unsigned long int n;
    int res;
    unsigned long int i, j, k, ctr, maxIndex;
    long int l,la,lb;
    string command, key,comment;
    string indexS;
    long int index, lo=0, hi,mid;
    char key1[500];
    char keyd[500];
    char keym[500];
    char data1[10000][500], maskS[10000][500];
    char maskey[10000];
    char maskSt[10000][500];
    char data[10000][500];
    
    void *runner(void *numbers);
    void *runnera(void *numbersa);
    void *runnerStar(void *numbers);
    void *runnerStara(void *numbersa);
    int readKey(string s); /* A function that returns 1 when '*' is found and returns 0 if '*' not found in key*/
    void checkKey(string str);
    void checkIndex(string strInd);
    char StoC(string input);
    void mErr(string eKey, unsigned long int mCheck);
    long int search(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    long int searchA(char dataS[10000][500], char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final);
    long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt);
    
    /*---Structure for threads--*/
    struct arg_struct {
      char numData[5000][500];
      char numKey[500];
      unsigned long int numM;
      unsigned long int numN;
      unsigned long int numLo;
      unsigned long int numMid;
    };
    
    struct arg_structa {
      char numaData[5000][500];
      char numaKey[500];
      unsigned long int numaM;
      unsigned long int numaN;
      unsigned long int numaMid;
      unsigned long int numaHi;
    };
    
    int main()
    {
     
      cout << "Enter the command:"<< endl;
      cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
     
      cin >> command;
      const char *comnta = command.c_str();
     
      if(*comnta =='#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>command;
        const char *comntb = command.c_str();
      
        if(*comntb =='#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
        const char *comntc = command.c_str();
      
        if(*comntc =='#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
      }
     
      try {
        if(command == "d") {
          cin >> indexS;
          const char *indexComm = indexS.c_str();
    
          if(*indexComm == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexComma = indexS.c_str();
       
        if(*indexComma == '#') { /*Skipping comment line part*/
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
        }
       
        else if(command =="s") {
          cin >> key;
          const char *keyComm = key.c_str();
        
          if(*keyComm == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyComma = key.c_str();
       
        if(*keyComma == '#') { /*Skipping comment line part*/
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
         
        else if(command == "i" || command == "r") {
          cin >> indexS;
         
          const char *indexCommc = indexS.c_str();
          if(*indexCommc == '#') { /*Skipping comment line part*/
        cin.clear();
        getline(cin,comment);
        cin>>indexS;
        index = atoi(indexS.c_str());
        const char *indexCommd = indexS.c_str();
       
        if(*indexCommd == '#') {
          index = 0;
          cin.clear();
          getline(cin,comment);
          cin>>indexS;
          index = atoi(indexS.c_str());
        }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
          ctr = index;
         
          if(maxIndex < ctr)
        maxIndex = ctr;
         
          cin >> key;
          const char *keyCommb = key.c_str();
         
          if(*keyCommb == '#') {
        cin.clear();
        getline(cin,comment);
        cin>> key;
        const char *keyCommc = key.c_str();
       
        if(*keyCommc == '#') {
          cin.clear();
          getline(cin,comment);
          cin>> key;
        }
          }
          checkKey(key);
        }
       
        else
          throw(command);  
      }
     
      catch(string err) {
        cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
      }
     
      m = key.size();  /* Bit size of each entry of TCAM*/
      n = 10000;                     /*Number of entries in TCAM*/
     
      /*--------------------------------------------------------------------------------------*/
      int loop = 0;
      for(;;) {
        cout << "Enter input  of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        if(loop == 1) {
          cout << "Enter the command:\n";
          if (!(cin >> command))
        break;
          const char *comntd = command.c_str();
          if(*comntd =='#') {
        cin.clear();
        getline(cin,comment);
        cin>>command;
        const char *comntd = command.c_str();
       
        if(*comntd =='#') {
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
        const char *comnte = command.c_str();
       
        if(*comnte =='#') {
          cin.clear();
          getline(cin,comment);
          cin>>command;
        }
          }
         
          try {
        if(command == "d")
          {
            cin >> indexS;
           
            const char *indexCommd = indexS.c_str();
            if(*indexCommd == '#') {
              cin.clear();
              getline(cin,comment);
              cin>>indexS;
              index = atoi(indexS.c_str());
              const char *indexComme = indexS.c_str();
             
              if(*indexComme == '#') {
            index = 0;
            cin.clear();
            getline(cin,comment);
            cin>>indexS;
            index = atoi(indexS.c_str());
              }
            }
            checkIndex(indexS);
            index  = atoi(indexS.c_str());
          }
        else if(command =="s") {
          cin >> key;
          const char *keyCommd = key.c_str();
          if(*keyCommd == '#') {
            cin.clear();
            getline(cin,comment);
            cin>> key;
            const char *keyComme = key.c_str();
            if(*keyComme == '#') {
              cin.clear();
              getline(cin,comment);
              cin>> key;
            }
          }
          checkKey(key);
          mErr(key, m);
        }
       
        else if(command == "i" || command == "r") {
          cin >> indexS;
          const char *indexCommf = indexS.c_str();
         
          if(*indexCommf == '#') {
            cin.clear();
            getline(cin,comment);
            cin>>indexS;
            index = atoi(indexS.c_str());
            const char *indexCommg = indexS.c_str();
           
            if(*indexCommg == '#') {
              index = 0;
              cin.clear();
              getline(cin,comment);
              cin>>indexS;
              index = atoi(indexS.c_str());
            }
          }
          checkIndex(indexS);
          index  = atoi(indexS.c_str());
          ctr = index;
         
          if(maxIndex < ctr)
            maxIndex = ctr;
         
          cin >> key;
          const char *keyCommf = key.c_str();
          if(*keyCommf == '#') {
            cin.clear();
            getline(cin,comment);
            cin>> key;
            const char *keyCommg = key.c_str();
       
            if(*keyCommg == '#') {
              cin.clear();
              getline(cin,comment);
              cin>> key;
            }
          }
          checkKey(key);
          mErr(key, m);
        }  
    
        else
          throw(command);
          }
         
          catch(string err)    {
        cout << "The input must be of the form:\n";
        cout << "(a) for insert(i) and replace(r) : i \n";
        cout << "(b) for delete(d)                : d \n";
        cout << "(c) for searching(s)             : s \n";
        exit(0);
          }
        }
          loop =1;
          /*-------------Without '*' bits in the search key i.e res=0-------------------------------*/
          /*-------------------------------Begin searching part of the code------------------------*/
         
          readKey(key);
         
          if(command == "s" && res ==0)
        {
          StoC(key); /* returns key1 which is a char type*/
       
          pthread_t tid, tida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
          /*----Spliting up the indices of the TCAM for parallelism-----*/
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
         
          /*-----Assigning values to the data structure-------*/
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
    
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
           
              }
         
            args.numM = m;
            args.numN = n;
            args.numLo = lo;
            args.numMid = mid;
    
                   argsa.numaM = m;
            argsa.numaN = n;
            argsa.numaMid = mid;
            argsa.numaHi = hi;
              
            /*----Creating 2 threads to be executed------*/
            pthread_create(&tid, &attr, runner, (void *)&args);
                 pthread_create(&tida, &attr, runnera, (void *)&argsa); /* thread for second half of data*/
           
            /*-------Waiting for both the thrads to complete execution-------*/
            pthread_join(tid, NULL);
            pthread_join(tida, NULL);
           
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <        
            else if(la == -1 && lb >=0)
              cout<< lb <        
            else if(la >= 0 && lb ==-1)
              cout<< la <        
            else if(la>=0 && lb >=0)
              cout<< la <     }
         
         
          /*-----------------Search with '*' bits in the search key---------------------------------------------*/
         
          else if(command == "s" && res==1)
        {
          StoC(key);
         
          pthread_t thid, thida;
          pthread_attr_t attr;
          pthread_attr_init(&attr);
         
          struct arg_struct args;
          struct arg_structa argsa;
         
          lo =0;
          hi =  maxIndex;
          mid = (lo+hi)/2;
    
          for(unsigned long int o=lo; o<=mid; o++)
            for(unsigned long int r=0; r           {
            args.numData[o][r] = data[o][r];
            args.numKey[r] = key1[r];
              }
          for(unsigned long int x=mid+1; x<=hi; x++)
            for(unsigned long int u=0; u           {
                   argsa.numaData[x][u] = data[x][u];
                   argsa.numaKey[u] = key1[u];
              }
         
          args.numM = m;
          args.numN = n;
          args.numLo = lo;
          args.numMid = mid;
         
          argsa.numaM = m;
          argsa.numaN = n;
          argsa.numaMid = mid;
          argsa.numaHi = hi;
         
          /*--------creating thread for program-----------------*/
          pthread_create(&thid, &attr, runnerStar, (void *)&args);
          pthread_create(&thida, &attr, runnerStara, (void *)&argsa); /* thread for second half of data*/
         
          /*------Wait for both the threads to complete execution--------*/
          pthread_join(thid, NULL);
          pthread_join(thida, NULL);
    
            /*------Printing ouput values based on lowest index match----------*/
            if(la ==-1 && lb==-1)
              cout<< la <
    
            else if(la == -1 && lb >=0)
              cout<< lb <
           
            else if(la >= 0 && lb ==-1)
              cout<< la <
    
            else if(la>=0 && lb >=0)
              cout<< la <
        }
     
          /*-------------Inserting data at index-- i key 1011-----dataind[n+1][m]-------------------------*/
          else if(command =="i")
        {
          StoC(key);
         
          if((data[index][0] == '\0')) {
            for(unsigned long int bit=0; bit           data[index][bit] = key1[bit];
          }
          else {
            unsigned long int g = 0;
            for(g=(index+1);g           if(data[g][0] =='\0')
            break;
            }
           
            if(maxIndex < g)
              maxIndex = g;
           
            for( unsigned long int e=g; e>index; e--)
              for(unsigned long int biti=0; biti         data[e][biti] = data[e-1][biti];
           
            for( unsigned long int bit=0; bit           data[index][bit] = key1[bit];
          }
        }
          /*------------- delete index---------------------------*/
          else if(command =="d")
        {  
          i=0;
          for(i=index; i<=maxIndex;i++)
            for(int bitd=0; bitd           data[i][bitd]= data[i+1][bitd];
         
          if(maxIndex >= index && maxIndex !=0)
            maxIndex = maxIndex-1;
        }
         
          /*------------- replace index key---------------------------*/
         
          else if(command == "r")
        {  
          StoC(key);
          for(unsigned long int bitr=0; bitr         data[index][bitr] = key1[bitr];
        }
           cout<<"\n\n\n";
      cout<<<"tcam table="" entries="" without="" star\n";   // line();
      cout<<<"index"<<<"data"<<<"mask"<
      //line();
      for(unsigned long int k=0;k<=10;k++)
        { 
          cout<<<<<
        }
      //line();
      cout<<<
    
      }
     
      return 0;
    }
    
    /*----------------------Function definitions begin------------------*/
    int readKey(string s)
    {
      const char *p = s.c_str();
      while (*p != '\0') {
        if (*p == '*') {
          res = 1;
          return(res);
        }
        else {
          res = 0;
        }
        *p++;
      }
      return(res);
    }
    
    /*-------------------------------------------------*/
    void checkKey(string str)
    {
      const char *p = str.c_str();
      try {
        while (*p != '\0') {
          if(*p=='*' || *p=='0' || *p=='1')
        *p++;
          else
        throw(*p);
        }
      }
      catch( char error) {
        cout <<"ERROR: The input key must be in '1', '0', or '*'" << endl;
        exit(0);
      }    
    }
    /*-------------------------------------------------*/
    
    void checkIndex(string strInd)
    {
      const char *ind = strInd.c_str();
      try {
        while (*ind != '\0') {
          if(*ind=='0'|| *ind=='1'|| *ind=='2'|| *ind=='3'|| *ind =='4'|| *ind=='5'|| *ind=='6'|| *ind=='7'|| *ind=='8'|| *ind=='9')
        *ind++;
          else
        throw(*ind);
        }
      }
      catch( char error) {
        cout <<"ERROR: The input index must be a number" << endl;
        exit(0);
      }
    }
    
    
    /*----------------------------------------------------*/
    char StoC(string input)
    {
      i = 0; 
      const char *p = input.c_str();
      while (*p != '\0')
        {
          key1[i] = *p;
          i++;
          *p++;   
        }
      return(*key1);
    }
    
     /*-----------------ERROR in key length-------------------------*/
    
    void mErr(string eKey, unsigned long int mCheck)
    {
      if(eKey.size() != mCheck) {
        cout<< "ERROR: Key size must not vary"<
        exit(0);
      }
    }
    
    /*---------------------------Search without * key-----------------*/
    
    long int search(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
     
      for(unsigned long int i=initial; i<=final; i++)
        for(unsigned long int j=0; j       if(dataS[i][j] == '*')
        maskS[i][j]='0';
          else
        {
          data1[i][j] = dataS[i][j];
          maskS[i][j] = '1';
        }
          
      bool flag = true;
      for(la=initial; la<=final; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskS[la][k]== '1') {
        if (keyS[k] != data1[la][k])
          {
            flag = false;
          }
          }
        }
        if (flag)
          {
        return(la);
          }
      }
      la = -1;
      return(la); 
    }
    
    /*---------------------------Search without * key2-----------------*/
    long int searchA(char dataS[10000][500],char keyS[500], unsigned long int mS, unsigned long int nS, unsigned long int initial, unsigned long int final)
    {
      /*---------------------Generating mask of the TCAM entries----------------------------*/
      for(unsigned long int u=initial+1; u<=final; u++)
        for(unsigned long int j=0; j       if(dataS[u][j] == '*')
        maskS[u][j]='0';
          else
        {
          data1[u][j] = dataS[u][j];
          maskS[u][j] = '1';
        }       
      bool flaga = true;
      for(lb=initial+1; lb<=final; lb++) {
        flaga = true;
        for(unsigned long int k=0;k       if ( maskS[lb][k]== '1') {
        if (keyS[k] != data1[lb][k]) {
          flaga = false;
        }
          }
        }
        if (flaga)
          {
        return(lb);
          }
      }
     
      lb = -1;
      return(lb); 
     
    }
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStar(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
    {
     
      for(unsigned long int a=initialSt; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flag = true;
      for( la=initialSt; la<=finalSt; la++) {
        flag = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flag = false;
          }
          }
        }
        if (flag)
          {
        return(la);
          }
      }
     
      la = -1;
      return(la); 
    }
    /*---------------------------------------------------------------------------------*/
    /*------------------------------Search with * key-------------------------------------------------------*/
    long int searchStarA(char dataSt[10000][500],char keySt[500], unsigned long int mSt, unsigned long int nSt, unsigned long int initialSt, unsigned long int finalSt)
    {
      for(unsigned long int a=initialSt+1; a<=finalSt; a++)
        for(unsigned long int b=0; b       if(dataSt[a][b] == '*') maskSt[a][b]='0';
          else
        {
          data1[a][b] = dataSt[a][b];
          maskSt[a][b] = '1';
        }
     
     
      for(unsigned long int j=0;j
        {
          if(keySt[j] =='*')
        keym[j] = '0';
         
          else
        { 
          keyd[j] = keySt[j];
          keym[j] = '1';
        }
        }
      bool flaga = true;
      for( lb=initialSt+1; lb<=finalSt; lb++) {
        flaga = true;
        for(unsigned long int k=0;k       if ( maskSt[la][k]== '1' && keym[k]=='1') {
        if (keyd[k] != data1[la][k]  )
          {
            flaga = false;
          }
          }
        }
        if (flaga)
          {
        return(lb);
          }
      }
     
      lb = -1;
      return(lb); 
    }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runner(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      search(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
    /*-----------Thread2 function for searching without star key------------------*/
    void *runnera(void *numbersa)
    {
      struct arg_structa *argsa = (struct arg_structa *)numbersa;
      searchA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
      pthread_exit(0);
      }
    
    /*-----------Thread1 function for searching without star key------------------*/
    void *runnerStar(void *numbers)
    {
      struct arg_struct *args = (struct arg_struct *)numbers;
      searchStar(args-> numData, args-> numKey, args-> numM, args-> numN, args-> numLo, args-> numMid);
      pthread_exit(0);
    }
    
    void *runnerStara(void *numbersa)
    {
      struct arg_structa *argsa = (struct arg_structa *)numbersa;
      searchStarA(argsa-> numaData, argsa-> numaKey, argsa-> numaM, argsa-> numaN, argsa-> numaMid, argsa-> numaHi);
      pthread_exit(0);
    }
Source Link

skipping comment line from keyboard (stdin)

I have to write a code in c++ where the requirement is to skip a line beginning from '#' sign. So basically we need to treat it as a comment line. The input is given by the user from the keyboard. I will not be reading it from a file. I have a link to the code that i have written so far. In this code i have used multiple lines to skip a commented line which is in red colored text. But i wanted to know if it could be improved. As in just one line of code to skip comment line. Please note: The comment line could be given anywhere from the keyboard. http://blog.yahoo.com/_6XO7JDO5HSRDEARZWNYHDRZVWA/articles/1264864/index