Resizing Array Function Error

Mind you the code it self is incomplete but im having problems with my function.

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
#include <iostream>
#include <string>
using namespace std;


////Write a program that lets users keep track of the last time they talked to each of their friends.
////Users should be able to add new friends (as many as they want!) and store the number of days ago that 
////they last talked to each friend. Let users update this value (but don't let them put in
////bogus numbers like negative values). Make it possible to display the list sorted by the names of
////the friends of by how recently it was since they talked to each friend.



class friendsAgo
{
public:
	string name;
	int daysAgo;

	void friendsAgo::resizeArr(friendsAgo *friends[], int size); 
};


void friendsAgo::resizeArr(friendsAgo *friends[],int size, int newSize)
{
		friendsAgo *friends = new friendsAgo[size];
		
		// Get a new array size- ERROR HERE
		friendsAgo *newFriendsAgo = new friendsAgo[newSize];
		// Copy elements
		for (int i = 0; i < size; i++) {
		  newFriendsAgo[i] = friends[i]; // its saying newFriendsAgo is not a pointer so it cant = friends[i]
		}
		// Delete friends ago
		delete[] friends;
		// Use friends ago again.
		friends = newFriendsAgo;
}

int main ()
{
	int entry(4);
	int menuSel;
	int newFriends(0);
	friendsAgo *friends = new friendsAgo[entry];
	
	cout << "How many friends do you want to add? " << endl;
	cin >> entry;
	resizeArr(&friends, entry, newFriends);

	do{
	cout << "Main Menu" << endl;
	cout << "1. Enter new friends. " << endl;
	cout << "2. Add friends' info. " << endl;
	cout << "3. Print the list of friend's names and the last time spoken." << endl;
	cin >> menuSel;
	}while(menuSel < 1 && menuSel > 3);

	switch(menuSel)
	{
	case 1:
		cout << "How many new friends would you like to add?" << endl;
		cin >> newFriends;
		for(int j=1 ; j <= newFriends; j++)
		{
			cout << "Please enter the Name of your friend: " << endl; /// entry += newFriends?
			getline(cin, friends[entry+j].name);
			cout << "How long ago did you speak to: " << friends[j].name << "?" << endl;
			cin >> friends[entry+j].daysAgo;
		}
		break;
	case 2:
		for(int k=0; k < (entry+newFriends); k++)
		{
			cout << "Please enter the Name of your friend: " << endl;
			getline(cin, friends[k].name);
			cout << "How long ago did you speak to: " << friends[k].name << "?" << endl;
			cin >> friends[k].daysAgo;
		}
	case 3:
			for(int i=0; i<entry; i++)
			{
				cout << "Name: " << friends[i].name << "\tspoken to you: " << friends[i].daysAgo << endl;
			}
			break;
	default: 
			cout << "Invalid Selection!" << endl;
	}


system("pause");
}
Last edited on
friendsAgo *friends[] ← you have an array of pointers here
friendsAgo *newFriendsAgo ← and an array of objects here
How would i go about fixing this? im new to c++. This is my current version:

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
#include <iostream>
#include <string>
using namespace std;


////Write a program that lets users keep track of the last time they talked to each of their friends.
////Users should be able to add new friends (as many as they want!) and store the number of days ago that 
////they last talked to each friend. Let users update this value (but don't let them put in
////bogus numbers like negative values). Make it possible to display the list sorted by the names of
////the friends of by how recently it was since they talked to each friend.



class friendsAgo
{
public:
	string name;
	int daysAgo;

	void friendsAgo::resizeArr(friendsAgo *friends, int size, int newSize); 
};


void friendsAgo::resizeArr(friendsAgo *friends,int size, int newSize)
{
		friendsAgo *friends = new friendsAgo[size];
		
		// Get a new array size
		friendsAgo *newFriendsAgo = new friendsAgo[newSize];
		// Copy elements
		for (int i = 0; i < size; i++) {
		  newFriendsAgo[i] = friends[i]; // its saying newFriendsAgo is not a pointer so it cant = friends[i]
		}
		// Delete friends ago
		delete[] friends;
		// Use friends ago again.
		friends = newFriendsAgo;
}

int main ()
{
	int entry(4);
	int menuSel;
	int newFriends(0);
	friendsAgo *friends = new friendsAgo[entry];
	
	cout << "How many friends do you want to add? " << endl;
	cin >> entry;
	friends->resizeArr(friends, entry, newFriends);

	do{
	cout << "Main Menu" << endl;
	cout << "1. Enter new friends. " << endl;
	cout << "2. Add friends' info. " << endl;
	cout << "3. Print the list of friend's names and the last time spoken." << endl;
	cin >> menuSel;
	}while(menuSel < 1 && menuSel > 3);

	switch(menuSel)
	{
	case 1:
		cout << "How many new friends would you like to add?" << endl;
		cin >> newFriends;
		for(int j=1 ; j <= newFriends; j++)
		{
			cout << "Please enter the Name of your friend: " << endl; /// entry += newFriends?
			getline(cin, friends[entry+j].name);
			cout << "How long ago did you speak to: " << friends[j].name << "?" << endl;
			cin >> friends[entry+j].daysAgo;
		}
		break;
	case 2:
		for(int k=0; k < (entry+newFriends); k++)
		{
			cout << "Please enter the Name of your friend: " << endl;
			getline(cin, friends[k].name);
			cout << "How long ago did you speak to: " << friends[k].name << "?" << endl;
			cin >> friends[k].daysAgo;
		}
	case 3:
			for(int i=0; i<entry; i++)
			{
				cout << "Name: " << friends[i].name << "\tspoken to you: " << friends[i].daysAgo << endl;
			}
			break;
	default: 
			cout << "Invalid Selection!" << endl;
	}


system("pause");
}
Last edited on
bump? anyone?
On line 26: friends hides/shadows the paramter friends. So the parameter is not used.

Line 24 of you first code was correct, but not the current one.

Rename friends on line 26 as oldFriends or so (in order to delete it on line 35).

Line 37 is supposed to look like this:

(*friends) = new friendsAgo[size];

all pevious friends require the * as well
Tyvm!

now im having trouble understanding what line 26 does since we changed it to oldfriends.

Its compiling but breaking half way and not sure what im doing wrong.

Here my current code:

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
#include <iostream>
#include <string>
using namespace std;


////Write a program that lets users keep track of the last time they talked to each of their friends.
////Users should be able to add new friends (as many as they want!) and store the number of days ago that 
////they last talked to each friend. Let users update this value (but don't let them put in
////bogus numbers like negative values). Make it possible to display the list sorted by the names of
////the friends of by how recently it was since they talked to each friend.



class friendsAgo
{
public:
	string name;
	int daysAgo;

	void friendsAgo::resizeArr(friendsAgo *friends[], int size, int newSize); 
};


void friendsAgo::resizeArr(friendsAgo *friends[],int size, int newSize)
{
		friendsAgo *oldFriends = new friendsAgo[size]; /// whats the point of having this now?
		
		// Get a new array size- ERROR HERE
		friendsAgo *newFriendsAgo = new friendsAgo[newSize];
		// Copy elements
		for (int i = 0; i < size; i++) {
		  newFriendsAgo[i] = *friends[i]; // its saying newFriendsAgo is not a pointer so it cant = friends[i]
		}
		// Delete friends ago
		delete[] *friends;
		// Use friends ago again.
		*friends = newFriendsAgo;
}

int main ()
{
	int entry(4);
	int menuSel;
	int newFriends(0);
	friendsAgo *friends = new friendsAgo[entry];
	
	cout << "How many friends do you want to add? " << endl;
	cin >> entry;
	friends->resizeArr(&friends, entry, newFriends);

	do{
	cout << "Main Menu" << endl;
	cout << "1. Enter new friends. " << endl;
	cout << "2. Add friends' info. " << endl;
	cout << "3. Print the list of friend's names and the last time spoken." << endl;
	cin >> menuSel;
	}while(menuSel < 1 && menuSel > 3);

	switch(menuSel)
	{
	case 1:
		cout << "How many new friends would you like to add?" << endl;
		cin >> newFriends;
		for(int j=1 ; j <= newFriends; j++)
		{
			cout << "Please enter the Name of your friend: " << endl; /// entry += newFriends?
			getline(cin, friends[entry+j].name);
			cout << "How long ago did you speak to: " << friends[j].name << "?" << endl;
			cin >> friends[entry+j].daysAgo;
		}
		break;
	case 2:
		for(int k=0; k < (entry+newFriends); k++)
		{
			cout << "Please enter the Name of your friend: " << endl;
			getline(cin, friends[k].name);
			cout << "How long ago did you speak to: " << friends[k].name << "?" << endl;
			cin >> friends[k].daysAgo;
		}
	case 3:
			for(int i=0; i<entry; i++)
			{
				cout << "Name: " << friends[i].name << "\tspoken to you: " << friends[i].daysAgo << endl;
			}
			break;
	default: 
			cout << "Invalid Selection!" << endl;
	}


system("pause");
}
Last edited on
Look at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
void friendsAgo::resizeArr(friendsAgo *friends[],int size, int newSize)
{
		friendsAgo *oldFriends = *friends;
		
		(*friends) = new friendsAgo[newSize];
		int min_size = min(size, newSize); // ensures that it doesn't go out of bounds
		// Copy elements
		for (int i = 0; i < min_size; i++) {
		  (*friends)[i] = oldFriends[i];
		}
		// Delete friends ago
		delete[] oldFriends;
}


if newSize (in your case it is 0 -> line 44) is smaller than size your program will crash, hence I'd suggest th 'old' approach instead of new.

By the way: Your program will crash when you access friends due to line 44
ty for the help thus far,

The code finally compiled, but if I remove the 0 in friends it still crashes. The only reason its there is so I can run it through the function.

friends->resizeArr(&friends, entry, newFriends);


otherwise I wouldnt even use it
Well, you have some problems to grasp the logic. You need to resize the array whenever the user wants to add friends, ie after line 63. So I revamped your code somewhat:
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
int main ()
{
	int entry(0); // No friends at the beginning
	int menuSel;
	int newFriends(0);
	friendsAgo *friends = NULL; // No friends at the beginning

	// This will be done later
	cout << "How many friends do you want to add? " << endl;
	cin >> entry;
	friends->resizeArr(&friends, entry, newFriends);

	do{
	cout << "Main Menu" << endl;
	cout << "1. Enter new friends. " << endl;
	cout << "2. Add friends' info. " << endl;
	cout << "3. Print the list of friend's names and the last time spoken." << endl;
	cin >> menuSel;
	}while(menuSel < 1 && menuSel > 3);

	switch(menuSel)
	{
	case 1:
		cout << "How many new friends would you like to add?" << endl;
		cin >> newFriends;
		friends->resizeArr(&friends, entry, entry + newFriends); // This is the point where you need to resize the array
		for(int j=0 ; j < newFriends; j++) // Don't go out of bounds
		{
			cout << "Please enter the Name of your friend: " << endl; /// entry += newFriends?
			getline(cin, friends[entry+j].name);
			cout << "How long ago did you speak to: " << friends[j].name << "?" << endl;
			cin >> friends[entry+j].daysAgo;
		}
		entry += newFriends; // At this point the friends are added
		break;
	case 2:
		for(int k=0; k < (entry+newFriends); k++) // entry contains the number of friends already (even if 0)
		{
			cout << "Please enter the Name of your friend: " << endl;
			getline(cin, friends[k].name);
			cout << "How long ago did you speak to: " << friends[k].name << "?" << endl;
			cin >> friends[k].daysAgo;
		}
	case 3:
			for(int i=0; i<entry; i++)
			{
				cout << "Name: " << friends[i].name << "\tspoken to you: " << friends[i].daysAgo << endl;
			}
			break;
	default: 
			cout << "Invalid Selection!" << endl;
	}


system("pause");
}
Not tested!

By the way: With the code above you can even remove friends when entering a negative value
tyvm for your help, it works fine up until you click change friends info first.

Ill mess around with it some more, and make sure it loops back to main menu.

ill let you know how it goes.
Topic archived. No new replies allowed.