Thursday, July 21, 2011

Sample project in C++

#include<iostream.h>
#include<process.h>
 #include<stdio.h>
 #include<conio.h>
 #include<fstream.h>
 class student
 {
 // int rlno;
 char na[30];
 public:
 int rlno;
 void getdata()
 {
 cout<<"\n Enter Name ";
 gets(na);
 cout<<"\n Enter roll number ";
 cin>>rlno;

}



void show()

{

// cout<<"\n Name is "<<na;

// cout<<"\n Roll number is "<<rlno;

cout<<endl<<rlno<<"\t\t\t\t"<<na;

}

};



void main()

{

clrscr();

fstream fp,temp;//objects to control files.

student stu;

int ch=0;

while(ch!=5)

{

clrscr();

cout<<"\n1. Add Student";

cout<<"\n2. List all";

cout<<"\n3. Edit";

cout<<"\n4. Delete";

cout<<"\n5. Exit";

cout<<"\n Enter your choice: ";

cin>>ch;

switch(ch)

{

case 1:

fp.open("mystu.txt",ios::app);

char ch1='y';

while(ch1=='y' || ch1=='Y')

{

cout<<"\n Enter Student details";

stu.getdata();

fp.write((char*)&stu,sizeof(stu));

cout<<"\n Do you want to add more records ";

fflush(stdin);

cin>>ch1;





}

fp.close();

break;



case 2:

clrscr();

int r=1;

fp.open("mystu.txt",ios::in);

cout<<"\nRoll Number\t\t\tName";

while(fp.read((char*)&stu,sizeof(stu)))

{

if(r>10)

{

cout<<"\n Press any key to cont...";

getch();

clrscr();

cout<<"\nRoll Number\t\t\tName";

r=1;

}

else

{

stu.show();

r++;

}

}

getch();

fp.close();



break;



case 3:

fp.open("mystu.txt",ios::in);

temp.open("temp.txt",ios::out);

int rl;

int fl=0;

char cnf='y';

cout<<"\n Enter roll number you want to edit";

cin>>rl;

while(fp.read((char*)&stu,sizeof(stu)))

{

if(stu.rlno==rl)

{

stu.show();

cout<<"\n Press y to edit or any key to ignore Update record";

fflush(stdin);

cin>>cnf;



if(cnf=='y' || cnf=='Y')

{

stu.getdata();

temp.write((char*)&stu,sizeof(stu));

fl=1;

}

else

{

temp.write((char*)&stu,sizeof(stu));

}

}

else

{



temp.write((char*)&stu,sizeof(stu));

}

}

if(fl==1)

cout<<"\n Record Updated";

else

cout<<"\n No Matching record found or Update ignored by user";

getch();



fp.close();

temp.close();

remove("mystu.txt");

rename("temp.txt","mystu.txt");



break;



case 4:



fp.open("mystu.txt",ios::in);

temp.open("temp.txt",ios::out);

int rld;

int fld=0;

char cnfd='y';

cout<<"\n Enter roll number you want to edit";

cin>>rld;

while(fp.read((char*)&stu,sizeof(stu)))

{

if(stu.rlno==rld)

{

stu.show();

cout<<"\n Press y to Delete or any key to save record";

fflush(stdin);

cin>>cnfd;



if(cnfd=='y' || cnfd=='Y')

{



fld=1;

}

else

{

temp.write((char*)&stu,sizeof(stu));

}

}

else

{



temp.write((char*)&stu,sizeof(stu));

}

}

if(fld==1)

cout<<"\n Record Deleted";

else

cout<<"\n No Matching record found or Saved by User";

getch();



fp.close();

temp.close();

remove("mystu.txt");

rename("temp.txt","mystu.txt");





break;



case 5:

cout<<"\n Thanks for using our software";

getch();

exit(0);

break;



default:

cout<<"\n Invalid Option";

getch();





}//main switch



}//while loop



}//main























Library Management System (Sample)

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<fstream.h>

#include<string.h>

#include<dos.h>

void book_menu();

void member_menu();

void issue_menu();

class book

{

public:

int bkno;

char bkna[30];

char auth[30];

char edi[20];

char pags[10];

char pblis[40];

void add();

void search();

void del();

void modify();

void show();

void exit();

};

void book::add()

{

cout<<"\n Enter book no. : ";

cin>>bkno;

cout<<"\n Enter book name : ";

gets(bkna);

cout<<"\n Enter author name : ";

gets(auth);

cout<<"\n Enter book edition : ";

gets(edi);

cout<<"\n Enter pages : ";

gets(pags);

cout<<"\n Enter book Publisher : ";

gets(pblis);

}

void book::search()

{

book b;

int d;

int f=0;

ifstream ob1;

ob1.open("book.txt",ios::in);

cout<<"\n Enter book no. to search : ";

cin>>d;

ob1.read((char*)&b,sizeof(b));

while(ob1)

{

if(b.bkno==d)

{

b.show();

cout<<"\n Book found.";

f=1;

break;

}

else

{

f=0;

}

ob1.read((char*)&b,sizeof(b));

}

if(f==0)

{

cout<<"\n Book not found.";

}

ob1.close();

getch();

}

void book::del()

{

book b;

int d;

ifstream ob2;

ofstream ob3;

ob2.open("book.txt",ios::in);

ob3.open("temp.txt",ios::app);

cout<<"\n Enter book no. to delete : ";

cin>>d;

ob2.read((char*)&b,sizeof(b));

while(ob2)

{

if(b.bkno==d)

{

b.show();

cout<<"\n Record deleted.";

}

else

{

ob3.write((char*)&b,sizeof(b));

}

ob2.read((char*)&b,sizeof(b));

}

remove("book.txt");

rename("temp.txt","book.txt");

ob2.close();

ob3.close();

getch();

}

void book::modify()

{

book b;

int d;

ifstream ob4;

ofstream ob5;

ob4.open("book.txt",ios::in);

ob5.open("temp.txt",ios::app);

cout<<"\n Enter book no. to modify record : ";

cin>>d;

ob4.read((char*)&b,sizeof(b));

while(ob4)

{

if(b.bkno==d)

{

b.show();

cout<<"\n Enter new values for record";

b.add();

ob5.write((char*)&b,sizeof(b));

}

else

{

ob5.write((char*)&b,sizeof(b));

}

ob4.read((char*)&b,sizeof(b));

}

remove("book.txt");

rename("temp.txt","book.txt");

ob4.close();

ob5.close();

getch();

}

void book::show()

{

cout<<"\n Book no. : "<<bkno;

cout<<"\n Book name : "<<bkna;

cout<<"\n Author's name : "<<auth;

cout<<"\n Book Edition : "<<edi;

cout<<"\n Book Pages : "<<pags;

cout<<"\n Book publisher : "<<pblis;

}

void book::exit()

{

getch();

}

class member

{

char nm[50];

char pno[40];

char val[30];

int idno;

char ad[100];

char mems[30];

public:

void add();

void search();

void del();

void modify();

void show();

void exit();

};

void member::add()

{

cout<<"\n Enter member name : ";

gets(nm);

cout<<"\n Enter id no. : ";

cin>>idno;

cout<<"\n Enter member address : ";

gets(ad);

cout<<"\n Enter phone no. : ";

gets(pno);

cout<<"\n Membership started on : ";

gets(mems);

cout<<"\n Membership valid upto : ";

gets(val);

}

void member::search()

{

member mem;

int d;

ifstream ob6;

int f=0;

ob6.open("meb.txt",ios::in);

cout<<"\n Enter id no. to search member : ";

cin>>d;

ob6.read((char*)&mem,sizeof(mem));

while(ob6)

{

if(mem.idno==d)

{

mem.show();

cout<<"\n Record found.";

f=1;

break;

}

else

{

f=0;

}

ob6.read((char*)&mem,sizeof(mem));

}

if(f==0)

{

cout<<"\n Record not found.";

}

ob6.close();

getch();

}

void member::del()

{

member mem;

int d;

ifstream ob7;

ofstream ob8;

ob7.open("meb.txt",ios::in);

ob8.open("temp.txt",ios::app);

cout<<"\n Enter id no. to delete a member : ";

cin>>d;

ob7.read((char*)&mem,sizeof(mem));

while(ob7)

{

if(mem.idno==d)

{

mem.show();

cout<<"\n Record deleted.";

}

else

{

ob8.write((char*)&mem,sizeof(mem));

}

ob7.read((char*)&mem,sizeof(mem));

}

remove("meb.txt");

rename("temp.txt","meb.txt");

ob7.close();

ob8.close();

getch();

}

void member::modify()

{

member mem;

int d;

ifstream ob9;

ofstream ob10;

ob9.open("meb.txt",ios::in);

ob10.open("temp.txt",ios::app);

cout<<"\n Enter id no. to modify record : ";

cin>>d;

ob9.read((char*)&mem,sizeof(mem));

while(ob9)

{

if(mem.idno==d)

{

mem.show();

cout<<"\n Enter new values for record";

mem.add();

ob10.write((char*)&mem,sizeof(mem));

}

else

{

ob10.write((char*)&mem,sizeof(mem));

}

ob9.read((char*)&mem,sizeof(mem));

}

remove("meb.txt");

rename("temp.txt","meb.txt");

ob9.close();

ob10.close();

getch();

}

void member::show()

{

cout<<"\n Id no. : "<<idno;

cout<<"\n Member name : "<<nm;

cout<<"\n Address of member : "<<ad;

cout<<"\n Phone no : "<<pno;

cout<<"\n Membership started on : "<<mems;

cout<<"\n Membership valid upto : "<<val;

}

void member::exit()

{

getch();

}

class issue

{

public:

char baa[50];

char buka[50];

char editi[59];

char doi[50];

char bi[50];

char mno[50];

char tik[40];

int bn;

issue()

{

strcpy(baa,"");

bn=0;

}

void add();

void search();

void del();

void modify();

void show();

void exit();

};



void issue::add()

{

cout<<("\n Enter book name : ");

gets(baa);

cout<<("\n Enter book author : ");

gets(buka);

cout<<("\n Enter edition : ");

gets(editi);

cout<<"\n Enter date of issue(Enter date without spaces) : ";

gets(doi);

cout<<"\n Book issued to : ";

gets(bi);

cout<<"\n Member id no. : ";

gets(mno);

cout<<"\n Ticket no. : ";

gets(tik);

}



void issue::search()

{

issue iss;

int d;

ifstream ob11;

int f=0;

ob11.open("isu.txt",ios::in);

cout<<"\n Enter book no. to search : ";

cin>>d;

ob11.read((char*)&iss,sizeof(iss));

while(ob11)

{

if(iss.bn==d)

{

iss.show();

cout<<"\n Book found.";

f=1;

break;

}

else

{

f=0;

}

ob11.read((char*)&iss,sizeof(iss));

}

if(f==0)

{

cout<<"\n Book is already issued.";

}

ob11.close();

getch();

}

void issue::del()

{

issue iss;

int d;

ifstream ob12;

ofstream ob13;

ob12.open("isu.txt",ios::in);

ob13.open("temp.txt",ios::app);

cout<<"\n Enter book no. to search : ";

cin>>d;

ob12.read((char*)&iss,sizeof(iss));

while(ob12)

{

if(iss.bn==d)

{

iss.show();

cout<<"\n Record deleted.";

}

else

{

ob13.write((char*)&iss,sizeof(iss));

}

ob12.read((char*)&iss,sizeof(iss));

}

remove("isu.txt");

rename("temp.txt","isu.txt");

ob12.close();

ob13.close();

getch();

}

void issue::modify()

{

issue iss;

int d;

ifstream ob14;

ofstream ob15;

ob14.open("isu.txt",ios::in);

ob15.open("temp.txt",ios::app);

cout<<"\n Enter book no. to modify record :";

cin>>d;

ob14.read((char*)&iss,sizeof(iss));

while(ob14)

{

if(iss.bn==d)

{

iss.show();

cout<<"\n Enter new values for record";

iss.add();

ob15.write((char*)&iss,sizeof(iss));

}

else

{

ob15.write((char*)&iss,sizeof(iss));

}

ob14.read((char*)&iss,sizeof(iss));

}

remove("isu.txt");

rename("temp.txt","isu.txt");

ob14.close();

ob15.close();

getch();

}

void issue::show()

{

cout<<"\n Book no : "<<bn;

cout<<"\n Book name : "<<baa;

cout<<"\n Book author : "<<buka;

cout<<"\n Book edition : "<<editi;

cout<<"\n Date of issue : "<<doi;

cout<<"\n Book issued to : "<<mno;

cout<<"\n Member id no. : "<<bi;

cout<<"\n Ticket no. : "<<tik;

}

void issue::exit()

{

getch();

}

class retrn

{

int bnn;

char ba[30];

char dor[20];

public:

void add();

void search();

void del();

void modify();

void show();

void exit();

};

void retrn::add()

{

issue iss;

retrn ret;

char ch='y';

ifstream ob16;

int d;

while(ch=='y'||ch=='Y')

{

ob16.open("file.txt",ios::in);

cout<<"\n Enter book no.";

cin>>bnn;

ob16.read((char*)&ret,sizeof(ret));

while(ob16)

{

if(iss.bn==bnn)

{

cout<<"\n Enter the date of return(Enter date without spaces) : ";

cin>>dor;

}

else

{

exit();

}

}

cout<<"\n Enter 'y' to continue : ";

cin>>ch;

}

}

void retrn::search()

{

retrn ret;

int d;

ifstream ob17;

int f=0;

ob17.open("file.txt",ios::in);

cout<<"Enter book no. to search : ";

cin>>d;

ob17.read((char*)&ret,sizeof(ret));

while(ob17)

{

if(ret.bnn==d)

{

ret.show();

cout<<"\n Book returned.";

f=1;

break;

}

else

{

f=0;

}

}

if(f==0)

{

cout<<"\n Book not returned.";

}

ob17.close();

}

void retrn::del()

{

retrn ret;

int d;

ifstream ob18;

ofstream ob19;

ob18.open("file.txt",ios::in);

ob19.open("temp.txt",ios::app);

cout<<"\n Enter book no. to search : ";

cin>>d;

ob18.read((char*)&ret,sizeof(ret));

while(ob18)

{

if(ret.bnn==d)

{

ret.show();

cout<<"\n Record deleted.";

}

else

{

ob19.write((char*)&ret,sizeof(ret));

}

ob18.read((char*)&ret,sizeof(ret));

}

remove("file.txt");

rename("temp.txt","file.txt");

ob18.close();

ob19.close();

}

void retrn::modify()

{

retrn ret;

int d;

ifstream ob20;

ofstream ob21;

ob20.open("file.txt",ios::in);

ob21.open("temp.txt",ios::app);

cout<<"Enter book no. to modify a record.";

cin>>d;

ob20.read((char*)&ret,sizeof(ret));

while(ob20)

{

if(ret.bnn==d)

{

ret.show();

cout<<"\n Enter new values for this record.";

ret.add();

ob21.write((char*)&ret,sizeof(ret));

}

else

{

ob21.write((char*)&ret,sizeof(ret));

}

ob20.read((char*)&ret,sizeof(ret));

}

remove("file.txt");

rename("temp.txt","file.txt");

ob20.close();

ob21.close();

getch();

}

void retrn::show()

{

cout<<"\n Book no. : "<<bnn;

cout<<"\n Book name : "<<ba;

cout<<"\n Date of issue : "<<dor;

}

void retrn::exit()

{

getch();

}

void book_menu()

{

char c='y';

ifstream ob1;

ofstream ob;

book b;

int ch;

clrscr();

cout<<"\n 1. Add new record";

cout<<"\n 2. Search record";

cout<<"\n 3. Delete record";

cout<<"\n 4. Modify record";

cout<<"\n 5. Show record";

cout<<"\n 6. Back to menu";

cout<<"\n Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

clrscr();

ob.open("book.txt",ios::app);

while(c=='y'||c=='Y')

{

b.add();

ob.write((char*)&b,sizeof(b));

cout<<"\n Press y to Continue : ";

cin>>c;

}

ob.close();

break;

case 2:

clrscr();

b.search();

break;

case 3:

clrscr();

b.del();

break;

case 4:

clrscr();

b.modify();

break;

case 5:

clrscr();

ob1.open("book.txt",ios::in);

ob1.read((char*)&b,sizeof(b));

while(ob1)

{

b.show();

ob1.read((char*)&b,sizeof(b));

}

ob1.close();

getch();

break;

case 6:

clrscr();

b.exit();

break;

}

}

void member_menu()

{

ofstream ob;

ifstream ob1;

int ch;

int days;

member mem;

char c='y';

clrscr();

cout<<"\n 1. Add new record";

cout<<"\n 2. Search record";

cout<<"\n 3. Delete record";

cout<<"\n 4. Modify record";

cout<<"\n 5. Show record";

cout<<"\n 6. Back to menu";

cout<<"\n Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

clrscr();

ob.open("meb.txt",ios::app);

while(c=='y'||c=='Y')

{

mem.add();

ob.write((char*)&mem,sizeof(mem));

cout<<"\n Press y to continue : ";

cin>>c;

}

ob.close();

break;

case 2:

clrscr();

mem.search();

break;

case 3:

clrscr();

mem.del();

break;

case 4:

clrscr();

mem.modify();

break;

case 5:

clrscr();

ob1.open("meb.txt",ios::in);

ob1.read((char*)&mem,sizeof(mem));

while(ob1)

{

mem.show();

ob1.read((char*)&mem,sizeof(mem));

}

ob1.close();

getch();

break;

case 6:

clrscr();

mem.exit();

break;

}

}

void issue_menu()

{

ofstream ob;

ifstream ob1;

int ch;

issue iss;

textcolor(RED);

cprintf("Warning : Book must be returned within a week or there is a fine of Rs.
2 / day.");

cout<<"\n 1. Add new record";

cout<<"\n 2. Search record";

cout<<"\n 3. Delete record";

cout<<"\n 4. Modify record";

cout<<"\n 5. Show record";

cout<<"\n 6. Back to menu";

cout<<"\n Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

clrscr();

book b;

int bn1;

int f=0;

ifstream ob11;

ofstream ob12;



ob12.open("isu.txt",ios::app);

ob11.open("book.txt",ios::in);

cout<<"\n Enter book no. you want to Issue : ";

cin>>bn1;

ob11.read((char*)&b,sizeof(b));

while(ob11)

{

if(b.bkno==bn1)

{

iss.bn=bn1;

iss.add();

f=1;

break;

}

else

{

f=0;

}

ob11.read((char*)&b,sizeof(b));

}



if(f==0)

cout<<"\n Book not available.";

else

ob12.write((char*)&iss,sizeof(iss));



ob11.close();

ob12.close();

getch();

getch();

break;

case 2:

iss.search();

break;

case 3:

iss.del();

break;

case 4:

iss.modify();

break;

case 5:

ob1.open("isu.txt",ios::in);

ob1.read((char*)&iss,sizeof(iss));

while(ob1)

{

iss.show();

ob1.read((char*)&iss,sizeof(iss));

cout<<"\n Press any key to continue.";

getch();

}

ob1.close();

getch();

break;

case 6:

iss.exit();

break;

}

}

void main()

{

int ch;

clrscr();

while(ch!=4)

{

clrscr();

textcolor(MAGENTA+BLINK);

cprintf("..........********** Welcome to 'Library Management
Project'**********..........");

textcolor(MAGENTA);

cprintf("................................................................................");

textcolor(YELLOW);

cprintf("\n\nSubmitted To : Prem Saini Sir Submitted by : Inderjeet Singh");

textcolor(YELLOW);

cprintf("\n................................................................................");

cout<<"\n 1. Book Menu";

cout<<"\n 2. Member Menu";

cout<<"\n 3. Issue Menu";

cout<<"\n 4. Exit";

cout<<"\n Enter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

clrscr();

book_menu();

break;

case 2:

clrscr();

member_menu();

break;

case 3:

clrscr();

issue_menu();

break;

case 4:

textcolor(MAGENTA);

cprintf("\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n Thanks for using it!!!");

getch();

}

}

};




To print system time


/* localtime example */
#include
#include

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
 
  return 0;
}

Friday, July 15, 2011

List of C/C++ programes

1 Program to add two numbers.
2 Program to use endl manipulator.
3 Program to calculate area and circumference of a circle.
4 Program to calculate average of three numbers.
5 Program to swap two numbers without using a third variable.
6 Program to find out the greater number.
7 Program to find whether a number is palindrome or not.
8 Program to find salary of an employee using switch statement.
9 Program to find out whether number is even or odd.
10 Program to convert temperature from degree centigrade to Fahrenheit.
11 Program to calculate the value of the function.
12 Program to find the sum of n natural numbers using for loop.
13 Program to find the sum of n natural numbers using do loop.
14 Program to find the sum of n natural numbers using while loop.
15 Program to calculate the volume of boxes using objects and classes.
16 Program to find out the percentage of student marks using classes.
17 Program to use a private data member.
18 Program to implement static member data.
19 Program to implement static member function.
20 Program to implement the friend function.
21 Program to implement friend classes.
22 Program to implement constructor.
23 Program to implement default constructor.
24 Program to implement parameterized constructor.
25 Program to implement multiple constructors in a class.
26 Program to implement default copy constructor.
27 Program to implement user defined copy constructor.
28 Program to implement constructor overloading.
29 Program to implement operator overloading : ++ operator
30 Program to print Fibonacci series using ++ operator overloading.
31 Program to implement the assignment operator overloading- points
32 Program to implement the assignment operator overloading-complex numbers.
33 Program to implement the relational operator overloading-points
34 Program to implement the binary + operator overloading -complex numbers.
35 Program to concat two strings using + operator overloading.
36 Program to implement deconstructor.