need some help with pointers and arrays

Having an issue displaying the contents of the loaded array using a pointer. Below i've posted my code. This is a homework assignment so i'd rather not get the answer but more so an explanation. When i run the program, it prints all 2's.

run the code and select menu item #1. input 1,2,3,4,5 for testing. then select menu item #2 and it will output 2's for all addresses

any help is appreciated

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

    #define down3 "\n\n\n";
    #define tab2 "\t\t";
    const int SIZE = 5;

    using namespace std;

    int getMenuItem();
    void menuOptions();
    void enterRents(int rent[]);
    void displayRents(int* pointer, int rent[]);

    int main()
    {
	int rent[SIZE];
	int* pointer = &rent[0];
	int choice;
	bool quit = false;


	while (quit == false)
	{
		choice = getMenuItem();

		if (choice == 1)
		{
			enterRents(rent);

		}
		else if (choice == 2)
		{
			displayRents(pointer, rent);
		}
		else if (choice == 3)
		{

		}
		else if (choice == 4)
		{

		}
		else if (choice == 5)
		{
			cout << "\n\nYou have chosen to exit the program. Goodbye!";
			quit = true;
		}
		else
		{
			cout << "\nInvalid entry. Try again.";
		}
	}



	cout << down3;
	system("Pause");
	return(0);
    }

    int getMenuItem()
    {

	int choice = 0;
	menuOptions();

	cout << "\nSelect a numbered option from the following menu: ";
	cin >> choice;

	return(choice);
    }

    void menuOptions()
    {
	cout << "\n1 - Enter rent amounts";
	cout << "\n2 - Display rent amounts";
	cout << "\n3 - Sort rent amounts from low to high";
	cout << "\n4 - Display memory locations";
	cout << "\n5 - Quit program" << endl << endl;
    }

    void enterRents(int rent[])
    {
	int loopCount = 0;

	for (int x = 0; x < SIZE; x++)
	{
		cout << "\nEnter the rent amount for property #" << ++loopCount << ": ";
		cin >> rent[x];
	}
    }

    void displayRents(int* pointer, int rent[])
    {
	int count = 0;
	cout << "\nDisplaying the rent values:" << endl << endl;

	for (int x = 0; x < SIZE; x++)
	{
		cout << "\nProperty #" << ++count << ": " << *(pointer + 1);
	}

    }
Hi, your code seems alright. The only thing that needs changing is line 102 where you are always adding by 1. Instead of that, try adding by index x.

1
2
3
4
for (int x = 0; x < SIZE; x++)
	{
		cout << "\nProperty #" << ++count << ": " << *(pointer + x);
	}


EDIT: Sorry, did not explain. In the first iteration. every time you go *(pointer + 1), the pointer points to the 2nd element of the array since you are advancing it by just 1. Then the next iteration, the pointer will also point to the 2nd element because the pointer by default will always point to the first element of the array and you are only adding one to it, so it is still [0] + [1]. So you want a way to say pointer + 1, then pointer + 2, then pointer + 3, .... n - 1. Hope that made sense.
Last edited on
@Uk Marine - This worked! What you said makes sense and i see now why i need to increment by index.

Thank you!
Topic archived. No new replies allowed.