#include <iostream>
usingnamespace std;
class Person
{
public:
string namn;
int alder;
void SkrivUt()
{
cout<<namn<<endl;
cout<<alder<<endl;
}
void SetInfo(string _namn, int _alder)
{
namn = _namn;
alder = _alder;
}
};
int linsok(Person p[], int n, int a) //algorithm for linear search.
{
for (int i = 0; i < n; i++)
{
if (p[i].alder == a)
return i;
}
return -1;
}
int tot = 4;
void bubblesort(Person p[], int n) // algorithm for bubblesort function
{
for (int i = 0; i < tot; i++)
{
int nrLeft = tot - i;
for (int j = 0; j < nrLeft; j++)
{
if (p[j] > p[j + 1])
{
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
cout<<p[i]<<endl;
}
}
int main()
{
Person familj[4];
familj[0].SetInfo("Elliot", 21);
familj[1].SetInfo("Kenneth", 51);
familj[2].SetInfo("Vicky", 48);
familj[3].SetInfo("Oliver", 18);
bubblesort(familj[], 4);
linsok(familj[], 4, 21);
}
To get more replies, always use code tags, and make sure your code is indented. When you say errors, do you mean compile errors/warnings? If so paste the full text of them here, verbatim.
The error on line 46 is because you are trying to compare a class to a class when you need to be comparing an int to an int. if (p[j].alder > p[j + 1].alder) would be the correct way.
On line 48 you define temp as an int and then try to set it equal to a class. This will not work. should be defined as Person temp = p[j] this will also correct the problem on line 50.
Line 57 is trying to output the entire class when it should be outputting the namn or alder i.e., cout << p[i].namn << endl;.
Lines 69 and 70 you need to remove the [] from the function calls.