reference a vector of objects in another function.

I am trying to make a form program that will allow the user to edit a form in the vector that was made by the user. I cannot find any information on how to reference a vector with the way I made my vector. I think that I need to create my vector in a different way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>

using namespace std;


class Form {
	public:
		string Fname;
		string Lname;
		string City;
		string Street;
		string State;
		string ZipCode;


};

void menuMain();

void menu1st(Form Fvect[])
{
	int MainM;
	int n;

	cout << "NEW FORM(s)" << endl;
	cout << "Enter the number of forms you would like to make (Maximum of 5): "; cin >> n; cout << endl;
	for (int i = 0; i < n; i++)
	{
		cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
		cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
		cout << "City: "; cin >> Fvect[i].City; cout << endl;
		cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
		cout << "State: "; cin >> Fvect[i].State; cout << endl;
		cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
	}

	cout << "Enter 1 to go back to main: "; cin >> MainM;
	if (MainM == 1)
	{
		menuMain();
	}
	else
	{
		cout << "Error not a correct input." << endl;
		menuMain();
	}
}

void menu2nd(Form Fvect[])
{
	int MainM;
	int Fnum;

	cout << "EDIT A FORM" << endl;
	cout << Fvect[1].Fname;
	cout << "Enter the ";
	cout << "Enter 1 to go back to main: "; cin >> MainM;

	if (MainM == 1)
	{
		menuMain();
	}
	else
	{
		cout << "Error not a correct input." << endl;
		menuMain();
	}

}
void menuMain()
{

	int Pnum;
	int n = 5;


	cout << "INFORMATION FORMATTING PROGRAM" << endl;
	cout << "1. Create new form's." << endl;
	cout << "2. Edit a form." << endl;
	cout << "3. Print forms." << endl;
	cout << "4. Erase a form." << endl;
	cout << "5. Exit Program." << endl;
	cout << "Enter the action you want to take (1-5): "; cin >> Pnum;

	vector<Form> Fvect(n);

	if (Pnum == 1)
	{
		menu1st(Fvect.data());
	}
	if (Pnum == 2)
	{
		menu2nd(Fvect.data());
	}
	else
	{
		cout << "Error not a correct input." << endl;
		menuMain();
	}
}

int main()
{
    
	menuMain();

}

http://www.cplusplus.com/doc/tutorial/control/ (Iteration statements)


every time you call `menuMain()' you create another `Fvect'

also, ¿what's the point of this?
1
2
3
4
5
6
7
8
9
10
	cout << "Enter 1 to go back to main: "; cin >> MainM;
	if (MainM == 1)
	{
		menuMain();
	}
	else
	{
		cout << "Error not a correct input." << endl;
		menuMain();
	}
both paths end calling `menuMain()'
Last edited on
Ok, so I want to move the create vector line to the function that fills the vector so that when I return to the main menu it wont make a new vector. And when I do that I should just be able to use the regular address-of operator (&).

And that is just a typo, gotta remove that.
I worked out how the code works for making a vector and fixed error's I came across. I finally came to this which ran without error's but when I get to the part where I input the information into the objects I get an popup error saying that the "vector subscript is out of range". I just posted what I changed, everything else is the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void menu1st(vector<Form> &Fvect)
{
	int MainM;
	int n;

	cout << "NEW FORM(s)" << endl;
	cout << "Enter the number of forms you would like to make (Maximum of 5): "; cin >> n; cout << endl;
	for (int i = 0; i < n; i++)
	{
		cout << "First Name: "; cin >> Fvect[i].Fname; cout << endl;
		cout << "Last Name: "; cin >> Fvect[i].Lname; cout << endl;
		cout << "City: "; cin >> Fvect[i].City; cout << endl;
		cout << "Street: "; cin >> Fvect[i].Street; cout << endl;
		cout << "State: "; cin >> Fvect[i].State; cout << endl;
		cout << "Zip Code: "; cin >> Fvect[i].ZipCode; cout << endl;
	}

void menu2nd()
{
	int MainM;
	//int Fnum;

	vector<Form> Fvect;
	cout << "EDIT A FORM" << endl;
	cout << Fvect[1].Fname;
	cout << "Enter the ";
	cout << "Enter 1 to go back to main: "; cin >> MainM;

void menuMain()
{

vector<Form> Fvect;

	if (Pnum == 1)
	{
		menu1st(Fvect);
	}
	if (Pnum == 2)
	{
		menu2nd();
	}
	else
	{
		cout << "Error not a correct input." << endl;
	}
}
So that others can use this later, I'll post the answer i found.. I needed to use Fvect.resize(n); to fill the vector with default objects, I then was able to use the Fvect[i] as normal. I also needed to move my vector from the function and make it a global vector. In the for loop I use Fvect.push_back(form); to have it based off of the base object that I created with Form form;. So now it looks like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Form form;


void menuMain();

vector<Form> Fvect;

void menu1st()
{
	int MainM;
	int n;

	
	cout << "NEW FORM(s)" << endl;
	cout << "Enter the number of forms you would like to make (Maximum of 5): "; cin >> n; cout << endl;
	Fvect.resize(n);
	for (int i = 0; i < n; i++)
	{
		cout << "First Name: "; cin >> Fvect[i].Fname;
		/*cout << "Last Name: "; cin >> form.Lname; cout << endl;
		cout << "City: "; cin >> form.City; cout << endl;
		cout << "Street: "; cin >> form.Street; cout << endl;
		cout << "State: "; cin >> form.State; cout << endl;
		cout << "Zip Code: "; cin >> form.ZipCode; cout << endl;*/

		Fvect.push_back(form);
	}
> I needed to use Fvect.resize(n); to fill the vector with default objects,
> In the for loop I use Fvect.push_back(form);
let's work with ints to simplify
say that `n' is 3, after the resize you have
{0, 0, 0}
then you enter the loop and put more numbers
{0, 0, 0, 54, 13, 42}
notice the size of the array, ¿are you going to do something with the 0?
then you call `menu1st()' again, so you resize the array and have
{0, 0, 0}
losing the numbers that you've put previously.

> "vector subscript is out of range"
1
2
3
	vector<Form> Fvect;
	cout << "EDIT A FORM" << endl;
	cout << Fvect[1].Fname;
¿what were you expecting? you just created an empty vector two lines ago and now you are trying to access its second element.



Also, when a function ends it returns to its caller
1
2
3
foo():
   bar() //when bar() ends program execution will continue
   zoo() //and zoo() will be called 
your calls to `menuMain()' from `menu1st()' and `menu2nd()' are wrong.
¿want to keep asking to select an operation? use a freaking loop.

> make it a global vector.
don't
pass it as an argument to the functions that may use of it.
Topic archived. No new replies allowed.