DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Debug Assertion Failed! (http://www.daniweb.com/forums/thread146295.html)

JackDurden Sep 17th, 2008 11:27 am
Debug Assertion Failed!
 
I have this error when debugging called Debug Assertion Failed , does anyone know what that means? Im using Visual C++ 2008.

void merge (string alpha, string beta)
{
        string temp;
        string temp2;
        stringstream insert(alpha);
    stringstream insert2(beta); // Insert the string into a stream
        ostream_iterator<string> output(cout, " ");

    vector<string> words; //vector to hold words
        vector<string> words2;
        vector<string> results;
   
        while (insert >> temp)
        words.push_back(temp);

        while (insert2 >> temp2)
        words2.push_back(temp2);
       
        merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());

        cout<<"Merge contains:";
                copy(results.begin(), results.end(), output );



}

Narue Sep 17th, 2008 11:31 am
Re: Debug Assertion Failed!
 
The error will also tell you what assertion failed and where it failed. Basically it means you used a library class or function the wrong way, such as passing a null pointer to strcpy.

JackDurden Sep 17th, 2008 11:39 am
Re: Debug Assertion Failed!
 
ok so since it happened after I put in this function its somewhere in here, do you see any problems with this code that would make that error?

void merge (string alpha, string beta)
{
        string temp;
        string temp2;
        stringstream insert(alpha);
    stringstream insert2(beta); // Insert the string into a stream
        ostream_iterator<string> output(cout, " ");

    vector<string> words; //vector to hold words
        vector<string> words2;
        vector<string> results;
   
        while (insert >> temp)
        words.push_back(temp);

        while (insert2 >> temp2)
        words2.push_back(temp2);
       
        merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());

        cout<<"Merge contains:";
                copy(results.begin(), results.end(), output );

}

niek_e Sep 17th, 2008 11:43 am
Re: Debug Assertion Failed!
 
this line is wrong:
merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());


You call the function with 5 parameters, but the function only expects 2 (
void merge (string alpha, string beta)


What is your program supposed to do?

Narue Sep 17th, 2008 11:53 am
Re: Debug Assertion Failed!
 
>do you see any problems with this code that would make that error?
Yes, and while I'm still miffed that you failed to post the complete text of your error even after I not so subtly informed you that more information is present, I'll tell you what the problem is:
merge(
  words.begin(), words.end(), // OK
  words2.begin(), words2.end(), // OK
  results.begin()); // Oops! results is empty
You need to use some kind of insert iterator to add new items to an empty vector, or you can resize the vector before calling merge:
merge ( 
  words.begin(), words.end(),
  words2.begin(), words2.end(),
  back_inserter ( results ) );
>You call the function with 5 parameters, but the function only expects 2
Can you say "overloading"?

niek_e Sep 17th, 2008 11:59 am
Re: Debug Assertion Failed!
 
Quote:

Originally Posted by Narue (Post 693372)
>You call the function with 5 parameters, but the function only expects 2
Can you say "overloading"?

Yes I can. Problem is that I forgot that merge() was a function from the C++ Std lib, hence my reply...
Silly me, disregard my previous post as it is not worth the space somewhere on a webserver...

JackDurden Sep 17th, 2008 12:56 pm
Re: Debug Assertion Failed!
 
The error looks like this:

Debug Assertion Failed!
line 2508
expression: sequence not ordered

And here is my entire code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

using namespace std;

//prototype of split function
void SplitLines (string& a);
void SplitLines2 (string& b);
void AunionB (string a, string b);
void AinterectB ();
void AdifferenceB ();



int main()
{
  string fileName;
  string a;
  string b;
  int names = 0;
  ifstream myfile;
 
  cout << "Enter file name: ";// Get filename from user
  cin >> fileName;

 
  myfile.open(fileName.c_str());

 
  while(myfile.good() == false)// Prompt for new file name if not able to open
  {
      cout << "Unable to open file. Enter a different name: ";
          myfile.clear();
      cin >> fileName;
      myfile.open(fileName.c_str());
         
  }

  while (!myfile.eof())
  { 
          getline(myfile,a, '\n');
          getline(myfile,b, '\n');
          //function call to split lines into words
          SplitLines(a);
          cout<<endl;
          SplitLines2(b);
          cout<<endl;
          AunionB (a, b);
  }
  myfile.close();
  return 0;
}
//name of fucntion for splitting
void SplitLines (string& a)
{
        string temp;
    stringstream insert(a); // Insert the string into a stream

    vector<string> words; //vector to hold words

    while (insert >> temp)
        words.push_back(temp);
        cout<<"Set A: {";
        for( int i = 0; i < words.size(); i++ ) {
                cout<< words[i] <<","<< " ";
    }
        cout<<"}";
}
void SplitLines2 (string& b)
{
        string temp;
    stringstream insert(b); // Insert the string into a stream

    vector<string> words; //vector to hold words

    while (insert >> temp)
        words.push_back(temp);
        cout<<"Set B: {";
  for( int i = 0; i < words.size(); i++ ) {//print vector to screen
  cout << words[i]  <<","<< " ";
  }
        cout<<"}";
}
void AunionB (string a, string b)
{
        string temp;
        string temp2;
        stringstream insert(a);
    stringstream insert2(b); // Insert the string into a stream
        ostream_iterator<string> output(cout, " ");

    vector<string> words; //vector to hold words
        vector<string> words2;
       
   
        while (insert >> temp)
        words.push_back(temp);

        while (insert2 >> temp2)
        words2.push_back(temp2);

                vector<string> results(words.size() + words2.size());
       
        merge(words.begin(), words.end(), words2.begin(), words2.end(), results.begin());

        cout<<"Merge contains:";
                copy(results.begin(), results.end(), output );

}

Narue Sep 17th, 2008 1:02 pm
Re: Debug Assertion Failed!
 
Sort your vectors before merging.


All times are GMT -4. The time now is 9:12 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC