It compiles and executes but for some reason my vectors do not initialize in the fillarrays function. What am I doing wrong?

#include <iostream>
#include <conio.h> // for getch()
#include <vector>
using namespace std;

bool and (std::vector<bool> p, std::vector<bool> q, int i); // returns bool for and
bool or (std::vector<bool> p, std::vector<bool> q, int i); // returns bool for or
bool xor (std::vector<bool> p, std::vector<bool> q, int i); // returns bool for  xor
bool implies (std::vector<bool> p, std::vector<bool> q, int i); // returns bool for if then;
bool ifandif (std::vector<bool> p, std::vector<bool> q, int i); // returns bool for if and only if
void fillarrays (std::vector<bool> p, std::vector<bool> q); // initialises p and q
void dashes ();

int main ()
{
	vector<bool> p, q;
	int i;
	fillarrays(p,q);
	cout << "P|Q|P and Q\n";
	dashes ();
	for (i=0; i<p.size(); i++)
	{
		cout << p[i] << "|" << q[i] << "|" << and (p, q, i) << endl;
	}
	cout << "Press any key to continue.\n";
	getch ();
	cout << "\nP|Q|P or Q\n";
	dashes ();
	for (i=0; i<p.size(); i++)
	{
		cout << p[i] << "|" << q[i] << "|" << or (p, q, i) << endl;
	}
	cout << "Press any key to continue.\n";
	getch ();
	cout << "\nP|Q|P xor Q\n";
	dashes ();
	for (i=0; i<p.size(); i++)
	{
		cout << p[i] << "|" << q[i] << "|" << xor (p, q, i) << endl;
	}
	cout << "Press any key to continue.\n";
	getch ();
	cout << "\nP|Q|P -> Q\n";
	dashes ();
	for (i=0; i<p.size(); i++)
	{
		cout << p[i] << "|" << q[i] << "|" << implies (p, q, i) << endl;
	}
	cout << "Press any key to continue.\n";
	getch ();
	cout << "\nP|Q|P <-> Q\n";
	dashes ();
	for (i=0; i<p.size(); i++)
	{
		cout << p[i] << "|" << q[i] << "|" << ifandif (p, q, i) << endl;
	}
	return 0;
}

bool and (std::vector<bool> p, vector<bool> q, int i)
{
	if (p[i]&&q[i]==true)
		return true;
	else return false;
}

bool or (std::vector<bool> p, vector<bool> q, int i)
{
	if (p[i]||q[i]==true)
		return true;
	else return false;
}

bool xor (std::vector<bool> p, vector<bool> q, int i)
{
	if (p[i]&&q[i]==true)
		return false;
	else if (p[i]||q[i]==true)
		return true;
	else return false;
}

bool implies (std::vector<bool> p, std::vector<bool> q, int i)
{
	if ((p[i]==false)&&(q[i]==false))
		return true;
	else if (q[i]==true)
		return true;
	else return false;
}

bool ifandif (std::vector<bool> p, std::vector<bool> q, int i)
{
	if (((p[i]==true)&&(q[i]==true))||((p[i]==false)&&(q[i]==false)))
		return true;
	else return true;
}
void fillarrays (std::vector<bool> p, std::vector<bool> q)
{
	p.push_back(true);
	q.push_back(true);
	p.push_back(true);
	q.push_back(false);
	p.push_back(false);
	q.push_back(true);
	p.push_back(false);
	q.push_back(false);
}

void dashes ()
{
	cout << "-------------------" << endl;
}

You're not passing references to them.

So inside the functions, changes are made on a local copy, which is lost when the function returns.

Hi Avatar99

When you call fillarrays() you do not pass the same instances of the vectors to the function. p and q are copied to the p and q instances in the function. When the function returns p and q on the caller side are still empty.

You probably want to pass these parameters as "call by reference" by using the & symbol ....

void fillarrays (std::vector<bool>& p, std::vector<bool>& q);

.
.
void fillarrays (std::vector<bool>& p, std::vector<bool>& q)
{
   p.push_back(true);
   q.push_back(true);
   p.push_back(true);
   q.push_back(false);
   p.push_back(false);
   q.push_back(true);
   p.push_back(false);
   q.push_back(false);
}

Edited 7 Years Ago by hrafnk2009: missing code tags

*heads desk* Thank you for that very easy solution I should have thought of it.

Comments
We've all been there :)
This article has been dead for over six months. Start a new discussion instead.