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