Changing Values With References and Iterators

I want the user to be able to select a number from the array that will select the item, and change it using references. So far I have been able to get it to change with iterators, but the references make it so it changes the first value in the array, when I only want it to change the one they've selected.

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
#include <iostream>
#include <vector>
#include <string>

using namespace std;

void display(const vector<string>& inventory);
string& refToElement(vector<string>& inventory, int i); 

int main()
{
	//Introduction
	cout << "\tPassing Returning References";
	cout << "\nPlease add 5 favorite characters.";

	//Define
	string item;
    vector<string> inventory;
	char again = 'y';
	unsigned int i=0;

	//Add 5 Characters
	while (again == 'y') 
	{	
		if (i != 5)
		{
			cout << "\n\nAdd a favorite character: ";
				getline (cin, item);
				inventory.push_back(item);
				++i;
		}

		else
		{	cout << endl;
			break;	}
	}

	//Display
    display(inventory);

	//Define iterators
	vector<string>::iterator myIterator;
	vector<string>::const_iterator iter;

	//Select number
	int favorite_num;
	cout << "\nSelect the number to change: ";
					cin >> favorite_num;
					cin.ignore();		

	//Create iterator
	myIterator = inventory.begin() + favorite_num - 1;
	
	//displays string that the returned reference refers to 
	string change = refToElement(inventory, 0);
    cout << "Sending the returned reference to cout:\n";   
    cout << change << "\n\n";

    cout << "Assigning the returned reference to another reference.\n";
    string& rStr = refToElement(inventory, 1); 
    cout << "Sending the new reference to cout:\n";
    cout << rStr << "\n\n";

    cout << "Assigning the returned reference to a string object.\n";
    string str = refToElement(inventory, 2);
    cout << "Sending the new string object to cout:\n";
    cout << str << "\n\n";
    
    cout << "Altering an object through a returned reference.\n";
	string variable;
	cout << "What do you want alter your favorite to?: ";
	getline (cin, variable);
	*myIterator = variable;
    rStr = variable;
    cout << "Sending the altered object to cout:\n";
    cout << rStr << endl;

	//Display
	display(inventory);

    return 0;

}

void display(const vector<string>& vec)
{
    cout << "Your items:\n";
    
    for (vector<string>::const_iterator iter = vec.begin(); 
         iter != vec.end(); ++iter)
	{
         cout << *iter << endl;
	}
}

string& refToElement(vector<string>& vec, int i)
{
    return vec[0];
}
the references make it so it changes the first value in the array

That's because you've written refToElement such that it only ever returns a reference to vec[0].
Last edited on
Topic archived. No new replies allowed.