Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 1st, 2012  |  syntax: C++  |  size: 15.10 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <string.h>
  7.  
  8. using namespace std;
  9.  
  10. class invoice
  11. {
  12.     int cNo;
  13.     char cName[40];
  14.     char cAddr[200];
  15.     char sDate[8];
  16.     int iNo;
  17.     char ides[50];
  18.     int iQty;
  19.     int iPrice;
  20.  
  21.     public:
  22.     void write();
  23.     void search();
  24.     void modify();
  25.     void del();
  26.     void show();
  27. };
  28. void invoice::modify()
  29. {
  30.     START:
  31.     system("cls");
  32.     cout<<"-------------------MODIFY---------------------";
  33.     cout<<"\n\n1. Search \n2. Enter Customer Number \n3. Go Back\n... ";
  34.     int ch;
  35.     cin>>ch;
  36.     if(ch==1){
  37.         search();
  38.         goto START;
  39.     }
  40.     if(ch==2)
  41.     {
  42.         system("cls");
  43.         cout<<"\n\nEnter customer Number : ";
  44.         int mCno;
  45.         cin>>mCno;
  46.         fstream file;
  47.         file.open("invoice.dat",ios::in|ios::out|ios::binary);
  48.         while(file.read((char*)this ,sizeof(invoice)))
  49.         {
  50.             if(mCno==cNo)
  51.             {
  52.                 show();
  53.                 cout<<"\n\nDo you want to modify this : ?";
  54.                 char ma;
  55.                 cin>>ma;
  56.                 if(ma=='y'||ma=='Y')
  57.                 {
  58.                 cout<<"\n\nEnter New Data .";
  59.                 cout<<"\nCustomer Name (enter . to keep it same) : ";
  60.                 char mCname[50];
  61.                 fflush(stdin);
  62.                 gets(mCname);
  63.                 if(mCname[0]!='.') strcpy(cName,mCname);
  64.                 cout<<"Customer Address (enter . to keep it same) : ";
  65.                 char mCadd[200];
  66.                 fflush(stdin);
  67.                 gets(mCadd);
  68.                 if(mCadd[0]!='.') strcpy(cAddr,mCadd);
  69.                 cout<<"Date of Sale : ";
  70.                 cout<<"Date of Sale (DD/MM/YYYY) :  __/__/____\b\b\b\b\b\b\b\b\b\b";
  71.                 char a;
  72.                 int i=0;
  73.                 for(i;;)
  74.                 {
  75.                     a=getch();
  76.                     if (a>='0'&&a<='9'&&i<8)
  77.                     {
  78.                         cout<<a;
  79.                         sDate[i]=a;
  80.                         ++i;
  81.                         if (i==2 || i == 4)   cout<<"/";
  82.                     }
  83.                     else if(a=='\b'&&i>0&&i<9)
  84.                     {
  85.                         if(i==2||i==4)
  86.                         {
  87.                             cout<<"\b\b_\b";
  88.                         }
  89.                         else
  90.                         cout<<"\b_\b";
  91.                         --i;
  92.  
  93.                     }
  94.                     else if(a==13)
  95.                     {
  96.                         if(i==8)
  97.                         {
  98.                         sDate[8]='\0';
  99.                         break;
  100.                         }
  101.                     }
  102.  
  103.                 }
  104.                 cout<<"\nItem Number (enter -1 to keep it same) : ";
  105.                 int miNo;
  106.                 cin>>miNo;
  107.                 if(miNo != -1) iNo=miNo;
  108.                 cout<<"Description (enter . to keep it same) : ";
  109.                 char mdes[50];
  110.                 fflush(stdin);
  111.                 gets(mdes);
  112.                 if(mdes[0]!='.') strcpy(ides,mdes);
  113.                 cout<<"Quantity (enter -1 to keep it same) : ";
  114.                 int iq;
  115.                 cin>>iq;
  116.                 if(iq != -1) iQty = iq;
  117.                 cout<<"Item Price (enter -1 to keep it same) : ";
  118.                 int price;
  119.                 cin>>price;
  120.                 if(price!=-1) iPrice = price;
  121.                 int pos = -1*sizeof(invoice);
  122.                 file.seekp(pos,ios::cur);
  123.                 file.write((char*)this,sizeof(invoice));
  124.                 cout<<"\n\nNew Data ";
  125.                 show();
  126.                 getch();
  127.                 break;
  128.                 }
  129.                 else continue;
  130.             }
  131.         }
  132.         file.close();
  133.         goto START;
  134.  
  135.     }
  136. }
  137. void invoice::del()
  138. {
  139.     START:
  140.     system("cls");
  141.     cout<<"-------------------DELETE----------------------";
  142.     cout<<"\n\n1. Search \n2. Enter Customer Number \n3. Go Back\n... ";
  143.     int ch;
  144.     cin>>ch;
  145.     if(ch==1){
  146.     search();
  147.     goto IN;}
  148.     if(ch==2) {
  149.     IN:
  150.     fstream file;
  151.     char d;
  152.     ofstream temp;
  153.     char ag='y';
  154.     while(ag=='y'||ag=='Y')
  155.     {
  156.     system("cls");
  157.     cout<<"\n\nEnter Customer Number : ";
  158.     int cn;
  159.     cin>>cn;
  160.     file.open("invoice.dat",ios::in|ios::binary);
  161.     temp.open("temp.dat",ios::binary|ios::app);
  162.     temp.seekp(0);
  163.     file.seekg(0);
  164.     int found=0;
  165.     while(file.read((char *)this,sizeof(invoice)))
  166.     {
  167.         if(cn==cNo)
  168.         {
  169.             cout<<"--------------------FOUND---------------------\n";
  170.             show();
  171.             cout<<"\n\nDo you want to delete this entry (y/n): ";
  172.             cin>>d;
  173.             found = 1;
  174.             if(d=='y' ||d == 'Y')
  175.             {
  176.                 continue;
  177.             }
  178.             else temp.write((char*)this,sizeof(invoice));
  179.         }
  180.         else temp.write((char*)this,sizeof(invoice));
  181.  
  182.     }
  183.  
  184.     file.close();
  185.     temp.close();
  186.     remove("invoice.dat");
  187.     rename("temp.dat","invoice.dat");
  188.     if (found == 1)
  189.     {
  190.         cout<<"\nDelete successful";
  191.         cout<<"\nDelete More? (y/n): ";
  192.     }
  193.     else
  194.     {
  195.         cout<<"\nNo results found.\nTry again(y/n) : ";
  196.     }
  197.     cin>>ag;
  198.     }
  199.     goto START;
  200.     }
  201. }
  202. void invoice::search()
  203. {
  204.     ifstream file;
  205.     char ch='y';
  206.     char ch1='y';
  207.     while(ch=='y'||ch=='Y')
  208.     {
  209.         START:
  210.         system("cls");
  211.         cout<<"-------------------------------SEARCH---------------------------";
  212.         cout<<"\nSearch by : \n1. Customer Number\n2. Customer Name\n3. Date of Sale\n4. Item Number\n5. Cancel\n\n...";
  213.         int s;
  214.         cin>>s;
  215.         if(s==1)
  216.         {
  217.             ch1='y';
  218.             while(ch1=='y'||ch1=='Y')
  219.             {
  220.                 system("cls");
  221.                 cout<<"Enter Customer Number : ";
  222.                 int cn;
  223.                 cin>>cn;
  224.                 int found = 0;
  225.                 file.open("invoice.dat",ios::binary);
  226.                 file.seekg(0);
  227.                 while(file.read((char*)this,sizeof(invoice)))
  228.                 {
  229.                     if(cNo==cn)
  230.                     {
  231.                         cout<<"\n-------------------------------FOUND---------------------------\n\n";
  232.                         show();
  233.                         getch();
  234.                         found = 1;
  235.                     }
  236.                     else found = 0;
  237.                 }
  238.                 file.close();
  239.                 if(found == 0)
  240.                 {
  241.                     cout<<"\nNo Record Found";
  242.                 }
  243.                 cout<<"\n\nSearch Again (y/n) : ";
  244.                 cin>>ch1;
  245.             }
  246.             goto START;
  247.  
  248.         }
  249.         if(s==2)
  250.         {
  251.         ch1='y';
  252.         while(ch1=='y'||ch1=='Y')
  253.         {
  254.                 system("cls");
  255.                 cout<<"Enter Customer Name : ";
  256.                 char nSearch[40];
  257.                 fflush(stdin);
  258.                 gets(nSearch);
  259.                 file.open("invoice.dat",ios::binary);
  260.                 file.seekg(0);
  261.                 int ffound = 0;
  262.                 while(file.read((char*)this,sizeof(invoice)))
  263.                 {
  264.                      int i=0,j=0;
  265.                      int found = 1;
  266.                      for(i,j;cName[i]!='\0'&&nSearch[j]!='\0';)
  267.                      {
  268.                         if(toupper(nSearch[j])==toupper(cName[i]))
  269.                         {
  270.                             ++i;
  271.                             ++j;
  272.                             ffound =1;
  273.                             continue;
  274.                         }
  275.                         else
  276.                         {
  277.                             ++i;
  278.                             found =0;
  279.                         }
  280.                     }
  281.                     if (found == 1)
  282.                     {
  283.                         system("cls");
  284.                         cout<<"-------------------------------FOUND---------------------------\n\n";
  285.                         show();
  286.                         getch();
  287.                     }
  288.  
  289.                 }
  290.                 file.close();
  291.                 if(ffound == 0)
  292.                 {
  293.                     cout<<"\nNo Record Found";
  294.                 }
  295.                 cout<<"\n\nSearch Again (y/n) : ";
  296.                 cin>>ch1;
  297.             }
  298.             goto START;
  299.  
  300.  
  301.  
  302.         }
  303.         if(s==3)
  304.         {
  305.         ch1='y';
  306.             while(ch1=='y'||ch1=='Y')
  307.             {
  308.                 system("cls");
  309.                 cout<<"Date of Sale (DD/MM/YYYY) :  __/__/____\b\b\b\b\b\b\b\b\b\b";
  310.                 char a;
  311.                 char cDate[8];
  312.                 int i=0;
  313.                 for(i;;)
  314.                 {
  315.                     a=getch();
  316.                     if (a>='0'&&a<='9'&&i<8)
  317.                     {
  318.                         cout<<a;
  319.                         cDate[i]=a;
  320.                         ++i;
  321.                         if (i==2 || i == 4)   cout<<"/";
  322.                     }
  323.                     else if(a=='\b'&&i>0&&i<9)
  324.                     {
  325.                         if(i==2||i==4)
  326.                         {
  327.                             cout<<"\b\b_\b";
  328.                         }
  329.                         else
  330.                         cout<<"\b_\b";
  331.                         --i;
  332.  
  333.                     }
  334.                     else if(a==13)
  335.                     {
  336.                         if(i==8)
  337.                         {
  338.                         cDate[8]='\0';
  339.                         break;
  340.                         }
  341.                     }
  342.  
  343.                 }
  344.                 int ffound = 0;
  345.                 file.open("invoice.dat",ios::binary);
  346.                 file.seekg(0);
  347.                 while( file.read((char*)this,sizeof(invoice)))
  348.                 {
  349.                     int found = 0;
  350.                     for(i=0;cDate[i]!='\0'&&sDate[i]!='\0';++i)
  351.                     {
  352.                         if(cDate[i]!=sDate[i]) {
  353.                             found = 1;
  354.                             break;
  355.                         }
  356.                     }
  357.                     if(found == 0)
  358.                     {
  359.                         cout<<"\n-------------------------------FOUND---------------------------\n\n";
  360.                         show();
  361.                         getch();
  362.                         ffound = 1;
  363.                         break;
  364.                     }
  365.                     else ffound = 0;
  366.                 }
  367.                 file.close();
  368.                 if(ffound == 0)
  369.                 {
  370.                     cout<<"\nNo Record Found";
  371.                 }
  372.                 cout<<"\n\nSearch Again (y/n) : ";
  373.                 cin>>ch1;
  374.             }
  375.             goto START;
  376.         }
  377.         if(s==4)
  378.         {
  379.          ch1='y';
  380.             while(ch1=='y'||ch1=='Y')
  381.             {
  382.                 system("cls");
  383.                 cout<<"Enter Item Number : ";
  384.                 int in;
  385.                 cin>>in;
  386.                 int found = 0;
  387.                 file.open("invoice.dat",ios::binary);
  388.                 file.seekg(0);
  389.                 while(file.read((char*)this,sizeof(invoice)))
  390.                 {
  391.                     if(iNo==in)
  392.                     {
  393.                         cout<<"\n-------------------------------FOUND---------------------------\n\n";
  394.                         show();
  395.                         getch();
  396.                         found = 1;
  397.                     }
  398.                     else found = 0;
  399.                 }
  400.                 file.close();
  401.                 if(found == 0)
  402.                 {
  403.                     cout<<"\nNo Record Found";
  404.                 }
  405.                 cout<<"\n\nSearch Again (y/n) : ";
  406.                 cin>>ch1;
  407.             }
  408.             goto START;
  409.  
  410.         }
  411.         if(s==5)
  412.             break;
  413.         cout<<"\nSearch More : (y/n)...";
  414.         cin>>ch;
  415.     }
  416.  
  417. }
  418. void invoice::show()
  419. {
  420.     cout<<"Customer No : "<<cNo;
  421.     cout<<"\nCustomer Name : "<<cName;
  422.     cout<<"\nCustomer Address : "<<cAddr;
  423.     cout<<"\nDate of Sale : ";
  424.     int j=0;
  425.     for(int i=0;i<10;++i)
  426.     {
  427.         if (i==2||i==5)
  428.             cout<<"/";
  429.         else
  430.         {
  431.             cout<<sDate[j];
  432.             ++j;
  433.         }
  434.     }
  435.     cout<<"\nItem Number : "<<iNo;
  436.     cout<<"\nDescription : "<<ides;
  437.     cout<<"\nQuantity : "<<iQty;
  438.     cout<<"\nPrice : "<<iPrice;
  439. }
  440.  
  441. void invoice::write()
  442. {
  443.     char ch ='y';
  444.     ofstream file;
  445.     file.open("invoice.dat",ios::app|ios::binary);
  446.     while(ch=='y'||ch=='Y')
  447.     {
  448.         system("cls");
  449.         cout<<"-------------------------------Entry---------------------------";
  450.         cout<<"\nCustomer Number : ";
  451.         cin>>cNo;
  452.         cout<<"Customer Name : ";
  453.         fflush(stdin);
  454.         gets(cName);
  455.         cout<<"Customer Address : ";
  456.         fflush(stdin);
  457.         gets(cAddr);
  458.         cout<<"Date of Sale (DD/MM/YYYY) :  __/__/____\b\b\b\b\b\b\b\b\b\b";
  459.         char a;
  460.         int i=0;
  461.         for(i;;)
  462.         {
  463.             a=getch();
  464.             if (a>='0'&&a<='9'&&i<8)
  465.             {
  466.                 cout<<a;
  467.                 sDate[i]=a;
  468.                 ++i;
  469.                 if (i==2 || i == 4)   cout<<"/";
  470.             }
  471.             else if(a=='\b'&&i>0&&i<9)
  472.             {
  473.                 if(i==2||i==4)
  474.                 {
  475.                     cout<<"\b\b_\b";
  476.                 }
  477.                 else
  478.                 cout<<"\b_\b";
  479.                 --i;
  480.  
  481.             }
  482.             else if(a==13)
  483.             {
  484.                 if(i==8)
  485.                 {
  486.                 sDate[8]='\0';
  487.                 break;
  488.                 }
  489.             }
  490.  
  491.         }
  492.         cout<<"\nItem Number : ";
  493.         cin>>iNo;
  494.         cout<<"Description : ";
  495.         fflush(stdin);
  496.         gets(ides);
  497.         cout<<"Quantity Sold : ";
  498.         cin>>iQty;
  499.         cout<<"Price of Item : ";
  500.         cin>>iPrice;
  501.         file.write((char *)this,sizeof(invoice));
  502.         cout<<"\nEntry successful";
  503.         cout<<"\n\nEnter again (y/n)...";
  504.         cin>>ch;
  505.     }
  506.     file.close();
  507. }
  508.  
  509. int main()
  510. {
  511.     START:
  512.     system("cls");
  513.     cout<<"------------------------CUSTOMER INVOICE--------------------";
  514.     cout<<"\n\n1. Enter New Data\n2. Search\n3. Modify\n4. Delete\n5. View Data\n\n...";
  515.     int a;
  516.     cin>>a;
  517.     invoice in;
  518.     if(a==1)
  519.     {
  520.         system("cls");
  521.         in.write();
  522.         goto START;
  523.     }
  524.     if(a==2)
  525.     {
  526.         in.search();
  527.         goto START;
  528.     }
  529.     if(a==3)
  530.     {
  531.         in.modify();
  532.         goto START;
  533.     }
  534.     if(a==4)
  535.     {
  536.         in.del();
  537.         goto START;
  538.     }
  539.     if(a==5)
  540.     {
  541.         ifstream file;
  542.         file.open("invoice.dat",ios::binary);
  543.         invoice c;
  544.         file.seekg(0);
  545.         if(!file.read((char*)&c,sizeof(c)))
  546.         {
  547.             cout<<"No records Presents.";
  548.             getch();
  549.             goto START;
  550.         }
  551.         file.seekg(0);
  552.         while(file.read((char*)&c,sizeof(c)))
  553.         {
  554.             system("cls");
  555.             c.show();
  556.             getch();
  557.         }
  558.         file.close();
  559.         goto START;
  560.     }
  561.  
  562.     return 0;
  563. }