Search function help

Hello folks!

I have been struggling with this problem since daaays back and can't find a solution to it so I hope somebody could bring some light to my novice script.

My question is if anybody could tell me a way to search for the title or type of the movies in the SetInfo part based on input from the user.

Thanks in advance!

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

#include <iostream>
using namespace std;

class Film 
{
public:
	char* titel;
	char* type;

	void SetInfo (char* _titel, char* _type)
	{
		titel = _titel;
		type = _type; 
	}
};

int main ()
{
	Film myFilm[10];

	myFilm[0].SetInfo("Donald Duck", "DvD");
	myFilm[1].SetInfo("Fast and the furious", "DvD");
	myFilm[2].SetInfo("Tropa De Elite", "BlueRay");
	myFilm[3].SetInfo("Bourne identity", "BlueRay");
	myFilm[4].SetInfo("Clueless", "DvD");
	myFilm[5].SetInfo("Klara", "BlueRay");


	cin.get();
	return 0;
}
Last edited on
You need a member search function with a simple for loop.

A few comments:
1) Lines 8-9, Declaring your member variables as char * is not safe. It works because you're passing const strings to SetInfo. If you were to call SetInfo with a variable set from user input or read from a file inside a loop, you would end up with all your films pointing to the same string.

2) Line 11, variable names starting with _ are reserved for the compiler.

3) You should use std::string instean of char *, but you may not have studied std::string yet.

4) Likewise, you should consider std::vector to hold your films. A vector is flexible in the number of entries you can have and will also keep track of how many films you have.

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
#include <string> 
#include <vector>
#include <iostream>
using namespace std;

class Film 
{
public:
    string titel;
    string type;

    //  Changed SetInfo to become a two argument constructor 
    Film (string _titel, string _type)
    {   titel = _titel;
        type = _type; 
    }

    //  This function is static since it doesn't apply to a specific instance
    static int FindByTitle (const vector<Film> & films, const string title) 
    {   for (int i=0; i<films.size(); i++)
            if (films[i].titel == title)
                return i;
        return -1;  // not found
    }
}
 
int main ()
{   vector<Film>    films;
    string          title;
    int             indx;

    films.push_back (Film("Donald Duck", "DvD"));
    //  ... etc
    cout  << "Enter title to search for: ";
    getline (cin, title);
    indx = Film::FindByTitle (films, title);
    if (indx == -1)
        cout << "Title not found" << endl;
    else
        cout << "Title is film # " <<indx << endl;
    return 0;
}

I love this!

First didn't understand why you added push_back, but after researching and reading about that function and what it does to vectors.

This seems to be a much better ! I thank you for that :P

And answer to your question, no we haven't gotten too much into vectors but we have some.. I thought char* vector[]; <-- created a vector :/ strangely that's how i got it from reading what I did in the book
Hum, even though that solution is great, i understand about 20% of the changes you made. My c++ knowledge is very basic and I haven't gotten so far to input vectors the way you did.

Is there any way to change this script and make it work with small changes so that my search function also can search for chars? (can't change too much because it requires that I actually understand the code itself)

As of now I can only get this version to work with int :(

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
#include <iostream>
using namespace std;

class Person 
{
public:
	char* name;
	int personnummer;


	void SetInfo (char* _name, int _personnummer)
	{
		name = _name;
		personnummer = _personnummer; 
	}

};

int LinearSearch (Person* personArray, int key)
{
	for (int i = 0; i < 2; i++) 
	{

		if (personArray[i].personnummer == key)
			return i;
	}
	return -1; 
}


int main ()
{

	Person myList[2];
	myList[0].SetInfo("Leo", 1);
	myList[1].SetInfo("George", 2);


	int index = LinearSearch (myList, 920411);


	if(index == -1) 
		cout<<"Not found";
	else
		cout<< "Persons name is " << myList[index].name<<
				"  with the index " << index;

	cin.get();
	return 0;
}

Last edited on
1) At line 13, you're copying the pointer. Not the name.

2) At line 21, you're searching 10 entries, but your array only has 2 entries allocated. You're going to be searching garbage, if your program doesn't crash from searching unallocated memory.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Topic archived. No new replies allowed.