PDA

View Full Version : Short Command Line Based Item Order Program


digioz
10-07-03, 10:46 PM
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!!


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

Mud
10-07-03, 11:07 PM
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.

Shane
10-08-03, 12:11 AM
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.

digioz
10-08-03, 03:37 AM
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++. :)

evo4ever
11-06-03, 04:22 AM
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.

:)

digioz
11-06-03, 05:30 PM
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. :D


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.

:)

evo4ever
11-06-03, 06:13 PM
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. :D

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.

:)

digioz
11-07-03, 09:18 PM
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.


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.

:)

evo4ever
11-08-03, 05:57 PM
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> ;)

digioz
11-09-03, 05:34 PM
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


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> ;)

evo4ever
11-11-03, 05:20 PM
Hey digioz. I've launched an open source project called "php2cpp". It gives developers the ability to see what kind of output a certain PHP function does in command line style (its so incase you don't have PHP installed locally and your server is down). The catch is that I've gotta write a library of PHP-Named member functions in C++ (class = php). I've done a few, and so far I havn't bumped into any trouble. The easiest functions will be created first, like all of the string manipulation functions (ie: strlen, strpos/strrpos, strstr/stristr, strrchr, strrev, substr, substr_replace, str_replace, str_ireplace, etc etc). Coding functions like these in C++ won't be hard, seen as tho a string is an array of chars in C++. Think of why it would be easy because of this! ;) So you want to help out?

digioz
11-11-03, 07:02 PM
Sure man, I will give it a shot. Post your code in a zip file and I will take a look at it. :)

Or if you don't want the source code exposed email me instead.


Pete


Hey digioz. I've launched an open source project called "php2cpp". It gives developers the ability to see what kind of output a certain PHP function does in command line style (its so incase you don't have PHP installed locally and your server is down). The catch is that I've gotta write a library of PHP-Named member functions in C++ (class = php). I've done a few, and so far I havn't bumped into any trouble. The easiest functions will be created first, like all of the string manipulation functions (ie: strlen, strpos/strrpos, strstr/stristr, strrchr, strrev, substr, substr_replace, str_replace, str_ireplace, etc etc). Coding functions like these in C++ won't be hard, seen as tho a string is an array of chars in C++. Think of why it would be easy because of this! ;) So you want to help out?

evo4ever
11-11-03, 07:28 PM
Hey, do u have msn?

digioz
11-12-03, 02:23 AM
No, I only use Yahoo I.M. Do you Yahoo? :)

Hey, do u have msn?

evo4ever
11-12-03, 09:07 AM
No, I only use Yahoo I.M. Do you Yahoo? :)

C'mon! who these days DOESNT use MSN Messenger??? Its like the most popular instant messaging service available! But anyway... i dont have Yahoo im affraid :( What is your email address?

PS: I may use Yahoo if I can use my msn address.

digioz
11-12-03, 11:33 AM
My email is digi_oz@yahoo.com . Just email me.



C'mon! who these days DOESNT use MSN Messenger??? Its like the most popular instant messaging service available! But anyway... i dont have Yahoo im affraid :( What is your email address?

PS: I may use Yahoo if I can use my msn address.

evo4ever
11-12-03, 12:00 PM
My email is digi_oz@yahoo.com . Just email me.

I've added and emailed you.

Pukster
11-18-03, 06:16 PM
Why would I want to show you my source code? I'd be practically giving away my applications which are worth alot money


Open source would be rolling in it's grave if it heard what you just said

Hackerdragons
11-24-03, 12:35 PM
Because PHP Is Coded With C++ It's Normal They've A Lot In Common.
Anyway. IT's A Good Program But When You Get Output, IT Must Tell What Is What.
It Writes
String1 String2 String3
String4 String5 String6
:)

VisualActive
12-26-03, 04:45 PM
C'mon! who these days DOESNT use MSN Messenger??? Its like the most popular instant messaging service available! But anyway... i dont have Yahoo im affraid :( What is your email address?

PS: I may use Yahoo if I can use my msn address.
i dont. :D

digioz
12-27-03, 09:17 PM
Heheh..... Just think how much cheaper software would be right now if it wasn't for Micro$oft raising the price of their operating system constantly. :D



i dont. :D