Help Please!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help Please!

  1. #1
    Join Date
    Nov 2010
    Posts
    2

    Help Please!

    Hi I keep trying to run this procedure in my program, however it keeps telling me that it is calling a vector index out of bounds and im getting extremely frustrated as to why it is doing this. Any help would be greatly appreciated!
    Thanks,
    I attached the code for the procedure as well

    void merge(vector <string> & songs1, vector <string> & artists1,
    vector <int> & ratings1, vector <string> & songs2,
    vector <string> & artists2, vector <int> & ratings2)
    {
    bool not_dupl = true;
    for(int i = 0; i < songs2.size(); i++)
    {

    for(int j = 0; j < songs1.size(); i++)
    {
    not_dupl = false;
    if((songs2.at(i) != songs1.at(j)) and (artists2.at(i) != artists1.at(j)))
    {
    //adds songs to playlist
    not_dupl = true;

    }
    }
    if( not_dupl == true)
    {
    songs1.push_back(songs2.at(i));
    artists1.push_back(artists2.at(i));
    ratings1.push_back(ratings2.at(i));
    cout << songs1.size() <<' ' << songs2.size() << endl;
    }
    }
    return;
    }
    Attached Files Attached Files

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: Help Please!

    Please use code tags and proper indentation to make your code readable.

    Look closely at that line.

    for(int j = 0; j < songs1.size(); i++)

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Help Please!

    It is dangerous to have multiple vectors which were running the same index. You always need to assure that both the vectors were filled same time. An easier approach is to have a struct or class which combines the triple song, artist and rating and then have only one vector:

    Code:
    struct SAR
    {
         std::string song;
         std::string artist;
         int rating;
         // operator need from std::find
         bool operator=(const SAR & sar)
         {
              // here compare all 3 members and return true if they were equal otherwise false
              ...
          }
    };
    
    ....
    void merge(std::vector<SAR> & sar1, std::vector<SAR> & sar2)
    {
         for (int i = 0; i < (int)sar2.size(); ++i)
         {
               // check if sar2[i] not in first vector
               if (std::find(sar1.begin(), sar1.end(), sar2[i]) == sar1.end())
               {
                       // now push_back the sar2[i] into sar1
               }
                    
         }
    }

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,281

    Re: Help Please!

    To add to what itsmeandnobodyelse just said, a further advantage to this approach is that you can use the standard algorithms, including merge. If this is homework, the goal is probably to actually write the merge function, but in real-life, you should try to avoid writing these kind of functions.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Nov 2010
    Posts
    2

    Re: Help Please!

    Thanks everyone I got it to work. And yeah this procedure was part of a larger ipod emulating program I had to write for my class and they wanted to see us actually write the merge function manually. Thanks again

  6. #6
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Help Please!

    bool operator=(const SAR & sar)
    That was a typo. It must be operator== for comparing two SAR objects.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center