code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//**************************************
//
// Name: Linked lists, almost working fi
// le i/o,weak sort
// Description:it takes some information
// the user enters into a linked list, and
// saves it to a file, Dynamically allocate
// s memory, can sort stuff out what not, o
// pening a file doesnt work for some odd r
// eason, and the sort is extremely weak
// By: Marc
//
// Side Effects:File i/o doenst work, so
// rt could be written better, no real erro
// r checking done with the inputs..
//
//This code is copyrighted and has// limited warranties.Please see http://
// www.1CPlusPlusStreet.com/xq/ASP/txtCodeI
// d.11/lngWId.3/qx/vb/scripts/ShowCode.htm//for details.//**************************************
//
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Accounts Receivable with File I/O
Built and compiled on Borland Turbo C++ 4.5 doesnt compile under does do to some
pointer error.
That virtually says it all i guess, uses linked lists file i/o has a problem though
cant figure out why. the sort is extremely weak but works.
Marc@kellyblue.com
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#include
#include
#include
#include
#define MAXNAME 41
void printmenu(void); //prints menu out
void addcompany(struct company **); //adds company
void delcompany(struct company **); //deletes company
void addendinfo(struct company **); //edit company info
void sortname(struct company **);//sort by company name
void sortbalance(struct company **); //sort by balance
void displayall(struct company **); //print all *is it sorted*?
void displaypastdue(struct company **); //prints past due amounts..
void save(struct company **);//saves information
void open(struct company **);//opens
void displaycustomer(struct company *);
void swap(struct company*,struct company *);
void freeall(struct company *);
struct company
{
char company[MAXNAME];
char contact[MAXNAME];
char address1[MAXNAME];
char address2[MAXNAME];
char city[24];
char state[4];
char zip[7];
char phone[11];
char fax[11];
float balance;
float pastdue;
struct company *nextcomp;
};
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Main
runs the menu
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void main (void)
{
struct company *firstcomp=NULL;
char ch;
do
{
flushall();
printmenu();
ch = getch();
switch (ch)
{
case '1': addcompany(&firstcomp); break;
case '2': delcompany(&firstcomp); break;
case '3': addendinfo(&firstcomp); break;
case '4': sortname(&firstcomp);break;
case '5': sortbalance(&firstcomp);break;
case '6': displayall(&firstcomp); break;
case '7': displaypastdue(&firstcomp); break;
case '8': save(&firstcomp); break;
case '9': open(&firstcomp);break;
case 0x1b: break;
default: break;
}
}
while (ch!=0x1b);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Print Menu
this will print the menu of choices for the user
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void printmenu(void)
{
clrscr();
printf ("1. Add a company\n");
printf ("2. delete a company\n");
printf ("3. Edit company info\n");
printf ("4. Sort by company name\n");
printf ("5. Sort by balance\n");
printf ("6. Display all info\n");
printf ("7. Display past due amounts\n");
printf ("8. Save the information\n");
printf ("9. Open the file\n");
printf ("ESC Exit the program\n");
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Add Company
Tried to do a bit of formatted input here, could be better if spent more time on it
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void addcompany(struct company **firstcomp)
{
clrscr();
struct company *thiscomp;
thiscomp = (company *) malloc(sizeof(struct company));
if (thiscomp == NULL)
{
printf("Memory Error");
getch();
return;
}
thiscomp->nextcomp = *firstcomp;
*firstcomp = thiscomp;
flushall();
gotoxy (2,2);
printf ("Company name: ");
gotoxy(35,2);
printf("Contact name: ");
gotoxy(2,3);
printf("Address Line 1: ");
gotoxy(2,4);
printf("Address Line 2: ");
gotoxy(2,5);
printf ("City: State:Zip: ");
gotoxy(2,6);
printf("Phone Number: ");
gotoxy(30,6);
printf("Fax Number: ");
gotoxy(2,7);
printf("Current Balance: ");
gotoxy(2,8);
printf("Balance Past Due: ");
gotoxy(16,2);
gets(thiscomp->company);
gotoxy(49,2);
gets(thiscomp->contact);
gotoxy(18,3);
gets(thiscomp->address1);
gotoxy(18,4);
gets(thiscomp->address2);
gotoxy(8,5);
gets(thiscomp->city);
gotoxy(31,5);
gets(thiscomp->state);
gotoxy(41,5);
gets(thiscomp->zip);
gotoxy(16,6);
gets(thiscomp->phone);
gotoxy(42,6);
gets(thiscomp->fax);
gotoxy(19,7);
scanf("%f",&thiscomp->balance);
gotoxy(20,8);
scanf("%f",&thiscomp->pastdue);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: delete a company
Deletes typed in company, as of right now has to be exact match
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void delcompany(struct company **firstcomp)
{
struct company *thiscomp;
struct company *lastcomp;
char deleting[MAXNAME];
flushall();
if(firstcomp == NULL)
{
printf("No one on list");
return;
}
printf("Enter a company name to delete: ");
gets(deleting);
thiscomp = *firstcomp;
do
{
if(strcmp(thiscomp->company,deleting)==0)
{
if (thiscomp == *firstcomp)
*firstcomp = thiscomp->nextcomp;
else
lastcomp->nextcomp = thiscomp->nextcomp;
free(thiscomp); //frees that particular memory
return;
}
lastcomp = thiscomp;
thiscomp = thiscomp->nextcomp;
}
while(thiscomp!=NULL);
printf("No such name on list\n");
getch();
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Edit Company information
this will run the search part to find the part customer u want to edit..
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void addendinfo(struct company **firstcomp)
{
struct company *thiscomp;
// struct company *lastcomp;
char search[MAXNAME];
if (firstcomp == NULL)
{
printf("No company to edit");
return;
}
printf ("Which company would you like to edit: ");
gets(search);
thiscomp = *firstcomp;
do
{
if(strcmp(thiscomp->company,search)==0)
{
displaycustomer(thiscomp);
}
thiscomp=thiscomp->nextcomp;
}
while(thiscomp!=NULL);
printf("No such company");
getch();
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Display One Customer
this will print one customer out that is used if you want to modify that particular
customer.. then it will ask for inputs.. just like add customer
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void displaycustomer(struct company *thiscomp)
{
clrscr();
gotoxy (2,2);
printf ("Company name: ");
gotoxy(35,2);
printf("Contact name: ");
gotoxy(2,3);
printf("Address Line 1: ");
gotoxy(2,4);
printf("Address Line 2: ");
gotoxy(2,5);
printf ("City: State:Zip: ");
gotoxy(2,6);
printf("Phone Number: ");
gotoxy(30,6);
printf("Fax Number: ");
gotoxy(2,7);
printf("Current Balance: ");
gotoxy(2,8);
printf("Balance Past Due: ");
printf ("\n\n\n");
printf("Company: %s",thiscomp->company);
printf("Contact: %s",thiscomp->contact);
printf("\nAddress line 1: %s",thiscomp->address1);
printf("\nAddress line 2: %s",thiscomp->address2);
printf("\nCity: %sState: %s Zip: %s ",
thiscomp->city,thiscomp->state,thiscomp->zip);
printf("\nPhone: %s",thiscomp->phone);
printf("Fax: %s",thiscomp->fax);
printf("\nCurrent balance: %.2f",thiscomp->balance);
printf(" Balance past due: %.2f",thiscomp->pastdue);
gotoxy(16,2);
gets(thiscomp->company);
gotoxy(49,2);
gets(thiscomp->contact);
gotoxy(18,3);
gets(thiscomp->address1);
gotoxy(18,4);
gets(thiscomp->address2);
gotoxy(8,5);
gets(thiscomp->city);
gotoxy(31,5);
gets(thiscomp->state);
gotoxy(40,5);
gets(thiscomp->zip);
gotoxy(16,6);
gets(thiscomp->phone);
gotoxy(42,6);
gets(thiscomp->fax);
gotoxy(19,7);
scanf("%f",&thiscomp->balance);
gotoxy(20,8);
scanf("%f",&thiscomp->pastdue);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Display Everything
this prints everything.. if it hasnt been sorted, then its not in sorted order
if it has been sorted in any way shape or form then well its sorted.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void displayall(struct company **firstcomp)
{
struct company *thiscomp;
clrscr();
if (*firstcomp == NULL)
{
printf("There arent any companies entered yet.. try adding some");
getch();
return;
}
thiscomp=*firstcomp;
do
{
printf("Company: %s",thiscomp->company);
printf("Contact: %s",thiscomp->contact);
printf("\nAddress line 1: %s",thiscomp->address1);
printf("\nAddress line 2: %s",thiscomp->address2);
printf("\nCity: %sState: %s Zip: %s ",
thiscomp->city,thiscomp->state,thiscomp->zip);
printf("\nPhone: %s",thiscomp->phone);
printf(" Fax: %s",thiscomp->fax);
printf("\nCurrent balance: %.2f",thiscomp->balance);
printf(" Balance past due: %.2f",thiscomp->pastdue);
printf("\n\n");
thiscomp=thiscomp->nextcomp;
getch();
}
while(thiscomp != NULL);
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Display Just those with past due balances
this function displays anyone with a pastdue > 0 which is what means to owe money cause most people dont owe -$.. would be nice though
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void displaypastdue(struct company **firstcomp)
{
struct company *thiscomp;
clrscr();
if (firstcomp == NULL)
{
printf("empty list");
return;
}
thiscomp=*firstcomp;
do
{
if (thiscomp->pastdue > 0)
{
printf("Company: %s\n",thiscomp->company);
printf("Amount due: %.2f\n",thiscomp->pastdue);
}
thiscomp=thiscomp->nextcomp;
}
while(thiscomp!=NULL);
getch();
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Sort by Company Name
this function sorts companies by their names..hence the name of the function..
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void sortname(struct company **firstcomp)
{
struct company *thiscomp;
struct company *newcomp;
clrscr();
if (*firstcomp == NULL)
{
printf("empty list");
return;
}
thiscomp=*firstcomp;
newcomp=*firstcomp;
do
{
while (thiscomp!=NULL)
{
if (strcmp(thiscomp->company,newcomp->company)>0)
swap(thiscomp,newcomp);
thiscomp=thiscomp->nextcomp;
}
newcomp=newcomp->nextcomp;
thiscomp=*firstcomp;
}
while (newcomp != NULL);
printf("\n\n\ndid it sort this time?");
getch();
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Sort by Company Balance
this function runs the loop to sort the lists of companies by total balances.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void sortbalance(struct company **firstcomp)
{
struct company *thiscomp;
struct company *newcomp;
clrscr();
if (*firstcomp == NULL)
{
printf("empty list");
return;
}
thiscomp=*firstcomp;
newcomp=*firstcomp;
do
{
while (thiscomp!=NULL)
{
if ((thiscomp->balance+thiscomp->pastdue) < (newcomp->balance+newcomp->pastdue))
swap(thiscomp,newcomp);
thiscomp=thiscomp->nextcomp;
}
newcomp=newcomp->nextcomp;
thiscomp=*firstcomp;
}
while (newcomp != NULL);
printf("\n\n\ndid it sort this time?");
getch();
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Function: Swap Company Records
this is where all the actual swapping of information is done, theres gotta be an
easier way of doing it if anyone wants to let me know please!!
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void swap(struct company *thiscomp,struct company *newcomp)
{
char temp[MAXNAME];
float tempbal;
//company
strcpy(temp,newcomp->company);
strcpy(newcomp->company,thiscomp->company);
strcpy(thiscomp->company,temp);
//contact
strcpy(temp,newcomp->contact);
strcpy(newcomp->contact,thiscomp->contact);
strcpy(thiscomp->contact,temp);
//address1
strcpy(temp,newcomp->address1);
strcpy(newcomp->address1,thiscomp->address1);
strcpy(thiscomp->address1,temp);
//address2
strcpy(temp,newcomp->address2);
strcpy(newcomp->address2,thiscomp->address2);
strcpy(thiscomp->address2,temp);
//city
strcpy(temp,newcomp->city);
strcpy(newcomp->city,thiscomp->city);
strcpy(thiscomp->city,temp);
//state
strcpy(temp,newcomp->state);
strcpy(newcomp->state,thiscomp->state);
strcpy(thiscomp->state,temp);
//zip
strcpy(temp,newcomp->zip);
strcpy(newcomp->zip,thiscomp->zip);
strcpy(thiscomp->zip,temp);
//phone
strcpy(temp,newcomp->phone);
strcpy(newcomp->phone,thiscomp->phone);
strcpy(thiscomp->phone,temp);
//fax
strcpy(temp,newcomp->fax);
strcpy(newcomp->fax,thiscomp->fax);
strcpy(thiscomp->fax,temp);
//balance
tempbal = newcomp->balance;
newcomp->balance = thiscomp->balance;
&nbs