I am trying to merge two vectors, alternating elements from both vectors. And if one vector is longer than the other, alternate as long as possible then append the remaining elements from the longer vector. This is what I have so far:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

int main ()
{
	vector<int> vector_a, vector_b, vector_c;
	int i;

	for(i=0; i+=2)
		vector_a.push_back('A'+i);
	for(i=0; i+=2)
		vector_b.push_back('B'+i);

	cout << "Original contents of vector a: ";
	for (i=0; i vector_a.size(); i++)
		cout <<vector_a[ i ];
	cout << endl;

	cout << "Original contents of vector b: ";
	for(i=0; i vector_b.size();i++)
		cout << vector_b[ i ];
	cout << endl;


	merge (vector_a.begin(), vector_a.end(), vector_b.begin(), vector_b.end(), vector_c.begin());

	cout << "Result of merge:\n";
	for(i=0; i vector_c.size(); i++)
		cout << vector_c[ i ];

	return 0;
}

feedback:

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(12) : error C2143: syntax error : missing ';' before ')'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(14) : error C2143: syntax error : missing ';' before ')'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(18) : error C2146: syntax error : missing ';' before identifier 'vector_a'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(18) : error C2146: syntax error : missing ')' before identifier 'i'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(18) : error C2059: syntax error : ';'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(18) : error C2059: syntax error : ')'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(19) : error C2146: syntax error : missing ';' before identifier 'cout'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(23) : error C2146: syntax error : missing ';' before identifier 'vector_b'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(23) : error C2146: syntax error : missing ')' before identifier 'i'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(23) : error C2059: syntax error : ';'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(23) : error C2059: syntax error : ')'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(24) : error C2146: syntax error : missing ';' before identifier 'cout'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(31) : error C2146: syntax error : missing ';' before identifier 'vector_c'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(31) : error C2146: syntax error : missing ')' before identifier 'i'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(31) : error C2059: syntax error : ';'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(31) : error C2059: syntax error : ')'

1>d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(32) : error C2146: syntax error : missing ';' before identifier 'cout'

You are missing the loop termination conditions in the for loops in the lines where compilation errors occur.

ok well i fixed it up a bit but i cannot get my cin statements properly to insert the two vectors this is what i have:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

	
int main ()
{
	vector<int> vector_a, vector_b, vector_c;
	int i;


	for(i=0; i < 0; i+=2)
	{
		vector_a.push_back('A'+i);
	}
	for(i=0; i < 0; i+=2)
	{
		vector_b.push_back('B'+i);
	}

	cout << "Original contents of vector a: " << endl;
	
	for (i=0; vector_a.size(); i++)
	{
	cout << vector_a[ i ];
	cout << endl;
	}

	cout << "Original contents of vector b: " << endl;
	
	for(i=0; vector_b.size(); i++)
	{
	cout << vector_b[ i ] << endl;
	}

	merge (vector_a.begin(), vector_a.end(), 
			vector_b.begin(), vector_b.end(), 
			vector_c.begin());

	cout << "Result of merge:\n";

	for(i=0;vector_c.size(); i++)
	{	
	cout << vector_c[ i ];
	}


	return 0;
}

like i know how to do cin statements cause they are simple, like first thing i learned this year but this is my code and the weird error that comes with my cin statements

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

	
int main ()
{
	vector<int> vector_a, vector_b, vector_c;
	int i;

	cout << "Please insert your first vector:" << endl;
	cin >> vector_a;
	cout << "Please insert your second vector:" << endl;
	cin >> vector_b;

	for(i=0; i < 0; i+=2)
	{
		vector_a.push_back('a'+i);
	}
	for(i=0; i < 0; i+=2)
	{
		vector_b.push_back('b'+i);
	}

	cout << "Results of your first vector before merge: " << endl;
	
	
	
	for (i=0; vector_a.size(); i++)
	{
	cout << vector_a[ i ] << endl;
	}

	cout << "Results of your second vector before merge: " << endl;
	

	
	for(i=0; vector_b.size(); i++)
	{
	cout << vector_b[ i ] << endl;
	}

	merge (vector_a.begin(), vector_a.end(), 
			vector_b.begin(), vector_b.end(), 
			vector_c.begin());

	cout << "Result of merge:\n";

	for(i=0;vector_c.size(); i++)
	{	
	cout << vector_c[ i ];
	}




	

	return 0;
}

feedback:
d:\my documents\programming\assignments\assignment 3\p6.8\p6.8\p6.8.cpp(14) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
1> with
1> [
1> _Ty=int
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1144): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1146): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1148): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1150): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(155): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(161): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(168): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(175): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(194): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(228): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(247): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(273): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(291): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(309): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__w64 unsigned long &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(329): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(348): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(367): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(386): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(404): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(422): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(441): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> while trying to match the argument list '(std::istream, std::vector<_Ty>)'
1> with
1> [
1> _Ty=int
1> ]

you have some problems with constructing for loop.
How many times this loop will be executed:

for(i=0; i < 0; i+=2)

?
And here, the loop termination 'condition' is wrong:

for(i=0; vector_b.size(); i++)

you have some problems with constructing for loop.
How many times this loop will be executed:

for(i=0; i < 0; i+=2)

?
And here, the loop termination 'condition' is wrong:

for(i=0; vector_b.size(); i++)

for the first for loop i didnt know what i should make the condition as due to the fact as its a user input, so idno how to make it any number.

I don't understand what you are trying to do on lines 14 and 16. The >> operator does not work with entire vectors like that. Also what's going on with your loops on 18 and 22? Check those loops by hand, if "i" starts at zero and it evaluates the loop condition i<0, it will already be false and the loop will stay right where it is. If the alphabet has 26 letters and you're going to be alternating them, you know how many elements each vector is going to have.

I don't understand what you are trying to do on lines 14 and 16. The >> operator does not work with entire vectors like that. Also what's going on with your loops on 18 and 22? Check those loops by hand, if "i" starts at zero and it evaluates the loop condition i<0, it will already be false and the loop will stay right where it is. If the alphabet has 26 letters and you're going to be alternating them, you know how many elements each vector is going to have.

I'm not trying to do the alphabet, its supposed to be something like this

(1 2 3 4 5)
(6 7 8 9 10 11 12)

=
(1 6 2 7 3 8 4 9 5 10 11 12)

The easiest way to get user input and use it in loop, is to do something like that:

//read input
int number;
cout<<"Enter the number: ";
cin>>number;

//use variable number in loop
for (int i=0;i<number;++i) {  //see the condition
...
}

for the first for loop i didnt know what i should make the condition as due to the fact as its a user input, so idno how to make it any number.

Maybe that's what I don't understand. It seems like you are generating the vectors in the two for loops. What input would you need from the user?

I'm not trying to do the alphabet, its supposed to be something like this

(1 2 3 4 5)
(6 7 8 9 10 11 12)

=
(1 6 2 7 3 8 4 9 5 10 11 12)

I see a little bit better now. Are you trying to ask the user for the number of elements? When I saw the 'a' I thought you were going to convert these numbers back to characters down the line.

Sorry our posts keep getting crossed.

Maybe that's what I don't understand. It seems like you are generating the vectors in the two for loops. What input would you need from the user?

the user would need to insert 2 vectors of any length

You'd have to replace your lines 13 and 14 with a while loop.

int temp;
while(cin >> temp)
{
   //Push back temp onto your vector
}

But you'll either have to have your user hit ctrl-z after they are done entering items(which would send an End Of File signal to cin>>temp which would stop the while loop) or you'll have to use a sentry value (like "Enter -999 when done" -- but then you couldn't have -999 as an element in your vector)

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.