Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 09-12-2008, 03:45 PM   #1 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default Need a little help


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.
pushkar is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 09-12-2008, 04:56 PM   #2 (permalink)
Alpha Geek
 
paroh's Avatar
 
Join Date: Jul 2008
Posts: 781
Default Re: Need a little help

Check PM
paroh is offline  
Old 10-12-2008, 06:24 PM   #3 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default Re: Need a little help

Anyone? I will soon post the updated code which I wrote yesterday.
pushkar is offline  
Old 10-12-2008, 09:11 PM   #4 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Need a little help

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.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 11-12-2008, 12:13 PM   #5 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default Re: Need a little help

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.
pushkar is offline  
Old 11-12-2008, 12:43 PM   #6 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Need a little help

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

I meant this statement specifically:
PHP Code:
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 Code:
fadd << id << " " << title << " " << year << " " << etc << endl// To write one line 
Which you can then read by a movie::readall() method like:

PHP Code:
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 Code:
#include <iostream>

using namespace std;

class 
Foo {
    
string a;
    public:
    
void seta (string t) { t; } // Sets a
    
string geta () { return a; } // Returns a
    
void adda (string t) { += 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.
__________________
Harsh J
www.harshj.com

Last edited by QwertyManiac; 11-12-2008 at 01:05 PM.
QwertyManiac is offline  
Old 11-12-2008, 01:51 PM   #7 (permalink)
Broken In
 
pushkar's Avatar
 
Join Date: May 2006
Posts: 196
Default Re: Need a little help

Quote:
Originally Posted by QwertyManiac View Post
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.

Quote:
Originally Posted by QwertyManiac View Post
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 Code:
fadd << id << " " << title << " " << year << " " << etc << endl// To write one line 
Which you can then read by a movie::readall() method like:

PHP Code:
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.

Quote:
Originally Posted by QwertyManiac View Post
]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.
pushkar is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:02 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2