#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>

class Movie{
    public:
        char name[40];
        char dir[40];
        char writer[40];
        char release[6];
        char genre[40];
        char cast[80];
        char budget[10];
        char grossing[10];
        char country[30];
        char lang[30];
        char rating[5];
};

int compare(char *, char *);
void input(char *);
void list(char *);
void output(Movie );
void searchByGenre(char *, char *);
void searchByName(char *, char *);
void viewMovie(char *, int );
void editMovie(char *);
void deleteMovie(char *);
Movie getMovie();

void input(char filename[]) {                            //   What does this take as input?
        fstream fmovie;                                    //  What does this line do?
    Movie movie = getMovie();                            //  This creates an object?? or.....
    fmovie.open(filename,ios::out|ios::app);
    fmovie.write((char *)&movie,sizeof(movie));
    fmovie.close();
    cout << endl << endl << "Movie has been saved successfully. Press any key to go back to the Main Menu";
    getchar();

Line 35 is a function which does not return a value. However it takes a one dimensional character array as its argument.

Line 36 Creates a file handler called "fmovie". It is of type fstream which means it can perform both read and write functions.

Line 37 creates an object of type Movie and it is assigned the value which is returned from the function getmovie().

Comments
thanks,can u tell me why is an object assigned with a value,whats the advantage using object to be assigned?i havent assigned object before thats why?

thanks,can u tell me why is an object assigned with a value,whats the advantage using object to be assigned?i havent assigned object before thats why?

Looking at this code I have questions of my own. I see that on line 33. The declaration of Movie getMovie() does not have have an implementation nor does it exist within the class Movie. So if I assume that what you intend to do here is get the name of the movie then you could have just used a string variable. To use the object Movie does not make sense to me. As is this code may not even compile

this is the full code......i am just trying to learn how this works....so i can make other programs like this

#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>

class Movie{
    public:
        char name[40];
        char dir[40];
        char writer[40];
        char release[6];
        char genre[40];
        char cast[80];
        char budget[10];
        char grossing[10];
        char country[30];
        char lang[30];
        char rating[5];
};

int compare(char *, char *);
void input(char *);
void list(char *);
void output(Movie );
void searchByGenre(char *, char *);
void searchByName(char *, char *);
void viewMovie(char *, int );
void editMovie(char *);
void deleteMovie(char *);
Movie getMovie();

void input(char filename[]) {
    fstream fmovie;
    Movie movie = getMovie();
    fmovie.open(filename,ios::out|ios::app);
    fmovie.write((char *)&movie,sizeof(movie));
    fmovie.close();
    cout << endl << endl << "Movie has been saved successfully. Press any key to go back to the Main Menu";
    getchar();
}

Movie getMovie() {
    Movie movie;
    clrscr();
    cout << endl << "Add Movie Console" << endl;
    cout << endl << "Movie Name  : ";
    gets(movie.name);
    cout << endl << "Director  : ";
    gets(movie.dir);
    cout << endl << "Writer  : ";
    gets(movie.writer);
    cout << endl << "Release Date  : ";
    gets(movie.release);
    cout << endl << "Genre  : ";
    gets(movie.genre);
    cout << endl << "Cast  : ";
    gets(movie.cast);
    cout << endl << "Budget  : ";
    gets(movie.budget);
    cout << endl << "Grossing  : ";
    gets(movie.grossing);
    cout << endl << "Country  : ";
    gets(movie.country);
    cout << endl << "Language  : ";
    gets(movie.lang);
    cout << endl << "Rating  : ";
    gets(movie.rating);
    return movie;
}

void list(char filename[]) {
    Movie movie;
    fstream fmovie;
    fmovie.open(filename,ios::in);
    int count = 0;
    cout << setiosflags(ios::left);
    cout << setw(25) << "NAME";
    cout << setw(5) << "REL";
    cout << setw(45) << "CAST";
    cout << setw(3) << "*";
    while (fmovie.read((char *)&movie,sizeof(movie)))
    {
        if (count > 0 && count % 15 == 0) {
            cout << "Press any key to continue";
            getchar();
        }
        //cout << endl << movie.name << " - " << movie.release << " - " << movie.cast << " - " << movie.rating;
        cout << endl;
        cout << setw(25) << movie.name;
        cout << setw(5) << movie.release;
        cout << setw(45) << movie.cast;
        cout << setw(3) << movie.rating;
        count ++;
    };
    fmovie.close();
    cout << endl << endl << endl << endl << "Press any key to go back to the Main Menu";
    getchar();
}

void output(Movie movie) {
    cout << endl << "Movie Name  : ";
    puts(movie.name);
    cout << endl << "Director  : ";
    puts(movie.dir);
    cout << endl << "Writer  : ";
    puts(movie.writer);
    cout << endl << "Release Date  : ";
    puts(movie.release);
    cout << endl << "Genre  : ";
    puts(movie.genre);
    cout << endl << "Cast  : ";
    puts(movie.cast);
    cout << endl << "Budget  : ";
    puts(movie.budget);
    cout << endl << "Grossing  : ";
    puts(movie.grossing);
    cout << endl << "Country  : ";
    puts(movie.country);
    cout << endl << "Country  : ";
    puts(movie.lang);
    cout << endl << "Rating  : ";
    puts(movie.rating);
}

void editMovie(char filename[]) {
    Movie movie;
    fstream fmovie;
    fmovie.open(filename,ios::in);
    int count = 0, selectedIndex;
    cout << setiosflags(ios::left);
    cout << setw(3) << "ID";
    cout << setw(20) << "NAME";
    cout << setw(5) << "REL";
    cout << setw(45) << "CAST";
    cout << setw(3) << "*";
    while (fmovie.read((char *)&movie,sizeof(movie)))
    {
        count ++;
        if (count > 0 && count % 15 == 0) {
            cout << "Press any key to continue";
            getchar();
        }
        cout << endl;
        cout << setw(3) << count;
        cout << setw(20) << movie.name;
        cout << setw(5) << movie.release;
        cout << setw(45) << movie.cast;
        cout << setw(3) << movie.rating;

    };
    fmovie.close();
    cout << endl << "Enter Movie Index to edit Movie details or 0 to go to Menu : ";
    cin >> selectedIndex;
    if (selectedIndex != 0) {
        clrscr();
        cout << endl << "Edit Console | Movie Id  :  " << selectedIndex;
        fstream temp;
        fmovie.open(filename,ios::in);
        temp.open("temp",ios::out|ios::app);
        int count = 0;
        while (fmovie.read((char *)&movie,sizeof(movie)))
        {
            count ++;
            if (count == selectedIndex) {
                Movie tempMovie = getMovie();
                temp.write((char *)&tempMovie,sizeof(tempMovie));
            }
            else {
                temp.write((char *)&movie,sizeof(movie));
            }
        };
        fmovie.close();
        temp.close();
        remove(filename);
        rename("temp", filename);
        cout << endl << endl << endl << "Movie Edited Successfully. Press any key to go to the Main Menu";
        getchar();
    }
}

void deleteMovie(char filename[]) {
    Movie movie;
    fstream fmovie;
    fmovie.open(filename,ios::in);
    int count = 0, selectedIndex;
    cout << setiosflags(ios::left);
    cout << setw(3) << "ID";
    cout << setw(20) << "NAME";
    cout << setw(5) << "REL";
    cout << setw(45) << "CAST";
    cout << setw(3) << "*";
    while (fmovie.read((char *)&movie,sizeof(movie)))
    {
        count ++;
        if (count > 0 && count % 15 == 0) {
            cout << "Press any key to continue";
            getchar();
        }
        cout << endl;
        cout << setw(3) << count;
        cout << setw(20) << movie.name;
        cout << setw(5) << movie.release;
        cout << setw(45) << movie.cast;
        cout << setw(3) << movie.rating;

    };
    fmovie.close();
    cout << endl << "Enter Movie Index to delete Movie from Library or 0 to go to Menu : ";
    cin >> selectedIndex;
    if (selectedIndex != 0) {
        clrscr();
        cout << endl << "Edit Console | Movie Id  :  " << selectedIndex;
        fstream temp;
        fmovie.open(filename,ios::in);
        temp.open("temp",ios::out|ios::app);
        int count = 0;
        while (fmovie.read((char *)&movie,sizeof(movie)))
        {
            count ++;
            if (count != selectedIndex) {
                temp.write((char *)&movie,sizeof(movie));
            }
        };
        fmovie.close();
        temp.close();
        remove(filename);
        rename("temp", filename);
        cout << endl << endl << endl << "Movie deleted Successfully. Press any key to go to the Main Menu";
        getchar();
    }
}

void searchByGenre(char filename[], char sgenre[]) {
    Movie movie;
    fstream fmovie;
    int count = 0, globalCount = 0, selectedIndex;
    int globalCountStore[100];
    fmovie.open(filename,ios::in);
    cout << "Search Results - By Genre" << endl << endl;
    cout << setiosflags(ios::left);
    cout << setw(3) << "";
    cout << setw(20) << "NAME";
    cout << setw(5) << "REL";
    cout << setw(30) << "GENRE";
    cout << setw(3) << "*";
    while (fmovie.read((char *)&movie,sizeof(movie)))
    {
        globalCount ++;
        if (compare(movie.genre, sgenre) == 1) {
            globalCountStore[count] = globalCount;
            count ++;
            cout << endl;
            cout << setw(3) << count;
            cout << setw(20) << movie.name;
            cout << setw(5) << movie.release;
            cout << setw(30) << movie.genre;
            cout << setw(3) << movie.rating;
        }
    };
    fmovie.close();
    cout << endl << endl << endl << "Total Number of Matches found  : " << count;
    cout << endl << "Enter Movie Index to view Movie details or 0 to go to Menu : ";
    cin >> selectedIndex;
    if (selectedIndex != 0) {
        viewMovie(filename, globalCountStore[selectedIndex-1]);
    }
}

void searchByName(char filename[], char sname[]) {
    Movie movie;
    fstream fmovie;
    int count = 0, globalCount = 0, selectedIndex;
    int globalCountStore[100];
    fmovie.open(filename,ios::in);
    cout << "Search Results - By Name" << endl << endl;
    cout << setw(3) << "";
    cout << setw(20) << "NAME";
    cout << setw(5) << "REL";
    cout << setw(30) << "GENRE";
    cout << setw(3) << "*";
    while (fmovie.read((char *)&movie,sizeof(movie)))
    {
        globalCount ++;
        if (compare(movie.name, sname) == 1) {
            globalCountStore[count] = globalCount;
            count ++;
            cout << endl;
            cout << setw(3) << count;
            cout << setw(20) << movie.name;
            cout << setw(5) << movie.release;
            cout << setw(30) << movie.genre;
            cout << setw(3) << movie.rating;
        }
    };
    fmovie.close();
    cout << endl << endl << endl << "Total Number of Matches found  : " << count;
    cout << endl << "Enter Movie Index to view Movie details or 0 to go to Menu : ";
    cin >> selectedIndex;
    if (selectedIndex != 0) {
        viewMovie(filename, globalCountStore[selectedIndex-1]);
    }
}

void viewMovie(char filename[], int selectedIndex) {
    Movie movie;
    fstream fmovie;
    int globalCount = 0;
    fmovie.open(filename,ios::in);
    cout << "Movie Details" << endl << endl;
    while (fmovie.read((char *)&movie,sizeof(movie)))
    {
        globalCount ++;
        if (globalCount == selectedIndex) {
            output(movie);
            break;
        }
    };
    fmovie.close();
    cout << endl << "Press any key to go back to the Main Menu";
    getchar();
}

int compare(char str2[], char str1[]) {
    for (int i=0;i<strlen(str2)-strlen(str1)+1;i++) {
        int flag = 1;
        int temp_c = i;
        for (int j = 0;j < strlen(str1);j ++) {
            if (str1[j] != str2[temp_c]) {
               flag = 0;
            }
            temp_c ++;
        }
        if (flag == 1) {
            return 1;
        }
    }
    return 0;
}

void menu (int clearScrFlag) {
    if (clearScrFlag == 1) {
        clrscr();
    }
    cout << "Welcome to Movie Library. Select an option: ";
    cout << endl << endl 
         << endl << "(1) Enter a new Movie in the Movie DATABASE"
         << endl << "(2) Search Movie By Genre"
         << endl << "(3) Search Movie Name by Name"
         << endl << "(4) List All Movies"
         << endl << "(5) Edit Movie"
         << endl << "(6) Delete Movie"
         << endl << "(7) Exit"
         << endl << endl << "Enter Choice :   ";
}

void main() {
    int loop = 0, ch;
    char filename[] = "mov";
    while (loop == 0){
        menu(1);
        cin >> ch;              
        switch(ch) {
            case 1: /* WRITE TO THE DATABASE*/
                input(filename);
            break;
            case 2: /* SEARCH DATABASE BASED ON GENRE*/
                clrscr();
                char sgenre[60];
                cout << "Enter Genre to be Searched  : ";
                gets(sgenre);
                searchByGenre(filename, sgenre);                
                break;
            case 3: /* SEARCH DATABASE BASED ON MOVIE NAME*/
                clrscr();
                char sname[60];
                cout << "Enter Name of the Movie to be Searched  : ";
                gets(sname);
                searchByName(filename, sname);
                break;              
            case 4: /* LIST THE CONTENTS IN THE DATABASE */
                clrscr();
                list(filename);
                break;
            case 5: /* EDIT MOVIE */
                clrscr();
                editMovie(filename);
                break;
            case 6: /* DELETE MOVIE */
                clrscr();
                deleteMovie(filename);
                break;
            case 7: /* EXIT THE APPLICATION */
                cout << endl << "Exit Successfully . . .";
                loop = 1;
                exit(1);
            default: /* WRONG OPTION */                 
                menu(0);
                cout << ch << endl << "Note : You have selected a wrong option. Try Again. " << endl << "Enter Choice :   ";
                break;
        }
        cin.clear(); cin.ignore(); ch = 0; 
    }
}

Line 36 Movie movie = getMovie(); calls the function getMovie() on line 46. Inside this function the member variables are being updated. At the end of the function movie is being returned back to line 36 and saved in the variable created there i.e Movie movie. An object is used here since the programmer wishes to access the member variables of the class Movie. If you used a regular variable it would not work since you are returning an object. If he didn't instantiate an object of type Movie then he would not be able to access its variables. As you can see the variables of class Movie are being accessed in the following format: movie.name. The dot operator allows you to access the variables of class Movie. Notice that the variables are also listed in the public section of the class which makes access via the dot operator possible. If these variables were listed as private, then you would have to create member functions to access them.

This article has been dead for over six months. Start a new discussion instead.