1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include<conio.h>
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include "antheader.h"//this is my own header to use wiht gotoxy
#include<string.h>
using namespace std;
class Customer
{
public:
char id[6], name[20], sex[6];
int qty;
float price ;
float total(int qt, float pri)
{
return qt*pri;
}
};
Customer cus;
FILE *MyFile;
FILE *temp;
char ftemp[15]="MyTempFile.txt";
char fFile[15]="MyTextFile.txt";
void manu();
void fInsert();
void view();
void fupdate();
void search();
void fdelete();
void fleave();
int main()
{
manu();
getch();
}
void manu()
{
system("cls");
int option;
gotoxy(20, 5);cout<<"1. Enter to Insert\n";
gotoxy(20, 7);cout<<"2. Enter to View\n";
gotoxy(20, 9);cout<<"3. Enter to Update\n";
gotoxy(20, 11);cout<<"4. Enter to Search\n";
gotoxy(20, 13);cout<<"5. Enter to Delete\n";
gotoxy(20, 15);cout<<"6. Enter to Escap => " ;
cin>>option;
switch(option)
{
case 1:
fInsert();
break;
case 2:
view();
break;
case 3:
fupdate();
break;
case 4:
search();
break;
case 5:
fdelete();
break;
case 6:
fleave();
break;
default:
cout<<"Invalid Option";
break;
}
}
void fInsert()
{
char check;
MyFile=fopen(fFile, "ab");
cout<<"Enter id => ";cin>>cus.id;fflush(stdin);
cout<<"Enter Name => ";gets(cus.name);
cout<<"Enter sex => ";cin>>cus.sex;
cout<<"Enter qty => ";cin>>cus.qty;
cout<<"Enter price => ";cin>>cus.price;
cout<<"Enter total => "<<cus.total(cus.qty, cus.price);
fwrite(&cus,sizeof(cus), 1, MyFile);
cout<<"\nDo you to continue if u want press Y or y else n => ";
cin>>check;
if(check=='Y' || check=='y')
{
manu();
}
fclose(MyFile);
}
void view()
{
cls();
rewind(MyFile);
MyFile=fopen(fFile, "rb");
gotoxy(0, 1);cout<<"id";
gotoxy(15, 1);cout<<"Name";
gotoxy(35, 1);cout<<"sex";
gotoxy(45, 1);cout<<"qty";
gotoxy(60, 1);cout<<"price";
gotoxy(70, 1);cout<<"Total";
int y=2;
int count=2;
while(fread(&cus, sizeof(cus), 1, MyFile))
{
gotoxy(1, y+1);cout<<cus.id;
gotoxy(15, y+1);cout<<cus.name;
gotoxy(35, y+1);cout<<cus.sex;
gotoxy(45, y+1);cout<<cus.qty;
gotoxy(60, y+1);cout<<cus.price;
gotoxy(70, y+1);cout<<cus.total(cus.qty, cus.price) ;
y++;
}
fclose(MyFile);
getch();
manu();
}
void fupdate()
{
char ask;
char upID[20];
MyFile=fopen(fFile,"rb+");
cout<<"Enter id to update ";cin>>upID;
while(fread(&cus, sizeof(cus), 1, MyFile))
{
if(strcmp(cus.id,upID)==0)
{
fseek(MyFile, -sizeof(cus),SEEK_CUR);
cout<<"Enter Name to Update ";cin>>cus.name;
cout<<"Enter Sex to Update ";cin>>cus.sex;
cout<<"Enter Qty to Update ";cin>>cus.qty;
cout<<"Enter Price to Update ";cin>>cus.price;
cus.total(cus.qty, cus.price);
fwrite(&cus, sizeof(cus), 1, MyFile);
break;
}
}
cout<<"if Y continue update ";cin>>ask;
if(ask=='Y' || ask=='y')
{
manu();
}
fclose(MyFile);
}
void search()
{
char idSearch[20];
MyFile=fopen(fFile, "rb");
gotoxy(20, 17);cout<<"Enter id to Search ";cin>>idSearch;
int y=18;
while(fread(&cus, sizeof(cus), 1, MyFile))
{
if(strcmp(cus.id, idSearch)==0)
{
gotoxy(1, y+1);cout<<cus.id;
gotoxy(5, y+1);cout<<cus.name;
gotoxy(10, y+1);cout<<cus.sex;
gotoxy(15, y+1);cout<<cus.qty;
gotoxy(20, y+1);cout<<cus.price;
gotoxy(25, y+1);cout<<cus.total(cus.qty, cus.price) ;
}
}
fclose(MyFile);
}
void fdelete()
{
char idDelete[20];
MyFile=fopen(fFile, "rb");
temp=fopen(ftemp, "a+b");
gotoxy(20, 17);cout<<"Enter id to delete ";cin>>idDelete;
while(fread(&cus, sizeof(cus), 1, MyFile))
{
if(strcmp(cus.id, idDelete)!=0)
{
fwrite(&cus, sizeof(cus), 1, temp);
}
}
fclose(MyFile);
fclose(temp);
remove(fFile);
rename(ftemp, fFile);
}
void fleave()
{
char ask;
cout<<"are you sure to leave if Y coninoue else Exit";
cin>>ask;
if(ask=='y' || ask=='Y')
{
manu();
}
else
{
exit(0);
}
}
|