Thread: problem in manipulating a maped array

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

    problem in manipulating a maped array

    hello every one , why do i get this error ,would any one tell me the reason?
    (probably im doing it the wrong way ,as it may be understood from the codes im trying to access a mapped array elements through a while loop (do it till you havent reached the end of the array) . here is the codes ,(sorry for indentation)
    before i post the codes , here is the error i got :
    C:\Documents and Settings\Master\My Documents\Goldedition\charpointer2.cpp|
    27|error: base operand of `->' has non-pointer type `std::map<int, std::string, std::less<int>, std::allocator<std:air<const int, std::string> > >'|
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <map>
    using namespace std;
    
    int main()
    {
    
        char memory[5][4]={0};
        char memory1[5][4]={0};
        char * bred="1101";
        char *red="red";
        char *lda="lda";
        char *blda="1001";
        char *sta="sta";
        char *ptr;
        string ptr1;//for testing only
        map<int,string>::iterator it;
        map<int,string>  commandregister;// is used to store the latest
        int i=0,c=0,k=0;
        bool ok=true;
    
        commandregister[0]=red;
        commandregister[1]=lda;
        commandregister[2]=sta;
    
    while (   (*it) != commandregister.end()  )
        {
    
             if ((*it).second ==red )
             {
                  cout<<"yes"<< endl;
                  for(int i=0;i<4;i++)
                  memory[k][i]=*(bred+i);
                  cout <<"we have red ";
                  for (int j=0;j<4;j++)
                  cout<<memory[k][j];
            }
                  k++;
      // else  //else cout<<"no";
           if ((*it).second ==lda )
            {
              cout<<"yes"<< endl;
              for(int i=0;i<4;i++)
              memory[k][i]=*(blda+i);
              cout <<"we have lda ";
              for (int j=0;j<4;j++)
              cout<<memory[k][j];
            }
     it++;   //next element
        }  //
        cout<<endl;
        for (int k=0;k<5;k++)
        {
            for (int j=0;j<4;j++)
                {  
                    cout<<memory[k][j];
                }
        }
    
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    any answer is greatly appreciated .
    Last edited by Masterx; 12-31-2008 at 07:57 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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see you initializing it anywhere. (I also am not sure which line is line 27.)

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Because you are not evaluating the iterator but rather the value it holds (actually, you are not even doing this because of how map works but that's another story).

    Change (*it) to it.

    Edit: Good call on it not being initialized too.

  4. #4
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by tabstop View Post
    I don't see you initializing it anywhere. (I also am not sure which line is line 27.)
    line 27 is t his line :
    while ( (*it) != commandregister.end() )

    and what should i initialize? command register is already being initialized!
    here: commandregister[0]=red;
    commandregister[1]=lda;
    commandregister[2]=sta;

    Desolation would you explain more? ididnt get you!
    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


  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Masterx View Post
    line 27 is t his line :
    while ( (*it) != commandregister.end() )

    and what should i initialize? command register is already being initialized!
    here: commandregister[0]=red;
    commandregister[1]=lda;
    commandregister[2]=sta;

    Desolation would you explain more? ididnt get you!
    You need to initialize it. That is, the variable "it". You probably want something like
    Code:
    it = commandregister.start();
    And you don't want to compare what is pointed to, but "it" itself.
    Code:
    it != commandregister.end()

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Isn't it .begin() rather than .start()?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes, you're right. Good point -- make it .begin().

  8. #8
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by tabstop View Post
    You need to initialize it. That is, the variable "it". You probably want something like
    Code:
    it = commandregister.start();
    And you don't want to compare what is pointed to, but "it" itself.
    Code:
    it != commandregister.end()
    many tanx dear tabstop .
    Quote Originally Posted by twomers View Post
    Isn't it .begin() rather than .start()?
    tanx dear
    Quote Originally Posted by tabstop View Post
    Yes, you're right. Good point -- make it .begin().
    again tanx , now there is another problem , it doesn't print anything on the console screen but the phrase "press any key to continue" which i believe belongs to the cstdlibs system("PAUSE") . why is it like this ?! im supposed to get this right , i used a for statement and all of these work just fine ! but changing it to a while statement it just doesnt reproduce what it should !

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <map>
    using namespace std;
    
    int main()
    {
    
    
        char * bred="1101";
        
        char *red="red";
    
        char *lda="lda";
    
        char *blda="1001";
    
        char *sta="sta";
    
        char *ptr;
    
        
        map<int,string>::iterator it;
    
        map<int,string>  commandregister;// is used to store the latest
    
        it = commandregister.begin();
    
        int i=0,c=0,k=0;
    
        bool ok=true;
    
    
        commandregister[0]=red;
        commandregister[1]=lda;
        commandregister[2]=sta;
    
    while (   it != commandregister.end()  )
        {
    
             if ((*it).second ==red )
             {
                  cout<<"yes"<< endl;
    
            }
               
    
           if ((*it).second ==lda )
            {
              cout<<"yeah"<< endl;
    
            }
     it++;   //next element
        }  //
        cout<<endl;
      
       
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by Masterx; 12-31-2008 at 10:36 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


  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    Try moving the initialization of it after the three lines where you do 'commandregister[0] = red ...'.

    I don't think your iterator is still valid after you add new elements to the container.

  10. #10
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by Desolation View Post
    Try moving the initialization of it after the three lines where you do 'commandregister[0] = red ...'.

    I don't think your iterator is still valid after you add new elements to the container.
    many tanx dear Desolation , it works now , but i wonder why is it like this ? i mean i just dont understand why this happened!
    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


  11. #11
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    by the way , if i have a mapped array which contains some data(in another function i filled this array ) and now i just want to go through the elements of the array , now what should i do in this case ? according to this example , im not able to initialize it like this!
    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


  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    if i have a mapped array which contains some data(in another function i filled this array ) and now i just want to go through the elements of the array , now what should i do in this case ? according to this example , im not able to initialize it like this!
    What do you mean? You certainly are able to traverse the map via iterators, and it looks like you have done so after applying the suggested fix (which is basically a variant of the "declare variables near first use" principle).
    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

  13. #13
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    What do you mean? You certainly are able to traverse the map via iterators, and it looks like you have done so after applying the suggested fix (which is basically a variant of the "declare variables near first use" principle).
    dear laserlight , i mean i have problem figuring this out! see im confused to some extend !
    first of all i learned that if im going to use a while with the iterator , i should initialize it (big deal, its fine) and this initialization must be placed just after array initialization , this is the only way that the "it "gets initialized properly! so if i have a pre filled array (initialized) do i have a problem in initializing the iterator ?cause i dont initialize the array now , how is "it" going to get initialized properly ! ?
    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


  14. #14
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    when you assign it = commandregister.begin(), commandregister is empty.

    doesn't it seem logical that the beginning point of an empty map and one that has been filled would be different?

  15. #15
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by m37h0d View Post
    when you assign it = commandregister.begin(), commandregister is empty.

    doesn't it seem logical that the beginning point of an empty map and one that has been filled would be different?
    tanx i didnt know that , all i was thinking was that, initially the" it "would point to the first element of the array , knowing that the array starts at 0 !
    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


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM