Popular Posts

6/24/2012

Sample Book Info in C++

the following code :
======================

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<process.h>
class Book{
private:
int id;
char bookname[30];
char author[30];
float price;
public:
void input();
void output();
 friend void search_update(Book t[],int n);
Book();
Book(int i, char *n, char *a, float p);
char *getbookname(){
return bookname;
}
float getprice(){
return price;}
int getid(){
return id;
}


 // ~Book(){cout<<" Destructor Function "<<endl;}
};
Book::Book(){
id=0;
strcpy(bookname,"Unknow");
strcpy(author,"Unknown");
price=0.0;
}
Book::Book(int i, char *n, char *a, float p){
id=i;
strcpy(bookname,n);
strcpy(author,a);
price=p;
}


void Book::input(){
cout<<"Input id:";
cin>>id;
cout<<"Input Bookname:";
cin.clear();cin.seekg(0,ios::end);cin.get(bookname,30);
cout<<"Input Author:";
cin.clear();cin.seekg(0,ios::end);cin.get(author,30);
cout<<"Input Price:";
cin>>price;
}
void Book::output(){
cout<<getid()<<"\t"<<getbookname()<<"\t"<<author<<"\t"<<getprice()<<"$"<<endl;
}

void sort(Book a[],int n)
{
Book t;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[i].getprice()<a[j].getprice())
{
t=a[i];
a[j]=a[i];
a[i]=t;
}
}
float total(Book a[],int n)
{
float s;
for(int i=0;i<n;i++)
s=0;
s=s+a[i].getprice();
return s;
}
void search_update(Book t[],int n)
{ char search[20];
int choice=-1;
cout<<"\nEnter bookname you want to update:"<<endl;
cin.clear();
cin.seekg(0,ios::end);
cin.get(search,20);
for(int i=0;i<n;i++)
if(strcmp(search,t[i].bookname)==0)
{ cout<<"Press 1 to update id"<<endl;
cout<<"Press 2 to update bookname"<<endl;
cout<<"Press 3 to update author"<<endl;
cout<<"Press 4 to update price"<<endl;
cin>>choice;
switch(choice)
{ case 1: cout<<"Enter new ID:";
cin>>t[i].id;
break;
case 2: cout<<"Enter new bookname:";
cin.clear();
cin.seekg(0,ios::end);
cin.get(t[i].bookname,30);
break;
case 3: cout<<"Enter new Author:";
cin.clear();
cin.seekg(0,ios::end);
cin.get(t[i].author,20);
break;
case 4: cout<<"Enter new Price:";
cin>>t[i].price;
break;
}
}
if(choice==-1)
cout<<"Not Found !!"<<endl;
}
void main(){
int i,n;

Book a[3]={Book(1,"Grammar","Dara",3.0),Book(2,"Computer","Net",2.0),Book(3,"Conversation","Sok",1.0)};
cout<<"Data of Initialize"<<endl;
for(i=0;i<3;i++)
a[i].output();
cout<<"Input n:";
cin>>n;
cout<<"=================================="<<endl;
Book *p=new Book[n];
for(i=0;i<n;i++)
p[i].input();
cout<<"=================================="<<endl;
cout<<"Original Data"<<endl;
for(i=0;i<n;i++)
p[i].output();
char ch;
d:
do{
cout<<"Press 1 for Search and update "<<endl;
cout<<"Press 2 for Sort "<<endl;
cout<<"Press 3 Total of Book Price"<<endl;
cout<<"Press 4 for Exit"<<endl;
cout<<"Menu Choice: "<<endl;
cin>>ch;

switch(ch)
{


case '1':
clrscr();
cout<<"Please Type Bookname for search and update"<<endl;
//cout<<"\nID\tTitle\tAuthor\tPrice";
for(i=0;i<n;i++)
p[i].output();
search_update(p,n);
for(i=0;i<n;i++)
p[i].output();
break;

case '2':

clrscr();
cout<<"After sort By Price "<<endl;
sort(a,n);
for(i=0;i<n;i++)

p[i].output();
break;

case '3':
clrscr();
//cout<<"All Total Price:"<<total(a[i],n)<<endl;

break;
case '4':
clrscr();

break;
case 's':exit(0);break;
default : cout<<"Invalid key Press againt"<<endl;

}
}
while (ch=='y' || ch=='Y');goto d;

}



0 comments:

Post a Comment