pushkar
Journeyman
I am creating a little movie rental program for a project. I am in Class 12.
The code is below:
Now, the problem I am facing is that when I select the option to see all the records, the last record is shown two times. I don't why this is happening. Is this a case of wrong use of eof(), etc.
Also please tell me how to read a character array using a function from a class, if the string is a private member. I have used the char * idtitle() function in the class. Please tell me if it is correct.
If you want you can compile the above code and see yourself. currently, the options 1, 4 and 5 are working. I haven't written the code for other options.
Also, any suggestions of features are welcome. I want my program to have some great features.
The code is below:
Code:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
class movie
{
int no;
char title[100];
int year;
float rating;
int rented;
public:
movie();
void add();
int idno();
char * idtitle();
void rent();
int rentstatus();
void unrent();
void show();
};
movie::movie()
{
no=0;
strcpy(title,"Untitled");
year=0;
rating=0;
rented=0;
}
void movie::add()
{
cout<<"\n\n--ADDING A MOVIE--";
cout<<"\nTitle: ";
gets(title);
cout<<"\nTitle Number: ";
cin>>no;
cout<<"\nYear: ";
cin>>year;
do
{
cout<<"\nIMDB Rating (out of 10): ";
cin>>rating;
if(rating>10)
cout<<"\n\tRating must be less than 10\n\tEnter again\n";
}while(rating>10); //so that the rating is below 10
}
int movie::idno()
{
return no;
}
char * movie::idtitle()
{
return title;
}
void movie::rent()
{
rented=1;
cout<<"\nMovie "<<no<<" titled "<<title<<" has been rented";
}
int movie::rentstatus()
{
return rented;
}
void movie::unrent()
{
if(rented==0)
{
cout<<"\nError: The movie "<<no<<" titled "<<title;
cout<<" cannot be returned as it is not rented";
}
else
{
rented=1;
cout<<"The movie "<<no<<" titled "<<title;
cout<<" has been returned";
}
}
void movie::show()
{
cout<<"\n\n--MOVIE DETAILS--";
cout<<"\nTitle: "<<title;
cout<<"\nTitle Number: "<<no;
cout<<"\nYear: "<<year;
cout<<"\nIMDB Rating (out of 10): "<<rating;
}
void main()
{
clrscr();
ofstream fadd;
cout<<"\t\tWELCOME TO TWILIGHT VIDEO STORE";
int choice=0;
do
{
clrscr();
cout<<"\n\nWhat do you want to do?";
cout<<"\n1. Add a movie ";
cout<<"\n2. Rent a movie ";
cout<<"\n3. See the status of a movie ";
cout<<"\n4. Fresh start (create fresh records)";
cout<<"\n5. See all movies";
cout<<"\n6. See rented movies";
cout<<"\n7. See movies in stock (not rented)";
cout<<"\n8. Exit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1: movie m1;
m1.add();
fadd.open("MOVIE.DOC",ios::app|ios::binary);
fadd.write((char*)&m1,sizeof(m1));
cout<<"\nThe movie titled "<<m1.idtitle()<<" has been added to the database";
getch();
fadd.close();
break;
case 4: remove("MOVIE.DOC");
ofstream fcreate("MOVIE.DOC");
fcreate.close();
break;
case 5: movie m5;
ifstream fall; //to see all movies
fall.open("MOVIE.DOC",ios::binary);
while(!fall.eof())
{
fall.read((char*)&m5,sizeof(m5));
m5.show();
cout<<"\nPress Enter for more (B to break) ";
char ch5;
ch5=getch();
if(ch5=='b'||ch5=='B')
break;
}
fall.close();
break;
}
}while(choice!=8);
getch();
}
Also please tell me how to read a character array using a function from a class, if the string is a private member. I have used the char * idtitle() function in the class. Please tell me if it is correct.
If you want you can compile the above code and see yourself. currently, the options 1, 4 and 5 are working. I haven't written the code for other options.
Also, any suggestions of features are welcome. I want my program to have some great features.