Thread: how can i write a function with a map as an argument?

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    how can i write a function with a map as an argument?

    hello all, im trying to make my progam smaller by making some function . one of them needs to have 2 map and a char * token , (for searching ) as arguments!
    sth like this Search(map1<>, map2<>,char *)
    .well i tried , but i couldnt make it my self. dear laiserlight gave me this "
    Code:
    Code:
    template<typename K, typename V>
    class MatchSecond
    {
    public:
            MatchSecond(const V& value) : value(value) {}
    
            bool operator()(const std::pair<K,V>& element) const
            {
                    return element.second == value;
            }
    private:
        V value;
    };
    i made this a header file (searcher.h) included this to my project , then i used them in such a way to search in maps!
    (LABEL and declaration , are located in "CLA.h"(private session!) .
    Code:
    Code:
    ///###################################### Search in LABEL and DECLARATIONS ################################
    
                                    int decimal_retrieved_number = 0;
                                    int converted_org = 0;
                                    int converted_octal_number = 0;
    
                                    char * octalstring;
                                    char * ptr;
    
                                    map<int,string>::iterator iter = find_if(LABEL.begin(), LABEL.end(), MatchSecond<int,string>(token));
                                                                              //till you havent found the token in label, go on .
                                    if (iter != LABEL.end())
                                    {                                        //if you found it retrieve the number.
    ///Attention: converted_org may be "0", because of local scope limitation. if there is an error , remmember to manually convert org {maybe form orgsafe)to decimal and then add it to retrieved number.
                                            decimal_retrieved_number = iter->first;
    
                                            converted_org = Oct2Dec(org);   //convert org from octal to decimal
    
    
                                            decimal_retrieved_number += converted_org;               //adds the org number to the retrieved line number , to get the memory location.(after this step, the result must be converted to octal.and then be saved to memory location.)
                                            octalstring = Dec2Oct(decimal_retrieved_number);
    
                                            converted_octal_number = atoi(octalstring);
    
                                            offsetptr = octalstring; //save the octal string , to offsetptr (this is the value that our generic label represented).
    
                                   }      ///end of LABEL searching.
                    //else if you couldn't find it in label, search the token in declaration until you find it
    ///if there is a problem here , uncomment the line below , and change the iter to iter2.
    
                  map<int,string>::iterator iter2 = find_if(declaration.begin(), declaration.end(), MatchSecond<int,string>(token));
    
                                     if (iter2 != declaration.end()) ///remember if you uncomment the above line , make this "iter2."
                                         {
                                                  int temp;
                                                  char * converted;
                                                  string message;
    
                                                  temp = iter2->first; ///check to see if it is octal. if it is not we convert it to octal and generate a warning.
    
    
                                                  message = Oct2Bin(temp);//lets see if this returned value is octal or not
    
                                                  if (message == "no")
                                                  {
                                                          converted = Dec2Oct(temp);
                                                          cout<<"conversion occurred!"<<" line "<<currentline;
    
                                                          // we can use this statement >>( offsetptr = itoa(temp,ptr,8);  ) instead of Dec2oct(temp);
                                                          offsetptr = converted; //copy the octal string in offsetptr
    
    
                                                  }
                                            else if( message != "no" )
                                                  {
                                                               //if it was octal . well do the routine .copy it to offset,convert it to binary
                                                               char * oo;
                                                               char * o;
                                                               o = itoa(temp,oo,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
                                                               cout<<o;
                                                               offsetptr = o;
    
                                                  }
                                         }//end of else if (ite!=dclaration.end())
    
    ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! End of Search in LABEL and DECLARATIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    its doing its job pretty great . tanx a billion to laiserlight , but as you see, there are more than 20 cases in which i have to search in maps, so this made my program really big in a manner of lines !
    so i managed to make the above codes into a the "below codes,(and i would add "search.h " to let it work.
    Code:
    ///###################################### Search in LABEL and DECLARATIONS ################################
    
    
    
    
    
    
    
    
    char * search(map1<>,map2<>,char * token)
    {
            int decimal_retrieved_number = 0;
            int converted_org = 0;
            int converted_octal_number = 0;
    
            char * octalstring;
            char * ptr;
            char * offsetptr;
    
            map<int,string>::iterator iter = find_if(K.begin(), K.end(), MatchSecond<int,string>(token));
                                                                              //till you havent found the token in label, go on .
            if (iter != K.end())
            {                                        //if you found it retrieve the number.
    ///Attention: converted_org may be "0", because of local scope limitation. if there is an error , remmember to manually convert org {maybe form orgsafe)to decimal and then add it to retrieved number.
            decimal_retrieved_number = iter->first;
    
            converted_org = Oct2Dec(org);   //convert org from octal to decimal
    
            decimal_retrieved_number += converted_org;               //adds the org number to the retrieved line number , to get the memory location.(after this step, the result must be converted to octal.and then be saved to memory location.)
            octalstring = Dec2Oct(decimal_retrieved_number);
    
            converted_octal_number = atoi(octalstring);
    
            offsetptr = octalstring; //save the octal string , to offsetptr (this is the value that our generic label represented).
    
            }   ///end of LABEL searching.
             //else if you couldnt find it in label, search the token in declaration untill you find it
    ///if there is a problem here , uncomment the line below , and change the iter to iter2.
    
            map<int,string>::iterator iter2 = find_if(V.begin(), V.end(), MatchSecond<int,string>(token));
    
            if (iter2 != V.end()) ///remmember if you uncomment the above line , make this "iter2."
            {
                    int temp;
                    char * converted;
                    string message;
    
                    temp = iter2->first; ///check to see if it is octal. if it is not we convert it to octal and generate a warning.
    
                    message = Oct2Bin(temp);//lets see if this returned value is octal or not
    
                    if (message == "no")
                    {
                            converted = Dec2Oct(temp);
                            cout<<"conversion occured!"<<" line "<<currentline;
    
                            // we can use this statement >>( offsetptr = itoa(temp,ptr,8);  ) instead of Dec2oct(temp);
                            offsetptr = converted; //copy the octal string in offsetptr
    
    
                    }
                    else if( message != "no" )
                    {
                            //if it was octal . well do the routin .copy it to offset,convert it to binary
                            char * oo;
                            char * o;
                            o = itoa(temp,oo,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
                            offsetptr = o;//itoa(temp,ptr,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
    
                    }
             }//end of declaraion if
    return offsetptr;
            }//end of function
    ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! End of Search in LABEL and DECLARATIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    but , there were error, first i need to find a way to send the maps as an argument , to be able to get the offset. and another is whenever i include other function headers( such as "Oct2Dec.h" it says , redeclaration and stuff! so i managed to include these functions to the search function too , so that i m not to include their respective headerfiles.

    anyway , can you please help me on this ? !
    Last edited by Masterx; 01-27-2009 at 01:23 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    one of them needs to have 2 map and a char * token , (for searching ) as arguments!
    sth like this Search(map1<>, map2<>,char *)
    You need to decide what are the key and mapped types of the map, or you would need a function template instead. In other words, I expect a function signature along the lines of:
    Code:
    char* search(const std::map<int, std::string>& map1, const std::map<int, std::string>& map2, char* token);
    Quote Originally Posted by Masterx
    but as you see, there are more than 20 cases in which i have to search in maps, so this made my program really big in a manner of lines !
    so i managed to make the above codes into a the "below codes,(and i would add "search.h " to let it work.
    Note that searching a map by its mapped type requires linear search. Searching a map by its key type takes advantage of the structure of the map such that it is comparable to a binary search. It seems strange to me that you are extensively searching the map by its mapped type rather than by its key. Are you sure you are mapping in the correct direction?

    Incidentally, why are you using char*? It looks like you should use std::string instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    You need to decide what are the key and mapped types of the map, or you would need a function template instead. In other words, I expect a function signature along the lines of:
    Code:
    char* search(const std::map<int, std::string>& map1, const std::map<int, std::string>& map2, char* token);
    Note that searching a map by its mapped type requires linear search. Searching a map by its key type takes advantage of the structure of the map such that it is comparable to a binary search. It seems strange to me that you are extensively searching the map by its mapped type rather than by its key. Are you sure you are mapping in the correct direction?

    Incidentally, why are you using char*? It looks like you should use std::string instead.
    many tanx dear laiserlight. about the search . the key value is the first one as integer type. (because i used the map as map[currentline] = mappedstring! this made me search like this . if it is not efficient i can reverse it , and thus searching would be reveresed ! by just writting iter->firsr instead of iter->second. ? !Iwithout any other modification? .( so by going this way , i should write map[mappedstring] = currentline , yes? )

    and another thing, if i want to use this function (call it ) , can i simple write"
    Search(map1,map2,token) or the shape of calling differs too? (eg. it may be sth like this ?! Search(map1<int,string>,map2<int,string>,token) ?

    about char * return type, i did this to avoid further coding to convert it to integer!(from string type to integer. )

    many many tanx Dear laiserlight.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    about the search . the key value is the first one as integer type. (because i used the map as map[currentline] = mappedstring! this made me search like this . if it is not efficient i can reverse it , and thus searching would be reveresed ! by just writting iter->firsr instead of iter->second. ? !Iwithout any other modification? .( so by going this way , i should write map[mappedstring] = currentline , yes? )
    Yes, swapping the key and mapped value makes sense here since you are searching for a string to get the corresponding integer, hence making the string the key and the integer the mapped value is appropriate. To take advantage of the faster searching you would use std::map's own find() member function instead of the std::find_if() generic algorithm, e.g.,
    Code:
    map<string, int>::iterator iter = map1.find(token);
    Quote Originally Posted by Masterx
    and another thing, if i want to use this function (call it ) , can i simple write"
    Search(map1,map2,token) or the shape of calling differs too? (eg. it may be sth like this ?! Search(map1<int,string>,map2<int,string>,token) ?
    The syntax of calling the function is like what you are used to. In fact, the syntax of declaring the function is the same.

    Quote Originally Posted by Masterx
    about char * return type, i did this to avoid further coding to convert it to integer!(from string type to integer. )
    You have to convert to integer either way, so I am not sure what you think you are avoiding since you are working with a string in either case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    tanx ,
    i called the function this way .. offsetptr = search(LABEL,declaration,token,& org,& currentline)
    (i added both to the protype ) . it seems everything goes fine , but there is an error which i dont understan!
    it says "
    Code:
    |21|error: conversion from `std::_Rb_tree_const_iterator<std::pair<const int, std::string> >' to non-scalar type `std::_Rb_tree_iterator<std::pair<const int, std::string> >' requested
    |
    line 21 is (
    Code:
     map<int,string>::iterator iter = find_if(map1.begin(), map1.end(), MatchSecond<int,string>(token));
    the source code so far is ( i havent swaped them yet, still wana check it first and then edit the whole cla.cpp):
    Code:
    //In The name of God
    ///###################################### Search in LABEL and DECLARATIONS ################################
    #include "Searcher.h"
     #include <string>
     #include <cstring>
     #include <cctype>
     #include <iostream>
     using namespace std;
     
     int OcttoDec(int );
     char* DectoOct(int );
     string OcttoBin(int );
    
    char* search(const map<int, string>& map1, const map<int, string>& map2, char* token,int * pointer,int * clptr)
    {
            int decimal_retrieved_number = 0;
            int converted_org = 0;
            int converted_octal_number = 0;
    
            char * octalstring;
            char * ptr;
            char * offsetptr;
    
            map<int,string>::iterator iter = find_if(map1.begin(), map1.end(), MatchSecond<int,string>(token));
                                                                              //till you havent found the token in label, go on .
            if (iter != map1.end())
            {                                        //if you found it retrieve the number.
    ///Attention: converted_org may be "0", because of local scope limitation. 
    ///if there is an error , remmember to manually convert org {maybe form orgsafe)to decimal and then add it to retrieved number.
            decimal_retrieved_number = iter->first;
    
            converted_org = OcttoDec(* pointer);   //convert org from octal to decimal
    
            decimal_retrieved_number += converted_org;               //adds the org number to the retrieved line number ,
                                                                    //to get the memory location.(after this step, the result must be converted to octal.
                                                                    //and then be saved to memory location.)
            octalstring = DectoOct(decimal_retrieved_number);
    
            converted_octal_number = atoi(octalstring);
    
            offsetptr = octalstring; //save the octal string , to offsetptr (this is the value that our generic label represented).
    
            }   ///end of LABEL searching.
             //else if you couldnt find it in label, search the token in declaration untill you find it
    ///if there is a problem here , uncomment the line below , and change the iter to iter2.
    
            map<int,string>::iterator iter2 = find_if(map2.begin(), map2.end(), MatchSecond<int,string>(token));
    
            if (iter2 != map2.end()) ///remmember if you uncomment the above line , make this "iter2."
            {
                    int temp;
                    char * converted;
                    string message;
    
                    temp = iter2->first; ///check to see if it is octal. if it is not we convert it to octal and generate a warning.
    
                    message = OcttoBin(temp);//lets see if this returned value is octal or not
    
                    if (message == "no")
                    {
                            converted = DectoOct(temp);
                            cout<<"conversion occured!"<<" line "<<*clptr;
    
                            // we can use this statement >>( offsetptr = itoa(temp,ptr,8);  ) instead of Dec2oct(temp);
                            offsetptr = converted; //copy the octal string in offsetptr
    
    
                    }
                    else if( message != "no" )
                    {
                            //if it was octal . well do the routin .copy it to offset,convert it to binary
                            char * oo;
                            char * o;
                            o = itoa(temp,oo,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
                            offsetptr = o;//itoa(temp,ptr,8);    //itoa(number,a buffer to hold the string,the base you want the number to be saved in buffer)
    
                    }
             }//end of declaraion if
    return offsetptr;
            }//end of function
    ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! End of Search in LABEL and DECLARATIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    int OcttoDec(int oct)
    {
            int n,r,s=0,i;
            n=oct;
            double S=0;
            for(i=0;n!=0;i++)
             {
                     r=n%10;
                     s=s + r * ((pow(8.0,i)));
                     n=n/10;
             }
            return static_cast<int>(s);
    }
    
    
    char* DectoOct(int dec)
    {
            using namespace std;
            char *rtn;
            int prev, len, temp, *OCT;
            prev = dec;
            for(int i=0;prev!=0;i++)
            {
                    prev = prev/8;
                    len=i;
            }
           OCT = new int[len+1];
           prev = dec;
           for(int i=0;prev!=0;i++)
            {
                    OCT[i] = prev%8;
                    prev = prev/8;
                    len=i;
            }
          OCT[len+1] = prev%8;
          len++;
          for(int i=0;i<len/2;i++)
           {
                   temp=OCT[i];
                   OCT[i]=OCT[len-1-i];
                   OCT[len-1-i]=temp;
            }
    
         rtn = new char[len+1];
    
         for(int i=0;i<len;i++)
          {
                  rtn[i]=OCT[i]+'0';
                  rtn[i+1]='\0';
          }
    
          return rtn;
    
    }
    
    
    string OcttoBin(int n)
    {
    
    
             int a[6],i=0,t=0,num;
             string str,str1,str2,s;
    
             char buffer[9];
             char * ptr, * not_octal;
             string str_array[9];
             const size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
    
             while(n!=0)
             {
                     a[i]=n%10;
                     n=n/10;
                     if(a[i]>7)
                     t=1;
                     i++;
             }
             i--;
             if(t==0)
              for(;i>=0;i--)
              {
                      switch(a[i])
                       {
                               case 0:
                               if (!str.empty())
                               str.append("000");
                               else
                               str="000";
                               break;
    
                               case 1:
                               if (!str.empty())
                               str.append("001");
                               else
                               str="001";
                               break;
    
                               case 2:
                               if (!str.empty())
                               str.append("010");
                               else
                               str="010";
                               break;
    
                               case 3:
                               if (!str.empty())
                               str.append("011");
                               else
                               str="011";
                               break;
    
                               case 4:
                               if (!str.empty())
                               str.append("100");
                               else
                               str="100";
                               break;
    
                               case 5:
                               if (!str.empty())
                               str.append("101");
                               else
                               str="101";
                               break;
    
                               case 6:
                               if (!str.empty())
                               str.append("110");
                               else
                               str="110";
                               break;
    
                               case 7:
                               if (!str.empty())
                               str.append("111");
                               else
                               str="111";
                               break;
                       }
              }
              str1="000000";
              str2="000";
                  if (str.length()==3)
                  {
                            str1.append(str);
                            str=str1;
                  }
    
                  if (str.length()==6)
                  {
                            str2.append(str);
                            str=str2;
                  }
    
    
            for (size_t i = 0; i < str_array_size; ++i)
            {
                       str_array[i] = str;
            }
    
              ptr = new char [str.size()+1];
              strcpy (ptr, str.c_str());
    
              //cout<<ptr;
             if(t==1)
              {
    
                      s="no";
                    // cout<<"Not a Octal number\n";
                     return s;
              }
    
    return str;
    }
    the reason , i added 3 other functions to the end of this function , is that , each time i try to include their respective header files, so that when i use the functions , they operate!
    it generates errors saying , that redeclaration of e.g Oct2Dec(int). i couldnt get that to work! and i dont know why it gives me this kind of error. so it made me do such a thing!

    and about & org , because in this function i couldnt access the CLA class's private variable, i declared a Global org in CLA.cpp , and i used its address to modify or use it . the same thing goes to currentline too. ( i didnt know a better way) .
    Last edited by Masterx; 01-27-2009 at 05:46 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    Yes, swapping the key and mapped value makes sense here since you are searching for a string to get the corresponding integer, hence making the string the key and the integer the mapped value is appropriate. To take advantage of the faster searching you would use std::map's own find() member function instead of the std::find_if() generic algorithm, e.g.,
    Code:
    map<string, int>::iterator iter = map1.find(token);
    by the way , you mean by using this built in function ! i dont need that custom function ofyours to search among maps? just simply doing :
    Code:
    map<string, int>::iterator iter = LABEL.find(token);
    if (iter != LABEL.end())
     {                                        
                decimal_retrieved_number = iter->second;
                converted_org = Oct2Dec(org);   
    
                decimal_retrieved_number += converted_org;             
                octalstring = Dec2Oct(decimal_retrieved_number);
    
                converted_octal_number = atoi(octalstring);
                offsetptr = octalstring; 
    
      }
    will give me the tokens mapped value !!?
    Last edited by Masterx; 01-27-2009 at 06:20 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Dear Laiserlight You are an Angle indeed . i've swapped all of the maps values and mapped string as you told me. now they are map<string, int> map1. and i replaced the new searcher with the old one. i checked it and it worked! tanx .
    but again , when trying to put the function in another header file ! i generates the same error!.

    ERROR MESSAGE:
    Code:
    |24|error: conversion from `std::_Rb_tree_const_iterator<std::pair<const std::string, int> >' to non-scalar type `std::_Rb_tree_iterator<std::pair<const std::string, int> >' requested|
    Edited:
    i changed the iterator to const_iterator and , the error is gone, but the search doesnt function properly!. im investigating on it! ( it returns rubbish!!)


    Code:
    //In The name of God
    ///###################################### Search in LABEL and DECLARATIONS #################
    #include <map>
     #include <string>
     #include <cstring>
     #include <cctype>
     #include <iostream>
     using namespace std;
    
     int OcttoDec(int );
     char* DectoOct(int );
     string OcttoBin(int );
    
    char* search(const map<string,int>& map1, const map<string,int>& map2, char* token,int * pointer,int * clptr)
    {
            int decimal_retrieved_number = 0;
            int converted_org = 0;
            int converted_octal_number = 0;
    
            char * octalstring;
            char * ptr;
            char * offsetptr;
    
            map<string, int>::const_iterator iter = map1.find(token);
    
            if (iter != map1.end())
            {
    
            decimal_retrieved_number = iter->second;
    
            converted_org = OcttoDec(* pointer);
    
            decimal_retrieved_number += converted_org;
            octalstring = DectoOct(decimal_retrieved_number);
    
            converted_octal_number = atoi(octalstring);
    
            offsetptr = octalstring;
    
            }
    
            map<string, int>::iterator iter2 = map2.find(token);
    
            if (iter2 != map2.end())
            {
                    int temp;
                    char * converted;
                    string message;
    
                    temp = iter2->second;
    
                    message = OcttoBin(temp);
    
                    if (message == "no")
                    {
                            converted = DectoOct(temp);
                            cout<<"conversion occured!"<<" line "<<*clptr;
    
    
                            offsetptr = converted;
    
    
                    }
                    else if( message != "no" )
                    {
    
                            char * oo;
                            char * o;
                            o = itoa(temp,oo,8);
                            offsetptr = o;//itoa(temp,ptr,8);
    
                    }
             }//end of declaraion if
    return offsetptr;
            }//end of function
    ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! End of Search in LABEL and DECLARATIONS!!!!!!!
    int OcttoDec(int oct)
    {
            int n,r,s=0,i;
            n=oct;
            double S=0;
            for(i=0;n!=0;i++)
             {
                     r=n%10;
                     s=s + r * ((pow(8.0,i)));
                     n=n/10;
             }
            return static_cast<int>(s);
    }
    
    
    char* DectoOct(int dec)
    {
            using namespace std;
            char *rtn;
            int prev, len, temp, *OCT;
            prev = dec;
            for(int i=0;prev!=0;i++)
            {
                    prev = prev/8;
                    len=i;
            }
           OCT = new int[len+1];
           prev = dec;
           for(int i=0;prev!=0;i++)
            {
                    OCT[i] = prev%8;
                    prev = prev/8;
                    len=i;
            }
          OCT[len+1] = prev%8;
          len++;
          for(int i=0;i<len/2;i++)
           {
                   temp=OCT[i];
                   OCT[i]=OCT[len-1-i];
                   OCT[len-1-i]=temp;
            }
    
         rtn = new char[len+1];
    
         for(int i=0;i<len;i++)
          {
                  rtn[i]=OCT[i]+'0';
                  rtn[i+1]='\0';
          }
    
          return rtn;
    
    }
    
    
    string OcttoBin(int n)
    {
    
    
             int a[6],i=0,t=0,num;
             string str,str1,str2,s;
    
             char buffer[9];
             char * ptr, * not_octal;
             string str_array[9];
             const size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
    
             while(n!=0)
             {
                     a[i]=n%10;
                     n=n/10;
                     if(a[i]>7)
                     t=1;
                     i++;
             }
             i--;
             if(t==0)
              for(;i>=0;i--)
              {
                      switch(a[i])
                       {
                               case 0:
                               if (!str.empty())
                               str.append("000");
                               else
                               str="000";
                               break;
    
                               case 1:
                               if (!str.empty())
                               str.append("001");
                               else
                               str="001";
                               break;
    
                               case 2:
                               if (!str.empty())
                               str.append("010");
                               else
                               str="010";
                               break;
    
                               case 3:
                               if (!str.empty())
                               str.append("011");
                               else
                               str="011";
                               break;
    
                               case 4:
                               if (!str.empty())
                               str.append("100");
                               else
                               str="100";
                               break;
    
                               case 5:
                               if (!str.empty())
                               str.append("101");
                               else
                               str="101";
                               break;
    
                               case 6:
                               if (!str.empty())
                               str.append("110");
                               else
                               str="110";
                               break;
    
                               case 7:
                               if (!str.empty())
                               str.append("111");
                               else
                               str="111";
                               break;
                       }
              }
              str1="000000";
              str2="000";
                  if (str.length()==3)
                  {
                            str1.append(str);
                            str=str1;
                  }
    
                  if (str.length()==6)
                  {
                            str2.append(str);
                            str=str2;
                  }
    
    
            for (size_t i = 0; i < str_array_size; ++i)
            {
                       str_array[i] = str;
            }
    
              ptr = new char [str.size()+1];
              strcpy (ptr, str.c_str());
    
              //cout<<ptr;
             if(t==1)
              {
    
                      s="no";
                    // cout<<"Not a Octal number\n";
                     return s;
              }
    
    return str;
    }
    Last edited by Masterx; 01-27-2009 at 07:24 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice the parameter:
    Code:
    const map<string,int>& map2
    Therefore instead of iterator, you should use const_iterator:
    Code:
    map<string, int>::const_iterator iter = map1.find(token);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    thanx dear laiserlight . the search now is operational. it works . somehow due to some unkown problem after that the program fetches, scans, tokenizes the strings through a loop ,and successfully going thought the last stage of the program,. by coming out of the loop( i think) , the dont send error message appears .
    (this was happening even before i commit such a change.)
    -------------------
    by the way , i changed the old search version , the one you (laiserlight) gave me at first. it wrks while the new one doesnt! i checked everything! but no way i couldnt get the right thing out of it! as i dont care about efficiency now, ill let it be so, maybe some other time i worked on it)
    Last edited by Masterx; 01-27-2009 at 10:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. help w/ write() function and open() function
    By Landroid in forum C Programming
    Replies: 1
    Last Post: 04-10-2005, 11:38 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM