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 );



}

Recommended Answers

All 8 Replies

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.

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 );

}

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?

>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"?

>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...

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 );

}

Sort your vectors before merging.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.