Alright, i am not quite sure how to manage this but i am kinda stuck to get this program to work. so i am looking for comments on it, its probebly aloth of errors and faults inside this. but i appriciate every comment i can get on this to improve myself.
my task is to make a class that holds a name and a age of a person.
make a function that writes that information out.
make a function that can linear search in a vector with a person object after the age.
make a function that bubblesort in the person vector after the age of the person.
And finaly test my class by making a family "Person familj[4];"
with 4 persons with diffrent names and ages and the program should sort
the people by there age and last call the linear function to search for a family members age.
i feel kinda lost this is how my code looks so far
#include <iostream>
#include <string>
usingnamespace std;
class person //<---- class that contains name and age of a person
{
public:
string namn;
int alder = 0;
staticvoid skrivut(string namn, int alder);
person(string _name, int _age);
};
void person::skrivut(string namn, int alder) { //<---- writes out name and age
cout << "Ålder: " << alder << "Namn: " << namn << endl;
}
person::person(string _name, int _age) { //<--- constructor
namn = _name;
alder = _age;
}
int linsok(person person[], int n, int a) { //<<----- Linear searcher
for (int i = 0; i < n; i++)
{
if (a == person[i]) //<--- error
{
return i;
}
}
return -1;
}
void bubblesort(person person[], int n) { //Bubblesorting the persons after age? how to do it?
bool not_sorted = true;
int j = 1, tmp;
while (not_sorted) {
not_sorted = false;
j++;
for (int i = 0; i < n - j; i++) {
if (person[i] > person[i + 1]) { //<--- error ">"
tmp = person[i]; //<--- error
person[i] = person[i + 1];
person[i + 1] = tmp; //<--- error operand types are person = int
not_sorted = true;
}//end of if
}//end of for loop
}//end of while loop
}
int main() {
int person[6] = { 4, 234, 67, 23, 69, 34 };
int uservalue;
cout << "Skriv in åldern du letar efter: " << endl;
cin >> uservalue;
int result = linsok(person, 4, uservalue); //<<----- error
if (result >= 0) {
cout << "Åldern " << person[result] << " was found at the element with index " << result << endl;
}
else {
cout << "the number " << uservalue << " was not found. " << endl;
}
person familj[4]; //<<<---- how do decorate a vector familj with 4 persons inside?
}
you need to access to the object field with . (dot).
line 46 for instance would be : if(person[i].age > person[i + 1].age)
line 62 confusing your array name is person like the class , but it has integers ????
line 30 you try to compare two objects (not pointers in this case where it would have work) , you need to implement the operator == in your structure.
line 9 , might be legal now , but initialize it in the constructor initializer list , instead.
Prefer make the two function as method member like linearSearch() and bubbleSort(), use good name for parameter -> "n" and "a" means nothing for anyone else , you might lose points for that.
line 78 are you sure you array is initialized ? Use aggregation for this case.
line 79 , call the functions and verify the values.
Person instead of person , use capital for classes.
void bubblesort(person person[].alder, int n) { //Bubblesorting the persons after age? how to do it?
bool not_sorted = true;
int j = 1, tmp;
while (not_sorted) {
not_sorted = false;
j++;
for (int i = 0; i < person[].size - j; i++) { // <-- error person[].size - j
if (person[i].alder > person[i + 1].alder) {
tmp = person[i].alder;
person[i].alder = person[i + 1].alder;
person[i + 1].alder = tmp;
not_sorted = true;
}//end of if
}//end of for loop
}//end of while loop
}
void bubblesort(person person[].alder, int n) { //i get an error here at the DOT "Expected a )"
for (int i = 0; i < person[].size - j; i++) { //also an error here "expected an expression"
line 62 you can ignore i removed it
line 30 you need to implement the operator == in your structure. (not quite sure what you mean?)
line 9 how do you mean? i get errors if i try to put "alder = 0" inside the constructor
make them method members? to the class?
Is this how i make the linear search a member of the class?
1 2 3 4 5 6 7
public:
string namn;
int alder = 0;
staticvoid skrivut(string namn, int alder);
person(string _name, int _age);
int linsok(Person person[], int n, int a); //<----- add this inside class?
};
and line 78
Person familj[4]{"olle", 5, "mia", 12 , "rolf", 38, "lina", 29}; //<<<---- how do decorate a vector familj with 4 persons inside?
#include <iostream>
#include <string>
usingnamespace std;
class Person //<---- class that contains name and age of a person
{
public:
string namn;
int alder = 0;
staticvoid skrivut(string namn, int alder);
void person(string _name, int _age) //<--- constructor
{
namn = _name;
alder = _age;
}
int linsok(Person person[], int n, int a) //<<----- Linear searcher
{
for (int i = 0; i < n; i++)
{
if (a == person[i]) //<--- error how do i fix this?
{
return i;
}
}
return -1;
}
void skrivut(string namn, int alder){
cout << "Ålder: " << alder << "Namn: " << namn << endl;
}
void bubblesort(Person person[], int n) { //Bubblesorting the persons after age?
bool not_sorted = true;
int j = 1, tmp;
while (not_sorted) {
not_sorted = false;
j++;
for (int i = 0; i < n - j; i++) {
if (person[i].alder > person[i + 1].alder) {
tmp = person[i].alder;
person[i].alder = person[i + 1].alder;
person[i + 1].alder = tmp;
not_sorted = true;
}//end of if
}//end of for loop
}//end of while loop
}
};
int main() {
int uservalue;
cout << "Skriv in åldern du letar efter: " << endl;
cin >> uservalue;
Person pob;
int result = pob.linsok;
if (result >= 0) {
cout << "Åldern " << uservalue << " was found " << result << endl;
}
else {
cout << "Åldern " << uservalue << " was not found. " << endl;
}
Person family[4] = { { "olle", 5 }, { "mia", 12 }, { "rolf", 38 }, { "lina", 29 }//<<<---- how do decorate a vector familj with 4 persons inside?
};
}
This is not working i get the following error - no person constructer match the list.
Person family[4] = {{"olle", 5},{"mia", 12},{"rolf", 38},{"lina", 29}};