#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<dos.h>
int pass();
void function();
void password();
void mainmenu();
void transaction();
void add();
void search();
void deleted();
void report();
char *get_pass(size_t size);
char *globalStr;
void main()
{
clrscr();
pass();
}
int pass() // password area
{
char Username[]="a"; // admin Username
char password[]="a"; // admin password
char *user;
char *pass;
char choose;
int ctr;
clrscr();
valid:
{
clrscr();
gotoxy(30,10); printf("Username : ");
gets(user); //username
gotoxy(30,12); printf("Password : "); //password
strcpy(pass,get_pass(11));
while(choose !='N')
if((strcmpi(user,Username)==0) && (strcmpi(pass,password)==0))
{
gotoxy(30,16); printf("Logging in");
delay(1000);
gotoxy(41,16); printf(".");
delay(1000);
gotoxy(42,16); printf(".");
delay(1000);
gotoxy(43,16); printf(".");
delay(1000);
clrscr();
mainmenu();
getch();
break;
}
else
{
clrscr();
gotoxy(30,13); printf("invalid!");
gotoxy(30,14); printf("try again [Y/N]");
choose=toupper(getch());
if (choose=='Y')
goto valid;
else
printf("goodbye!");
getch();
}
}
return 0;
}
void mainmenu()
{
int choice, code;
clrscr();
do
{
gotoxy(30,5); printf("INDEX Co. INVENTORY");
gotoxy(30,10); printf("[1] Sales");
gotoxy(30,12); printf("[2] Add");
gotoxy(30,14); printf("[3] Search");
gotoxy(30,16); printf("[4] Delete");
gotoxy(30,18); printf("[5] Report");
gotoxy(30,20); printf("[6] Exit");
gotoxy(30,22); printf("Option: ");
gotoxy(37,22);scanf("%d", &choice);
switch(choice)
{
case 1:
clrscr();
transaction();
break;
case 2:
clrscr();
add();
break;
case 3:
clrscr();
search();
break;
case 4:
clrscr();
deleted();
break;
case 5:
clrscr();
report();
break;
case 6:
clrscr();
gotoxy(40,15); printf("Goodbye!!!");
getch();
break;
default:
gotoxy(35,15); printf("Invalid Choice");
}
} while(choice != 6);
}
void transaction()
{
char code[5], desc[20], type[20];
float price, total;
char Target[4];
int Found=0, qty;
FILE *sfile;
gotoxy(40,15); printf("Search");
clrscr();
if((sfile=fopen("c:\\sales.txt","a+")) == NULL)
printf("File Empty");
else
{
gotoxy(30,5);printf("SALES");
gotoxy(30,9);printf("Enter Product Code: ");
fflush(stdin);
gets(Target);
while(!feof(sfile))
{
fscanf(sfile,"%s %s %s %f",code, desc, type, &price);
if(strcmp(Target, code) == 0) Found = 1;
}
if(Found)
{
gotoxy(30,10);printf("\n\nFILE FOUND\n");
gotoxy(30,11);printf("================================\n");
gotoxy(30,12); printf("Product Code: %s\n", code);
gotoxy(30,14); printf("Description : %s\n", desc);
gotoxy(30,16); printf("Type : %s\n", type);
gotoxy(30,18); printf("Price : %.2f\n", price);
gotoxy(30,20); printf("Qty : ", qty);
scanf("%d", &qty);
total = price * qty;
gotoxy(30,22); printf("Price : %.2f\n", total);
fclose(sfile);
sfile=fopen("c:\\sales.txt","a+");
fprintf(sfile,"%s %s %s %.2f %d %f\n", code, desc, type, price, qty, total);
}
else
printf("\n\nFile not found");
getch();
}
printf("\n\nPress Enter to continue");
clrscr();
fclose(sfile);
main();
}
void report()
{
char code[4], desc[20], type[20];
float price, total;
int ctr=0, gtotal=0, qty;
FILE *sfile;
gotoxy(40,15); printf("Search");
clrscr();
if((sfile=fopen("c:\\sales.txt","a+")) == NULL)
printf("File Empty");
else
{
while(!feof(sfile))
{
fflush(stdin);
fscanf(sfile,"%s %s %s %f %d %f\n",code, desc, type, &price, &qty, &total);
clrscr();
gotoxy(34,1); printf("Sales Report");
gotoxy(2, 3); printf("Code\tDescription\tType\tPrice \tQty \tSales");
gotoxy(10,3+ctr); printf("%s", code);
gotoxy(20,3+ctr); printf("%s", desc);
gotoxy(32,3+ctr);printf("%s", type);
gotoxy(42,3+ctr);printf("%.2f", price);
gotoxy(52,3+ctr);printf("%d", qty);
gotoxy(62,3+ctr);printf("%.2f", total);
ctr++;
gtotal += total;
}
gotoxy(24,23); printf("Grand Total: %.2f", gtotal);
}
fclose(sfile);
printf("\n\nPress Enter to continue");
getch();
clrscr();
}
void add()
{
gotoxy(40,15); printf("ADD");
clrscr();
char code[4], desc[20], type[20];
float price;
FILE *sfile;
sfile=fopen("c:\\sales.txt","a+");
fflush(stdin);
gotoxy(30,10);printf("Product Code: "); gets(code);
gotoxy(30,12);printf("Description : "); gets(desc);
gotoxy(30,14);printf("Type : "); gets(type);
gotoxy(30,16);printf("Price : "); scanf("%f", &price);
gotoxy(30,20);printf("\nRecord Saved!");
fprintf(sfile,"%s %s %s %.2f\n", code, desc, type, price);
fclose(sfile);
getch();
clrscr();
}
void search()
{
char code[4], desc[20], type[ 20];
float price;
char Target[4];
int Found=0;
FILE *sfile;
gotoxy(40,15); printf("Search");
clrscr();
if((sfile=fopen("c:\\sales.txt","r")) == NULL)
printf("File Empty");
else
{
gotoxy(30,10);printf("Enter Product Code: ");
fflush(stdin);
gets(Target);
while(!feof(sfile))
{
fscanf(sfile,"%s %s %s %f",code, desc, type, &price);
if(strcmp(Target, code) == 0) Found = 1;
}
if(Found)
{
gotoxy(30,12); printf("\n\nFILE FOUND\n");
gotoxy(30,14); printf("================================\n");
gotoxy(30,16); printf("Product Code: %s\n", code);
gotoxy(30,18); printf("Description : %s\n", desc);
gotoxy(30,20); printf("Type : %s\n", type);
gotoxy(30,22); printf("Price : %.2f\n", price);
}
else
printf("\n\nFile not found");
}
fclose(sfile);
printf("\n\nPress Enter to continue");
getch();
mainmenu();
}
void deleted()
{
char code[4], desc[20], type[20];
float price;
char Target[4];
int Found=0;
FILE *sfile, *temp;
gotoxy(40,15); printf("Delete");
clrscr();
temp=fopen("c:\\temp.txt","w");
if((sfile=fopen("c:\\sales.txt","a+")) == NULL)
printf("File Empty");
else
{
printf("Enter Product Code: ");
fflush(stdin);
gets(Target);
while(!feof(sfile))
{
fscanf(sfile,"%s %s %s %f",code, desc, type, &price);
if(feof(sfile)) break;
if(strcmp(Target, code) != 0)
fprintf(temp, "%s %s %s %f\n", code, desc,type, &price);
else
{
Found = 1;
gotoxy(30,10);printf("\n\nFILE FOUND\n");
gotoxy(30,12);printf("================================\n");
gotoxy(30,14);printf("Product Code: %s\n", code);
gotoxy(30,16);printf("Description : %s\n", desc);
gotoxy(30,18);printf("Type : %s\n", type);
gotoxy(30,20);printf("Price : %.2f\n", price);
}
}
}
if(!Found)
printf("Record not found\n");
else
{
gotoxy(30,27); printf("\nRecord Deleted");
fclose(sfile);
fclose(temp);
remove("c:\\sales.txt");
rename("c:\\temp.txt","c:\\sales.txt");
}
getch();
clrscr();
}
char *get_pass(size_t size) // to hide password
{
char input,str[50];
int ctr=0;
while(1) //while not pressing enter
{
input = getch();
if( input == '\b' && ctr > 0 )
{
putch( input );
putch(' ');
putch( input );
ctr--;
}
else if( input !='\r' && ( isalpha( input ) || isspace( input ) ) && ctr != size )
{
str[ ctr ] = input;
ctr++;
putch( '#' );
}
else if(input=='\r'&& ctr>0)
{
break;
}
}
str[ctr]='\0';
globalStr=str;
return globalStr;
}