Current location: Hot Scripts Forums » Programming Languages » C/C++ » Short Command Line Based Item Order Program


Short Command Line Based Item Order Program

Reply
  #1 (permalink)  
Old 10-07-03, 09:46 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Short Command Line Based Item Order Program

Hi all,

I saw this forum was empty, so here is a short Command Line Based Item Order Program I wrote, which can be modified to use with any item. You can download and use it for free if you like. I also included a compiled version of it for Windows. Enjoy!!

Code:
#include <iostream.h>
#include <fstream.h>

// ----------------------- Class Item --------------------------------------
class Item
{
private:
	int itemno, quantity;
	float unitPrice;
public:
	virtual void getData();
    virtual void showData();
	long getId(){return itemno;}
	virtual char getType()=0;
};
void Item::getData(){cout<<"\nEnter Item Number: "; cin>>itemno;
						cout<<"\n        Quantity: "; cin>>quantity;
						cout<<"\n      Unit Price: "; cin>>unitPrice;
}

void Item::showData(){cout<<endl<<itemno<<"  "<<quantity<<"  "<<unitPrice;}

// ------------------------  Class Book -------------------------------------
class Book: public Item                        
{
private:
	char author[30];
	char subject[30];
	int pages;
public:
	void getData(){ cout<<"\nItem Information: ";Item::getData();
	              cout<<"\n  Enter Author:"; cin.ignore(); cin.get(author, 30);
	cout<<"\n     Enter Subject:"; cin.ignore(); cin.get(subject, 30);
	cout<<"\nPages:"; cin>>pages;
	}
	void showData(){ cout<<"\nItem Information: "; Item::showData();
		cout<<endl<<author<<"  "<<subject<<"  "<<pages;}
	char getType(){return 'b';}
};

// ------------------------ Class CD ----------------------------------------
class CD: public Item
{
private:
	char title[30];
	char publisher[30];
	int size;
public:
	void getData(){cout<<"\nItem information: "; Item::getData();
					cout<<"\n       CD Title: "; cin.ignore(); cin.get(title, 30);
					cout<<"\n   CD Publisher: "; cin.ignore(); cin.get(publisher, 30);
					cout<<"\n        CD Size: "; cin>>size;}
	void showData(){cout<<"\nItem information: "; Item::showData();
					cout<<endl<<title<<"  "<<publisher<<"   "<<size;}
	char getType(){return 'c';}
};

// ------------------------- Class VTape -----------------------------------

class VTape: public Item
{
private:
	char subject[30];
	int minutes;
	int version;
public:
	void getData(){cout<<"\nItem information: "; Item::getData();
					cout<<"\n        Subject: "; cin.ignore(); cin.get(subject, 30);
					cout<<"\n        Minutes: "; cin>>minutes;
					cout<<"\n        Version: "; cin>>version;}
	void showData(){cout<<"\nItem information: "; Item::showData();
					cout<<endl<<subject<<"  "<<minutes<<"   "<<version;}
	char getType(){return 't';}
};

// -------------------------- Add Object ----------------------------------

void addObject(Item *p, fstream& f, char *str, char tag, int size)
{
	f.open(str, ios::app|ios::binary);
	f.write(&tag, 1);
	f.write((char *)p, size);
	f.close();
}

// ----------------------- Display Object ---------------------------------

void display(fstream& f, char *fname)
{
	Item *p;
	int count=0, size;
	char tag;
	f.open(fname, ios::in|ios::binary);
	f.read(&tag, 1);
	while(!f.eof())
	{
		switch(tag)
		{
		case 'b':p=new Book;
			     size=sizeof(Book);
				 break;
		case 'c':p=new CD;
			     size=sizeof(CD);
				 break;
		case 't':p=new VTape;
			     size=sizeof(VTape);
				 break;
		}
		f.read((char*)p, size);
		p->showData();
		delete p;
		count++;
		f.read(&tag,1);
	}
	f.close();
	cout<<endl<<count<<" objects were listed.";
}

// ----------------------- Display Certain Object ------------------

void display(fstream& f, char fname[], char& tag)
{
	Item *p;
	int count=0, size;
	char x;
	f.open(fname, ios::in|ios::binary);
	f.read(&x, 1);
	while(!f.eof())
	{
		switch(x)
		{
		case 'b':p=new Book;
			     size=sizeof(Book);
				 break;
		case 'c':p=new CD;
			     size=sizeof(CD);
				 break;
		case 't':p=new VTape;
			     size=sizeof(VTape);
				 break;
		}
		if(x==tag)
		{
			f.read((char *)p, size);
			p->showData();
			delete p;
			count++;
		}
		else
			f.seekg(size, ios::cur);
		f.read(&x, 1);
	}
	f.close();
	cout<<endl<<count<<" objects were listed.";
}

// ----------------------- Search File ---------------------------------

void  search(fstream f, char *fname, long id, char tag)
{
	Item *p;
	char x;
	int size;
	bool found=false;
	f.open(fname, ios::in|ios::binary);
	f.read(&x, 1);
	while(!f.eof() && !found)
	{
		switch(x)
		{
		case 'b':p=new Book;
			     size=sizeof(Book);
				 break;
		case 'c':p=new CD;
			     size=sizeof(CD);
				 break;
		case 't':p=new VTape;
			     size=sizeof(VTape);
				 break;
		}
		if(x!=tag)
			f.seekg(size, ios::cur);
		else
		  f.read((char *)p, size);
			if(p->getId()==id)
			{
				p->showData();
			    found=true;
			    delete p;
                break;
			}
	  f.read(&x, 1);
	}
	f.close();
	if(!found)
		cout<<"\nNo matching Item found in our file!!!!!!!";
}

// ----------------------- Program Menu ---------------------------------

int menu()
{
	int c;
	cout<<"\n            I T E M   O R D E R   P R O G R A M";
	cout<<"\n            -----------------------------------";
	cout<<"\n\nEnter:      1.....To add new Book to the file";
    cout<<"\n            2.....To add new CD to the file";
	cout<<"\n            3.....to add new VTape to the file";
	cout<<"\n            4.....Show all Books from the file";
	cout<<"\n            5.....Show all CDs from the file";
	cout<<"\n            6.....Show all VTapes from the file";
	cout<<"\n            7.....Show Everything from the file";
	cout<<"\n            8.....Search for a Book in the file";
	cout<<"\n            9.....Search for a CD in the file";
	cout<<"\n           10.....Search for a Video Tape in the file";
	cout<<"\n           11.....Search for a Specific Item";
	cout<<"\n\nEnter your choice: "; cin>>c;
	return c;
}

// ----------------------- Main Function --------------------------------

void main()
{
	Item *p;
	fstream file;
	char tag;
	long id;
	int choice, size;
	choice=menu();
	char fileName[]="items.dat";
	while(choice > 0 && choice < 12)
	{
		switch(choice)
		{
		case 1: p=new Book;
			    p->getData();
				tag=p->getType();
				size=sizeof(Book);
			    addObject(p, file, fileName, tag, size);
				delete p;
				break;
		case 2: p=new CD;
			    p->getData();
				tag=p->getType();
				size=sizeof(CD);
			    addObject(p, file, fileName, tag, size);
				delete p;
				break;
		case 3: p=new VTape;
                p->getData();
				tag=p->getType();
				size=sizeof(VTape);
				addObject(p, file, fileName, tag, size);
				delete p;
				break;
		case 4: cout<<"\nL I S T  O F  B O O K S\n";
				tag = 'b';
				display(file, fileName, tag);
				break;
		case 5: cout<<"\nL I S T    O F    C D\n";
				tag = 'c';
				display(file, fileName, tag);
				break;
		case 6: cout<<"\nL I S T  O F  V I D E O  T A P E S\n";
				tag = 't';
				display(file, fileName, tag);
				break;
		case 7: cout<<"\nL I S T  O F  A L L  I T E M S\n";
				display(file, fileName);
			    break;
		case 8: cout<<"Enter Item id :"; cin>>id;
			    tag = 'b';
				search(file, fileName, id, tag);
				break;
		case 9: cout<<"Enter Item id :"; cin>>id;
			    tag = 'c';
				search(file, fileName, id, tag);
				break;
		case 10:cout<<"Enter Item id :"; cin>>id;
			    tag = 't';
				search(file, fileName, id, tag);
				break;
		case 11:cout<<"Enter Item id :"; cin>>id;
			    cout<<"\nEnter b..Books, c..CDs, t..VTapes: ";
			    cin>>tag;
				search(file, fileName, id, tag);
		}
	choice=menu();
	}
}
Attached Files
File Type: zip Item_Order.zip (51.2 KB, 310 views)
Reply With Quote
  #2 (permalink)  
Old 10-07-03, 10:07 PM
Mud Mud is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Southern California
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Wow that sorta looks like php. Some of the commands are the same. Also isnt asp like VB6?
Nice script to bad i dont know c.
__________________
http://websoftblog.com/ - Web Software Blog
http://www.seekpire.com/ Make money searching!
http://www.gamingwise.com/ flash games!
Reply With Quote
  #3 (permalink)  
Old 10-07-03, 11:11 PM
Shane Shane is offline
Coding Addict
 
Join Date: Jun 2003
Location: Maryland, US
Posts: 268
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Mud
Wow that sorta looks like php. Some of the commands are the same. Also isnt asp like VB6?
Nice script to bad i dont know c.
The developers of PHP borrowed a few concepts from various languages.

ASP isn't a language. ASP pages can be created in VBScript, JScript or various other third party supported languages. VBScript is similiar to VB, yes.
__________________
Shane Bauer
Microsoft Certified Professional (MCP) - ASP.NET
ASP/ASP.net, C#, VB/VB.NET, PHP, Perl, SQL
Reply With Quote
  #4 (permalink)  
Old 10-08-03, 02:37 AM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Thumbs up

Thanks Mud. You are quite correct. C++ and PHP have a lot of their syntax incommon. Like Shane said, it seems that the developers of PHP borrowed a few syntax and definitions from C++, which I think is a smart thing. Most C++ have no problem at all switching to PHP, but it's a little bit more work if you want to switch from PHP to C++.
Reply With Quote
  #5 (permalink)  
Old 11-06-03, 03:22 AM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Correct me if im wrong but PHP is more like C than C++ because PHP inherits alot of C namespacing like File handling (ie. fopen, fputs, fgets, fread etc). C++ is OOP and uses different namespacing for standard libraries. Java is more like C++ when it comes to syntax of OO, not PHP.

Reply With Quote
  #6 (permalink)  
Old 11-06-03, 04:30 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Talking

Well, if you wanted to be more accurate C++ comes from C as well. So technically you could say that C is the grandfather of both of them.


Quote:
Originally Posted by evo4ever
Correct me if im wrong but PHP is more like C than C++ because PHP inherits alot of C namespacing like File handling (ie. fopen, fputs, fgets, fread etc). C++ is OOP and uses different namespacing for standard libraries. Java is more like C++ when it comes to syntax of OO, not PHP.

__________________
Reply With Quote
  #7 (permalink)  
Old 11-06-03, 05:13 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by digioz
Well, if you wanted to be more accurate C++ comes from C as well. So technically you could say that C is the grandfather of both of them.
The context of programming dictates that C and C++ are different languages, they may be related in many ways but they are officially different so therefore what you said is inacurrate becuase you are associating C and C++ together as a single language. So PHP is more like C not C++ if you were to refer them as separate languages... which they are.

Reply With Quote
  #8 (permalink)  
Old 11-07-03, 08:18 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
I don't think you read my comment carefully enough. What I said was that C++ inherated many of the same functions from C. I did not say they were the same language, nor did I state that they have the same exact syntax. So your comments about my comments are incorrect.

In any case, I think we kind of killed this subject. If you have any sample C or C++ code I would be happy to take a look at it for you, but otherwise I think we should just leave it at that.


Quote:
Originally Posted by evo4ever
The context of programming dictates that C and C++ are different languages, they may be related in many ways but they are officially different so therefore what you said is inacurrate becuase you are associating C and C++ together as a single language. So PHP is more like C not C++ if you were to refer them as separate languages... which they are.

__________________
Reply With Quote
  #9 (permalink)  
Old 11-08-03, 04:57 PM
evo4ever evo4ever is offline
Software Developer Guru
 
Join Date: Aug 2003
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by digioz
I don't think you read my comment carefully enough. What I said was that C++ inherated many of the same functions from C. I did not say they were the same language, nor did I state that they have the same exact syntax. So your comments about my comments are incorrect.

In any case, I think we kind of killed this subject. If you have any sample C or C++ code I would be happy to take a look at it for you, but otherwise I think we should just leave it at that.
Why would I want to show you my source code? I'd be practically giving away my applications which are worth alot money, besides I don't have the legal rights to publish my source code, whatever I write for the company I work for belongs to my company no one else. And no I don't casually write apps for public use, I havn't the time.

However, Your cmd line Item ordering program is very good. You should make one using the windows api next...
#include <windows.h>

Last edited by evo4ever; 11-08-03 at 05:11 PM.
Reply With Quote
  #10 (permalink)  
Old 11-09-03, 04:34 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,171
Thanks: 3
Thanked 9 Times in 9 Posts
Thanks man. Glad to hear you like my little program. Even though I also write programs for sale, I do some free coding on the side for people who don't have the money to buy scripts. Being that I own DigiOz.com, I can easily efford to publish simple source codes like the one above.

Pete


Quote:
Originally Posted by evo4ever
Why would I want to show you my source code? I'd be practically giving away my applications which are worth alot money, besides I don't have the legal rights to publish my source code, whatever I write for the company I work for belongs to my company no one else. And no I don't casually write apps for public use, I havn't the time.

However, Your cmd line Item ordering program is very good. You should make one using the windows api next...
#include <windows.h>
__________________
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 04:35 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.