Hello Friends ! I am working on a project of CD CAFE. In which I have arranged movies like this :
Movie Name::::Genre::::Hero of Movie::::Heroine of Movie

and I have entered data in this. Now I want to add search option in my program that I can search by Movie name or genre or hero or heroine it searches all records from texgt file and give me all matches searched results.
Can Anyone help me ??

I would suggest that when the program starts you read in all of the data in the file and load it into an collection of "Movie Records". Then you can search that collection for the records you want.

You may say more details are not

Member Avatar for iamthwee

Create a class

class Movie

public name
public genre

Then you search by those class attributes, or you could create an associative array same thing.

Google classes or assoc arrays in c++.

You may like to file your data using commas to delimit the data record fields.

That way you can easily handle missing fields:

MovieName1,Genre1,HeroOfMovie1,HeroineOfMovie1
MovieName2,Genre2,,HeroineOfMovie2
MovieName3,Genre3,HeroOfMovie3,

And display as:

MovieName1::::Genre1::::HeroOfMovie1::::HeroineOfMovie1
MovieName2::::Genre2::::NA_HERO::::HeroineOfMovie2
MovieName3::::Genre3::::HeroOfMovie3::::NA_HEROINE

This example of sorting data records and finding data records using the C++ library functions may help you to get started.

This example uses an array of Student records. It demo's the idea of sorting on various fields and finding records that have particular field values.

// sortStudentsById_with_find2.cpp //

#include <iostream>
#include <iomanip>
#include <string>
#include <cctype> // re. tolower ...

#include <algorithm> // re array sort, find, etc...
#include <vector>

using namespace std;

const int SIZE_STUDENT_ARY = 5;

class Student
{
public:
    // ctors...
    Student() : age(0), grade(0), id(0) {}
    Student( int age, int grade, int id, const string& name )
    : age(age), grade(grade), id(id), name(name) {}

    void setAge( int age ) { this->age = age; }
    void setGrade( int grade ) { this->grade = grade; }
    void setId( int id ) { this->id = id ; }
    void setName( const string& name ) { this->name = name ; }

    int getAge() const  { return age; }
    int getGrade() const { return grade; }
    int getId() const { return id; }
    string getName() const { return name; }

    void print( ostream& os ) const
    {
        os << left << setw(14) << name << ' '
           << setw(7) << id << ' '
           << setw(7) << grade << ' '
           << age << right;
    }
private:
    int age;
    int grade;
    int id;
    string name;

    // to permit sorting by id's ... //
    friend bool cmpById( const Student& a, const Student& b );

    // to permit finding by id and thus if id was used already ... //
    friend bool operator == ( const Student& a, const Student& b );

    // to permit sorting and binary_search by name ... //
    friend bool cmpByName( const Student& a, const Student& b );
} ;


// def'n of friend functions ...
bool cmpById( const Student& a, const Student& b )
{
    return a.id < b.id;
}
bool operator == ( const Student& a, const Student& b )
{
    return a.id == b.id;
}
bool cmpByName( const Student& a, const Student& b )
{
    return a.name < b.name;
}

// def'n of overloaded << for Student objects ...
ostream& operator << ( ostream& os, const Student& s )
{
    s.print( os );
    return os;
}


// some utilities used here ..

void printHeader()
{
    cout << left << setw(14) << "NAME" << ' '
           << setw(7) << "ID" << ' '
           << setw(7) << "GRADE" << ' '
           << "AGE" << right << endl;
}


string toCapsOnAllFirstLetters( const string& strIn )
{
    string str = strIn;
    int prev_was_space = 1;
    int len = str.size();
    for( int i = 0; i < len; ++ i )
    {
        if( prev_was_space )
            str[i] = std::toupper( str[i] );
        prev_was_space = isspace( str[i] );
    }
    return str;
}
string takeInString( const string& msg = "" )
{
    cout << msg << flush;
    string val;
    getline( cin, val );
    return val;
}
char takeInChr( const std::string& msg = "" )
{
    string reply = takeInString( msg );
    if( reply.size() )
        return reply[0];
    // else ...
    return 0;
}
bool more()
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' )
        return false;
    // else ...
    return true;
}
int takeInValidInt( const char* msg )
{
    int val;
    while( true )
    {
        cout << msg << flush;
        if( cin >> val && cin.get() == '\n' )
            break;
        else
        {
            cout << "Invalid input ... numbers only!\n";
            cin.clear(); // clear error flags
            cin.sync(); // 'flush' cin stream ...
        }
    }
    return val;
}



int main()
{
    Student students[SIZE_STUDENT_ARY];

    // Ok ... take in an array of Student records ...

    int size = 0;
    do
    {
        students[size].setName(
           toCapsOnAllFirstLetters( takeInString("Enter Name  : " )));
        students[size].setAge( takeInValidInt(   "Enter Age   : " ));
        students[size].setGrade( takeInValidInt( "Enter Grade : " ));
        students[size].setId( takeInValidInt(    "Enter Id    : " ));

        if( find( students, students+size, students[size] )
            != students+size ) // then found
        {
            cout << "Sorry ... that ID is taken ... \n";
            continue;
        }

        printHeader();
        cout << students[size];
        if( tolower(takeInChr( "  ...  Ok (y/n) ? " )) == 'y' )
            ++size;
        else
            cout << "Ok ... that student was NOT entered ...\n";

        if( size == SIZE_STUDENT_ARY )
        {
            cout << "You have reached "
                 << SIZE_STUDENT_ARY
                 << ", the max space here!\n";
             break;
        }
    }
    while( more() );



    // Ok ... output results ...

    cout<< "\nStudents in entered order ...  \n";
    printHeader();
    for( int x = 0; x < size; ++x )
    {
        students[x].print( cout );
        cout << endl;
    }

    cout<< "Students ordered by ID ...  \n";
    sort( students, students+size, cmpById );

    printHeader();
    for( int x = 0; x < size; ++x )
    {
        cout << students[x] << endl; // using overloaded <<
    }

    cout<< "Students ordered by name ...  \n";
    stable_sort( students, students+size, cmpByName );
    printHeader();
    for( int x = 0; x < size; ++x )
    {
        cout << students[x] << endl; // using overloaded <<
    }


    cout << "\nLoop to test binary_search by name ...\n";
    do
    {
        Student tmp;
        tmp.setName( toCapsOnAllFirstLetters(takeInString(
                     "Enter name to find: " )) );
        if( binary_search( students, students+size, tmp, cmpByName ) )
        {
            cout << "'"<< tmp.getName() << "' was found ...\n";
        }
        else cout << "'"<< tmp.getName()<< "' was NOT found ...\n";
    }
    while( more() );


    cout << "\nloop to test find by name using 'lower bound' ...\n";
    do
    {
        Student tmp;
        tmp.setName( toCapsOnAllFirstLetters(takeInString(
                     "Enter name to find: " )) );

        Student* low = lower_bound( students, students+size, tmp,
                                    cmpByName );
        bool found = false;
        while( low->getName() == tmp.getName() )
        {
            cout << "'"<< tmp.getName() << "' was found: "
                 << *low << endl;
            found = true;
            low = lower_bound( low+1, students+size, tmp,
                                    cmpByName );
        }
        if( !found )
            cout << "'"<< tmp.getName()<< "' was NOT found ...\n";
    }
    while( more() );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.