Hi, I'm new to programming. I have an assignment, but there is a bug.
When i run it on my own laptop, it is executable. But a problem is everytime i enter a new name, it will replace the first name. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13
>1 Mike 40
>3
Mike,40
>1 Alex 30
>3
Alex,30
Alex,30
>1 Jessica 25
>3
Jessica,25
Alex,30
Jessica,25
....
When i run it on the university computer,it shows:
1 2 3 4 5 6 7
> 1 Mike 30
Segmentation fault (core dumped)
sun01:5%
[/Mike]
void Group::get_insert(string & name, int & age){
string n;
int a;
int i;
cin >> n;
people[i].setName(n);
cin >> a;
people[i].setAge(a);
name=people[i].getName();
age=people[i].getAge();
}
void Group::get_delete(string & name){
cin >> name;
}
/////////////////////////////////////////////////////////////////
// Hint Hint, This comment separates logical chunks of code... //
/////////////////////////////////////////////////////////////////
int Group::find_index(string name){
int index = -1;
for(int i=0;i<size; i++){
if(people[i].getName() == name){
index = i;
i = size;
}
}
return index;
}
int Group::insert_person(string name, int age){
// If our group is full, we cannot add anymore
if(size == GROUP_SIZE) return 0;
// Find where we need to insert the person
int index = size;
for(int i=0;i<size; i++){
if(people[i].getName().compare(name) > 0){
index = i;
i = size;
}
}
// Move everyone else down one space to make room
for(int i=size;i>index;i--){
people[i]=people[i-1];
}
// Insert the person
people[index].setName(name);
people[index].setAge(age);
size++;
return 1;
}
You don't initialize i. Even if you did initialize it to 0 (which is what is happening on your computer in debug mode). You'd just write to the first element
I can't see where you initialize the private 'size' variable for your 'Group' class and you are defiantly using it in your loops. Some compilers will set an uninitialized integer to 0 but don't rely on that behavior.
This would work (plus you need to add it to your header): Group::Group() : size(0) {}
Here's another function that could be working, but could really be improved:
1 2 3 4 5 6 7 8 9 10
int Group::find_index(string name){
int index = -1;
for(int i=0;i<size; i++){
if(people[i].getName() == name){
index = i;
i = size;
}
}
return index;
}
// Find where we need to insert the person
int index = size;
for(int i=0;i<size; i++){
if(people[i].getName().compare(name) > 0){
index = i;
i = size;
}
}
// Find where we need to insert the person
int index = size;
for(int i=0;i<size; i++){
if(people[i].getName().compare(name) > 0){
index = i;
break;
}
}
I initialized the i and add the group() constructor. Now i can run it on the university computer, but the result is still the same as it on my own laptop. The first array sill be changed everytime. Any more suggestions?
Instead of strolling through the array in your "insert_person()" function, why not just use the 'size' variable to keep track of where you are inserting the next person? That function could just be:
I initialized the i and add the group() constructor.
As previously mentioned, just initializing i is not enough.
Both get_insert and insert_person are doing the same job.. inserting a person into the group, and you're calling both for each person that you insert.
get_insert also attempts to get input from the user which, should not be a job for Group, but if you want to keep get_insert I imagine it should be implemented in terms of insert_person:
Then you should probably remove the "insert" from the name as it is a bit misleading now, don't you think? Better yet, get rid of the function altogether and change your main from:
21 22
g.get_insert(temp_name, temp_age);
int success = g.insert_person(temp_name, temp_age);
to
21 22
cin >> temp_name >> temp_age;
int success = g.insert_person(temp_name, temp_age);