Can't find the problem

Hello. My code should show 4 persons names and age, but now its repeat it twice like:

Name: Alex and Age: 20
Name: Alex and Age: 20

instead of just once, also the message at the end "Found no person with the specified age" is also showing, and i dont now why.

Any kind soul who wants to help?

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

using namespace std;

const int Amoutfamily = 4; // How big the family is.

// ClASS:
class Person{
public:
    string name;
    int age;

    void Print(){
        cout << "Name: " << name << " and Age: " << age << endl;


    }
};

// Function: LinearSearch: Search for a person in a list
int LinearSearch(Person family[], int Amoutfamily, int a){
    int n = Amoutfamily;
    for(int i = 0; i < n; i++){ // Går igenom hela listan.
        // Is it the number we are searching for?
        if(family[i].age == a)
            return i;
    }
    return -1; // Person were not found
}

void swap(Person &p, Person &q){ // Need this in the code
    Person temp;
    temp.name = p.name;
    temp.age = p.age;
    p.name = q.name;
    p.age = q.age;
    q.name = temp.name;
    q.age = temp.age;
}

// BubbleSort, sorting Person by age
void bubblesort(Person family[], int Amoutfamily){
    // The outer loop, going through all list
    for(int i = 0; i < Amoutfamily; i++){

        //Inner loop, going through element by element
        int nrleft = Amoutfamily - i;  // To se how many has been gone through

        for(int j = 0; j < nrleft; j++){
            if(family[j].age > family[j+1].age){ // Compare the elements
               swap(family[j], family[j+1]);
            }
        }
    }
    // Write the list:
    for(int i = 0; i < Amoutfamily; i++){
        family[i].Print();
    }
}


// FUNCTION: Main, start the program
int main(){
  Person family[Amoutfamily];


    // Create a list and fill with persons
    family[0].name = "Mats";
    family[0].age = 52;
    family[1].name = "Carina";
    family[1].age = 48;
    family[2].name = "Alex";
    family[2].age = 20;
    family[3].name = "Emily";
    family[3].age = 18;


  // Sort the persons in the list and print them in order of increasing age:
  bubblesort(family, Amoutfamily);
    for(int i = 0; i < Amoutfamily; i++){
        family[i].Print();
    }

int findIndex = LinearSearch(family, Amoutfamily, 4);
    if(findIndex >= 0){
        family[findIndex].Print();
    }
    else{
        cout << "Found no person with the specified age." << endl;
    }

    cin.get();
    return 0;
}
You Print() on lines 80 and 82.

None on the family has age==4.
wow, i forgot the 8, = 48 !

but it still writes the names and age twice? I can't figured whats wrong.
Lines 51 and 52 dereference family[j+1]. That can be an out-of-range error; index too large; access to outside of the array.
but it still writes the names and age twice?

As Keskiverto said, you print the names and ages twice: once at the end of bubblesort(), and again at lines 81-83.
Thanks for all replies! i have got it to work :)
Topic archived. No new replies allowed.