#include <iostream>
#include <string>
usingnamespace 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");
}
#include <iostream>
#include <string>
usingnamespace 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");
}
#include <iostream>
#include <string>
usingnamespace 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");
}
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
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:
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