Need a little help

Status
Not open for further replies.

pushkar

Journeyman
I am creating a little movie rental program for a project. I am in Class 12.

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();
}
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.
 

QwertyManiac

Commander in Chief
1. Your writing objects method is troublesome. Instead, define movie::read() and movie::write() methods to write to and read from a file in a normal manner than converting and pushing it in like that.

2. Yes, using a char * fname() { return some_char_array; } is correct for returning character arrays.

Suggestions:
1. When you're using C++, why not use the string class to store strings? Makes life hell easier, cause you're mixing both C and C++ libraries and making horrible code right now.

2. If you're planning to make this into a big console-oriented application, why not use a real database itself? Using files is slow for maintaining (over the time) large catalogs.

3. If its gonna be only console-oriented, and with no console graphics involved, use standard C++ code so that it runs on many platforms.
 
OP
pushkar

pushkar

Journeyman
Blame CBSE, not me. I am writing code in a manner which I have been taught. I don't have external source of learning. I didn't understand what you mean by converting and pushing. and what are standard c++ commands so that it runs on many platforms. Do you mean I should not use gets, etc.
 

QwertyManiac

Commander in Chief
I'm neither blaming you, nor CBSE here. You asked for some suggestions, and I gave mine [IMO]. :)

I meant this statement specifically:
PHP:
fadd.write((char*)&m1,sizeof(m1));
Don't do it, even though it looks like a very easy method of writing objects to a file directly, it can always go wrong.

Instead, write a movie::write() method that writes all the values in a line normally, like:

PHP:
fadd << id << " " << title << " " << year << " " << etc << endl; // To write one line
Which you can then read by a movie::readall() method like:

PHP:
while (fall) {
    fall >> id >> title >> year >> etc; // To read one line.
    // Conditions, output, etc.
}
Strings in C++ are very easy to use via the string class. Here's a tiny demo:
PHP:
#include <iostream>

using namespace std;

class Foo {
    string a;
    public:
    void seta (string t) { a = t; } // Sets a
    string geta () { return a; } // Returns a
    void adda (string t) { a += t; } // Appends a
};

int main () {
    Foo t;
    string foo("Hello"), bar(" World");
    t.seta(foo);
    cout << t.geta() << endl;
    t.adda(bar);
    cout << t.geta() << endl;
    return 0;
    // Output:
    // Hello
    // Hello World
}
P.s. You're on the internet, there's no limitation on learning here. You can very well ignore the standards part of my suggestion, or use sites like cplusplus.com and others to know more about it and its standard libraries. And, gets() is standard but its dangerous to use (some bounding flaws exist), instead you could perhaps use cin.getline() or fgets() methods to read a full line, with a bound.

P.p.s. My examples are just that, examples. You can of course write and read in any order or condition you like (even with different delimiters), but make sure its not the way you're doing it right now.
 
Last edited:
OP
pushkar

pushkar

Journeyman
I'm neither blaming you, nor CBSE here. You asked for some suggestions, and I gave mine [IMO]. :)
Oh, I meant that in a light vein.

Don't do it, even though it looks like a very easy method of writing objects to a file directly, it can always go wrong.

Instead, write a movie::write() method that writes all the values in a line normally, like:

PHP:
fadd << id << " " << title << " " << year << " " << etc << endl; // To write one line
Which you can then read by a movie::readall() method like:

PHP:
while (fall) {
    fall >> id >> title >> year >> etc; // To read one line.
    // Conditions, output, etc.
}
OK, I will follow this suggestion of yours. I think the redundant last record is because of this mistake.

]P.s. You're on the internet, there's no limitation on learning here. You can very well ignore the standards part of my suggestion, or use sites like cplusplus.com and others to know more about it and its standard libraries. And, gets() is standard but its dangerous to use (some bounding flaws exist), instead you could perhaps use cin.getline() or fgets() methods to read a full line, with a bound.

P.p.s. My examples are just that, examples. You can of course write and read in any order or condition you like (even with different delimiters), but make sure its not the way you're doing it right now.
I will convert gets to cin.getline. Of course, I want to learn more about C++ programming by the right standards. But as I already told you I am a class 12th student, it is difficult to take out time to learn different things out of course. But its not that I cannot learn more. I will definitely have a look at cplusplus.com.

Thank you for your suggestions. I will get back after modifying the code in 1 or 2 days.
 
Status
Not open for further replies.
Top Bottom