Need a function to do an output

Want to do a function Linearsearch and need to know how to search on age and print it.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  // Bubblesort.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>
#include <string>

using namespace std; 

class Person
{
public:
	Person() : name(""), age(0) {}
	Person(const string &_name, int _age) : name(_name), age(_age) {}
	Person(const Person &rhs) : name(rhs.name), age(rhs.age) {}
	Person operator=(const Person &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		age = rhs.age;
		return *this;
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _age)
	{
		name = _name;
		age = _age;
	}
	
	const string &Name() const { return name; }
	int Age() const { return age; }
	
private:
	string name;
	int age;
};

//Function Declarations
void BubbleSort(vector<Person> &myList);
void swap(Person &p, Person &q);
int LinearSearch(vector<Person> &myList, &int);

// Function: Main, start on program
int main()
{
	//Create a list with persons and fill it:
	  
	vector<Person> myList;
	cout << "size of myList is " << myList.size() << endl;
	myList.push_back(Person("Charles", 22));
	myList.push_back(Person("Darwin", 21));
	myList.push_back(Person("Phytagoras", 75));
	myList.push_back(Person("Michelle", 65));
	myList.push_back(Person("Carl", 76));
	myList.push_back(Person("Cathrine", 54));
	myList.push_back(Person("Marielle", 25));
	myList.push_back(Person("Patrick", 85));
	myList.push_back(Person("Oliver", 48));
	myList.push_back(Person("Elinette", 52));

	int index = LinearSearch(myList, 75); //

	//Write out searchresults
	if(index == -1)
		cout << "Person wasn´t found!";
	else
		cout << "The person you are looking for is named" << myList[index].Name() << " and is on index " << index << "\n\n";
	
	cout << "\nsize of myList is " << myList.size() << endl;

	cout << "\nBefore sort:" << endl;
	for (int i = 0; i < myList.size(); ++i)
	{
		cout << myList[i].Name() << " " << myList[i].Age() << endl;
	}
	
	BubbleSort(myList);
	cout << "\nAfter sort:" << endl;
	for (int i = 0; i < myList.size(); ++i)
	{
		cout << myList[i].Name() << " " << myList[i].Age() << endl;
	}
}

//Function Definitions

void BubbleSort(vector<Person> &myList)
{
	// The outer loop, going through all list
    for(int i = 0; i < myList.size(); i++)
	{
		//Inner loop, going through element by element
		int nrLeft = myList.size() - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft - 1; j++)
		{
			
			if (myList[j].Age() > myList[j+1].Age()) // Compare the elements
			{
				swap(myList[j], myList[j+1]); 
			}
		}
	}
}

void swap(Person &p, Person &q) 
{ 
	Person temp = p; 
	p = q;
	q = temp;
}

// Function: LinearSearch: Search for a person in a list
int LinearSearch(vector<Person> &myList, int key)
{
	for (int i = 0; i < 10; i++) //Go through the whole list
	{
		// Is it the number we are searching for?
		if (myList[i].Age == key)
			return i;
	}
	return -1; // Person were not found
}


This part is the one i want help with

1
2
3
4
5
6
7
8
9
10
// Function: LinearSearch: Search for a person in a list
int LinearSearch(vector<Person> &myList, int key)
{
	for (int i = 0; i < 10; i++) //Go through the whole list
	{
		// Is it the number we are searching for?
		if (myList[i].Age == key)
			return i;
	}
	return -1; // Person were not found 
line 46: you don't need the & on the final int parameter.

line 123: you're trying to directly access Age as a variable, it is a member function.

line 120: might be better to do for (int i = 0; i < myList.size()-1; i++)
Last edited on
At line 123, How can I Access the age in the vector?
if( myList[i].Age() == key )
Function: LinearSearch

Does this code work? I cant find anythiong wrong with it but Visual refuses to do LinearSearch output

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

// Bubblesort.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>
#include <string>

using namespace std; 

class Person
{
public:
	Person() : name(""), age(0) {}
	Person(const string &_name, int _age) : name(_name), age(_age) {}
	Person(const Person &rhs) : name(rhs.name), age(rhs.age) {}
	Person operator=(const Person &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		name = rhs.name;
		age = rhs.age;
		return *this;
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const string &_name, int _age)
	{
		name = _name;
		age = _age;
	}
	
	const string &Name() const { return name; }
	int Age() const { return age; }
	
private:
	string name;
	int age;
};

//Function Declarations
void BubbleSort(vector<Person> &myList);
void swap(Person &p, Person &q);
int LinearSearch(vector<Person> &myList, int);

// Function: Main, start on program
int main()
{
	//Create a list with persons and fill it:
	  
	vector<Person> myList;
	cout << "size of myList is " << myList.size() << endl;
	myList.push_back(Person("Charles", 22));
	myList.push_back(Person("Darwin", 21));
	myList.push_back(Person("Phytagoras", 75));
	myList.push_back(Person("Michelle", 65));
	myList.push_back(Person("Carl", 76));
	myList.push_back(Person("Cathrine", 54));
	myList.push_back(Person("Marielle", 25));
	myList.push_back(Person("Patrick", 85));
	myList.push_back(Person("Oliver", 48));
	myList.push_back(Person("Elinette", 52));

	int index = LinearSearch(myList, 75); //

	//Write out searchresults
	if(index == -1)
	{
		cout << "Person wasn´t found!";
	}
	else
	{
		cout << "The person you are looking for is named" << myList[index].Name() << " and is on index " << index << "\n\n";
	}
	cout << "\nsize of myList is " << myList.size() << endl;

	cout << "\nBefore sort:" << endl;
	for (int i = 0; i < myList.size(); ++i)
	{
		cout << myList[i].Name() << " " << myList[i].Age() << endl;
	}
	
	BubbleSort(myList);
	cout << "\nAfter sort:" << endl;
	for (int i = 0; i < myList.size(); ++i)
	{
		cout << myList[i].Name() << " " << myList[i].Age() << endl;
	}
}

//Function Definitions

void BubbleSort(vector<Person> &myList)
{
	// The outer loop, going through all list
    for(int i = 0; i < myList.size(); i++)
	{
		//Inner loop, going through element by element
		int nrLeft = myList.size() - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft - 1; j++)
		{
			
			if (myList[j].Age() > myList[j+1].Age()) // Compare the elements
			{
				swap(myList[j], myList[j+1]); 
			}
		}
	}
}

void swap(Person &p, Person &q) 
{ 
	Person temp = p; 
	p = q;
	q = temp;
}

// Function: LinearSearch: Search for a person in a list
int LinearSearch(vector<Person> &myList, int key)
{
	for (int i = 0; i < myList.size()-1; i++) //Go through the whole list
	{
		// Is it the number we are searching for?
		if (myList[i].Age() == key)
			return i;
	}
	return -1; // Person were not found 
}
compiles and runs fine on my system. VS 2013

also runs in the shell (gear icon in top right of your code post).
Last edited on
Yes, I got it to work. Thank you!

/P
Topic archived. No new replies allowed.