#include #include #include #include void clearInput(void); void addNewAccount(void); void listAll(void); void deleteAccount(void); void modifyAccount(void); int prompt(void); struct account { int number; char lastname[15]; char firstname[15]; float balance; struct account *next; }; struct account *firsta,*currenta,*newa; int anum = 0; int main() { char ch; firsta = NULL; do { clearInput(); puts("\nA - Add a new account"); puts("D - Delete account"); puts("L - List all accounts"); puts("M - Modify account"); puts("Q - Quit this program\n"); printf("\tYour choice:"); ch = getchar(); ch = toupper(ch); switch(ch) { case 'A': puts("Add new account\n"); clearInput(); addNewAccount(); break; case 'D': puts("Delete account\n"); deleteAccount(); break; case 'L': puts("List all accounts\n"); listAll(); break; case 'M': puts("Modify an account\n"); modifyAccount(); break; case 'Q': puts("Quit\n"); default: break; } } while(ch != 'Q'); return(0); } /* * This function clears any text from the input stream */ void clearInput(void) { fflush(stdin); /* fpurge(stdin) */ } void addNewAccount(void) { newa = (struct account *)malloc(sizeof(struct account)); /* * Check to see if this is the first record * If so, then initialize all the pointers to this, * first structure in the database */ if(firsta==NULL) firsta = currenta = newa; /* * Otherwise, you must find the end of the structure list * (Easily spotted by the NULL pointer) and add on the * new structure you just allocated memory for */ else { currenta = firsta; /* make the first record the current one */ /* and loop through all records: */ while(currenta->next != NULL) currenta = currenta->next; /* the last record is found */ currenta->next = newa; /* save the address of new */ currenta = newa; /* make current record the new one */ } /* Now, you just fill in the new structure */ anum++; printf("%27s: %5i\n","Account number",anum); currenta->number = anum; printf("%27s: ","Enter customer's last name"); gets(currenta->lastname); printf("%27s: ","Enter customer's first name"); gets(currenta->firstname); printf("%27s: $","Enter account balance"); scanf("%f",¤ta->balance); /* * Finally, cap the new record with a NULL pointer * so that you know it's the last record: */ currenta->next = NULL; } void listAll(void) { if(firsta==NULL) puts("There are no records to print!"); else { printf("%6s %-15s %-15s %11s\n","Acct#","Last","First","Balance"); currenta=firsta; do { printf("%5d: %-15s %-15s $%8.2f\n", currenta->number, currenta->lastname, currenta->firstname, currenta->balance); } while((currenta=currenta->next) != NULL); } } void deleteAccount(void) { int record; struct account *previousa; if(firsta==NULL) { puts("There are no records to delete!"); return; } listAll(); /* show all records first */ printf("Enter account number to delete: "); scanf("%d",&record); currenta = firsta; while(currenta != NULL) { if(currenta->number == record) { if(currenta == firsta) /* special condition */ firsta=currenta->next; else previousa->next = currenta->next; free(currenta); printf("Acount %d deleted!\n",record); return; } else { previousa = currenta; currenta = currenta->next; } } printf("Account %d was not found!\n",record); puts("Nothing deleted."); } void modifyAccount(void) { int record; if(firsta==NULL) { puts("There are no records to modify!"); return; } listAll(); /* show all records first */ printf("Enter account number to modify or change: "); scanf("%d",&record); currenta = firsta; while(currenta != NULL) { if(currenta->number == record) { printf("Account $%d:\n",currenta->number); printf("Last name: %s\n",currenta->lastname); if(prompt()) gets(currenta->lastname); printf("First name: %s\n",currenta->firstname); if(prompt()) gets(currenta->firstname); printf("Balance %8.2f\n",currenta->balance); if(prompt()) scanf("%f",¤ta->balance); return; } else { currenta = currenta->next; } } printf("Account %d was not found!\n",record); } int prompt(void) { char ch; clearInput(); printf("Update?"); ch = getchar(); ch = toupper(ch); clearInput(); if(ch == 'Y') { printf("Enter new: "); return(1); } else return(0); }