Hi everyone,

This is my first post so go easy on me please :). I am trying to compile a simple program that takes two binary numbers and computes a third based on bit operations (i.e. C = A&B, C = A|B etc....) but I run into the "vector subscript out of range error" even though I have carefully tried to ensure that I'm not stepping out of index bounds.

Is anyone able to help me with this?

NOTE: The binary numbers are stored as vector arrays of type char

int main()
{
	// Request and obtain valid decimal numbers
	int A = obtaindec('A');
	int B = obtaindec('B');

	// Convert A and B to binary and store as vector<char> arrays

	vector<char> Abin = dectobinstring('A', A);
	vector<char> Bbin = dectobinstring('B', B);

	// Create C based on the largest size of the other two
	vector<char> Cbin;
	if ( Abin.size() > Bbin.size() )
		Cbin.resize(Abin.size());
	else
		Cbin.resize(Bbin.size());
	int Cbinsize = Cbin.size();
	for ( unsigned int x = 0; x < Cbinsize; ++x )
		Cbin[x] = 48;

	// Now recalculate C as the result of relational operations
	// on A and B

	for ( unsigned int x = 0; x < Cbinsize; ++x )
		if ( Abin[x] == 49 && Bbin[x] == 49 )
			Cbin[x] = 49;

	cout << "\n\nA & B = ";
	 for ( unsigned int x = 0; x < Cbinsize; ++x )
		cout << Cbin[x];
	cout << endl;
}
int main()
{
	// Request and obtain valid decimal numbers
	int A = obtaindec('A');
	int B = obtaindec('B');

	// Convert A and B to binary and store as vector<char> arrays

	vector<char> Abin = dectobinstring('A', A);
	vector<char> Bbin = dectobinstring('B', B);

	// Create C based on the largest size of the other two
	vector<char> Cbin;
	if ( Abin.size() > Bbin.size() )
		Cbin.resize(Abin.size());
	else
		Cbin.resize(Bbin.size());
	int Cbinsize = Cbin.size();
	for ( unsigned int x = 0; x < Cbinsize; ++x )
		Cbin[x] = 48;

	// Now recalculate C as the result of relational operations
	// on A and B

	for ( unsigned int x = 0; x < Cbinsize; ++x )
		if ( Abin[x] == 49 && Bbin[x] == 49 )
			Cbin[x] = 49;

	cout << "\n\nA & B = ";
	 for ( unsigned int x = 0; x < Cbinsize; ++x )
		cout << Cbin[x];
	cout << endl;
}

Consider Abin to be of size 10 and Bbin to be of size 8 now according to your code since Abin.size() > Bbin.size(), Cbin is resized to Abin.size(), hence 10

Then jump to line 25, you have a loop which goes upto CbinSize, which is 10 and you end up accessing Bbin[8] which could either throw an error or end up giving out some garbage.

commented: Concise help, thank you! +1

Consider Abin to be of size 10 and Bbin to be of size 8 now according to your code since Abin.size() > Bbin.size(), Cbin is resized to Abin.size(), hence 10

Then jump to line 25, you have a loop which goes upto CbinSize, which is 10 and you end up accessing Bbin[8] which could either throw an error or end up giving out some garbage.

Wow, I can't believe I didn't see that, thank you very much!

One of the requirements imposed on this exercise is to give binary output as a char string but not necessarily perform the calculations in this form.

I just realized that bit operations work fine on integers: I could have done all the operations on A and B as ints and then convert the resulting C back to binary, a lot more efficient!

Cheers,
Ivan

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.