Please help me to fix the problem

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] 

Anyone can help me to figure this out?


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
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;
}

Last edited on
Your problem is in Group::get_insert().

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.
Last edited on
That's another good find.

You should make a constructor for Group

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;
}
int Group::find_index(string name){
    for(int i=0;i<size; i++){
        if(people[i].getName() == name){
            return i;
        }
    }
    return -1;
}


Another one is in Group::insert_person()
1
2
3
4
5
6
7
8
    // 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:

1
2
3
4
5
6
7
if(size >= GROUP_SIZE)
{
     return false;
}

people[size] = name;
size++;


Does your code serve a purpose that I am being too lazy to decipher?
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Group::get_insert(string & name, int & age){
    cin >> name >> age;
    insert_person(name, age);

    //string n;
    //int a;
    //int i=0;

    //cin >> n;
    //people[i].setName(n);
    //cin >> a;
    //people[i].setAge(a);
    //name = people[i].getName();
    //age = people[i].getAge();
}


cire,

I removed the code in get_insert(), and it worked now.

1
2
3
4
void Group::get_insert(string & name, int & age){
    cin >> name >> age;
}


only the cin is needed.
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);
Topic archived. No new replies allowed.