passing vector to another function

Feb 25, 2012 at 8:42am
here is the scenario...

i have this 4 function that i want my vector to work into..

function 1: where i started inputting in my vector

function 2: where i will be ask if i want to add another input or print it

function 3: where my answer in function 2 will be evaluated, if want to add.. we will go back to function 1 and add another item to our vector, and if print we will be proceed to..

function 4: where we need to print all inputs in function 1

here is my super confusing code:

function1()
{
do{
cout<<"\n\n ";
cout<<"Product Number ('0'-List of Products): ";
cin>>n;
prodnum.push_back (n);
if (cin.fail())
cout<<"\n\nError! Input an integer.\n";
else
supernestedif (n, quan=0);
}while (n==0 || n>10);

do{
cout<<" Quantity: ";
cin>>quan;
quantt.push_back (quan);
if (quan==0)
cout<<"\n\n Order atleast 1.\n\n";
else if (quan>10000)
cout<<"\n\n We cannot take too many orders.\n\n";
}while (quan==0 || quan>10000);

function 2();

return 0;
}

function2()
{
cout<<"\n\n Do you want to..\n";
cout<<" T-Compute Total A-Add Product\n";
function3 (wntto);
return 0;
}

function3(char w)
{
char surexit;

if (w=='T' || w=='t')
function4 ();
else if (w=='A' || w=='a')
function1();
return w;
}


}

function4 ()
{
cout<<" Item Description Qty Unit Price Amount\n";
vector<int>::iterator it;
vector<int>::iterator ip;

it=quantt.begin();


for ( ip=prodnum.begin() ; ip < prodnum.end(); ip++ )
{

cout<<" "<<pn[*ip]<<" "<<*it<<" "<<up<<" "<<"\n";
it++;

}

delete [] pn;

return 0;
}

if you ran this code.. debug problem occur
*note: some of the unnecessary code are omitted for fast evaluation


please help??
Feb 25, 2012 at 11:52am
It's a bit tough to read without code tags but this is how you would pass a vector into a function:

If you do not plan to make changes to the vector:
void functionA(vector<int> MyVector);

If you do plan to make changes to the vector:
void functionB(vector<int> &MyVector);

To use these functions:
1
2
3
4
vector<int> TheVector;
...
functionA(TheVector);
functionB(TheVector);

It's so easy!

To use the vector in these functions:
1
2
3
4
5
6
7
8
9
void functionA(vector<int> MyVector)
{
  int a = MyVector.at(MyVector.begin());
}
void functionB(vector<int> &MyVector)
{
  int a = 0;
  MyVector.push_back(a);
}


Oh by the way, your functions don't have types. You need to make them int or void or something.
Last edited on Feb 25, 2012 at 11:54am
Feb 25, 2012 at 12:14pm
whats the difference of having function type and not putting it?
Feb 25, 2012 at 12:29pm
the function doesn't recognize the type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

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

void func_without_vect (int a) {
	cout << a[1] << endl;
}

int main () {
	vector <int> a;
	a.push_back (2);
	a.push_back(1);
	//func_without_vect(a);
	func_with_vect(a);
	cin.get();
	return 0;
}


work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
using namespace std;

void func_with_vect (vector <int> a) {
	cout << a[0] << endl;
}

int main () {
	vector <int> a;
	a.push_back (2);
	a.push_back(1);
	//func_without_vect(a);
	func_with_vect(a);
	cin.get();
	return 0;
}
Feb 25, 2012 at 12:53pm
The first one is wrong because your passing a vector, but the function expects an int.

The second one is correct. I compiled it and it worked just fine.

What's the problem?
Topic archived. No new replies allowed.