Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef MAIN_H_INCLUDED
- #define MAIN_H_INCLUDED
- void clearInput();
- void addNewAccount();
- char* myGets();
- void listAll();
- int promptForDelete();
- void loadRecordsFromFile();
- void saveRecordsToFile();
- void promptAndStoreRecordInMemory();
- struct account {
- int number;
- char lastname[15];
- char firstname[15];
- float balance;
- struct account *next; /*This is the "Link" to the next list record/node*/
- };
- struct account *firsta, *currenta, *newa;
- #endif // MAIN_H_INCLUDED
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "deleteARecord.h"
- #include "modifyARecord.h"
- #include "main.h"
- int anum = 0;
- /*PROGRAM ENTRY POINT*/
- int main() {
- char ch;
- firsta = NULL;
- loadRecordsFromFile();
- do {
- clearInput();
- puts("A - Add a new account");
- puts("D - Delete a record");
- puts("M - Modify a Record Account #");
- puts("L - List all records");
- puts("Q - Save Records and Quit this programn");
- printf("tYour choice:");
- ch = getchar();
- clearInput();
- ch = toupper(ch);
- switch (ch) {
- case 'A':
- puts("Add new accountn");
- printf("Original Input: %c", ch);
- addNewAccount();
- break;
- case 'D':
- puts("Delete a recordn");
- deleteARecord();
- break;
- case 'L':
- puts("List All Accountsn");
- listAll();
- break;
- case 'M':
- puts("Modify a Recordn");
- modifyARecord();
- break;
- case 'Q':
- puts("Quitn");
- break;
- case 'n':
- ch = getchar();
- default:
- break;
- }
- } while (ch != 'Q');
- saveRecordsToFile();
- if (newa != NULL){
- free(newa);
- printf("n Memory freed!");
- }
- return(0);
- }
- void saveRecordsToFile(){
- FILE *f;
- currenta = firsta;
- if(currenta == NULL)
- return;
- f = fopen("record.tdb","w");
- if(f == NULL)
- {
- printf("Error writing to file!n");
- return;
- }
- while(currenta != NULL)
- {
- fwrite(currenta,sizeof(struct account),1,f);
- currenta = currenta->next;
- }
- fclose(f);
- printf("Data saved to disk!n");
- return;
- }
- void loadRecordsFromFile(){
- FILE *f;
- f = fopen("record.tdb","r");
- if(f == NULL){
- printf("Error opening file or file does not exist yet...");
- return;
- }
- firsta = malloc(sizeof(struct account));
- currenta = firsta;
- while(1){
- newa = (struct account *)malloc(sizeof(struct account));
- /*This line reads each struct from the file and stores in currenta*/
- fread(currenta,sizeof(struct account),1,f);
- /*If we're at the last record, break out of the loop.*/
- if(currenta->next == NULL)
- break;
- /*Otherwise, continue to the next iteration/link in file*/
- currenta->next = newa;
- currenta = newa;
- }
- fclose(f);
- anum = currenta->number;
- }
- void clearInput(void)
- {
- fflush(stdin);
- }
- void listAll(void){
- unsigned int counter = 0;
- if(firsta != NULL)
- currenta = firsta;
- else{
- printf("There are no items in the list.n");
- return;
- }
- while(currenta != NULL){
- printf("----------------------------n");
- printf("ID#:%dn",currenta->number);
- printf("Last Name: %sn",currenta->lastname);
- printf("First Name: %sn", currenta->firstname);
- printf("Balance: %.2fn", currenta->balance);
- counter++;
- currenta = currenta->next;
- }
- }
- //////////////////////////////////////////////////////////////
- void addNewAccount(void) {
- newa = malloc(sizeof(struct account));
- /*If this is the first record, initialize all pointers to this record*/
- if (firsta == NULL)
- firsta = currenta = newa;
- /*Otherwise, find the end (NULL ptr) and insert there*/
- else {
- currenta = firsta; /*Select first record then loop thru all records til end*/
- while (currenta->next != NULL)
- currenta = currenta->next;
- /*Last record found*/
- currenta->next = newa; /*assign the last record's NEXT ptr to our new record to be inserted*/
- currenta = newa; /*switch to the newly inserted record and make it currently selected*/
- }
- /*Fill in the new record with data*/
- anum++;
- promptAndStoreRecordInMemory();
- }
- //////////////////////////////////////////////////////////////
- void promptAndStoreRecordInMemory()
- {
- printf("%27s: %5in", "Account number", anum);
- currenta->number = anum;
- clearInput();
- printf("%27s: ", "Enter customer's last name");
- gets(currenta->lastname);
- //getchar(); //Clear buffer
- printf("The lastname is: "%s"", currenta->lastname);
- printf("n");
- printf("%27s ", "Enter customer's first name");
- gets(currenta->firstname);
- //getchar(); //Clear buffer
- printf("n");
- printf("%27s: $", "Enter account balance");
- scanf("%f", ¤ta->balance);
- currenta->next = NULL;
- return;
- }
- //////////////////////////////////////////////////////////////
- int promptForDelete(void){
- int numberToDelete;
- char shouldContinue;
- do{
- printf("Enter an ID number of the record you want to delete: ");
- scanf("%d",&numberToDelete);
- printf("You entered %d, is this correct? (y/n)",numberToDelete);
- getchar();
- shouldContinue = getchar();
- shouldContinue = toupper(shouldContinue);
- /*TODO: Refactor*/
- switch(shouldContinue){
- case 'Y':
- return numberToDelete;
- default:
- break;
- }
- }while(shouldContinue != 'Y');
- return -1;
- }
- #ifndef DELETEARECORD_H_INCLUDED
- #define DELETEARECORD_H_INCLUDED
- void deleteARecord(void);
- #endif // DELETEARECORD_H_INCLUDED
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "deleteARecord.h"
- #include "main.h"
- void deleteARecord(){
- int numberToDelete;
- struct account *previousa;
- numberToDelete = promptForDelete();
- if(numberToDelete == -1)
- {
- printf("No items to delete - (-1 return)n");
- return;
- }
- if (firsta == NULL)
- {
- printf("There are no items to delete!");
- return;
- }
- else
- {
- currenta = firsta;
- while(currenta != NULL)
- {
- if(currenta->number == numberToDelete)
- {
- if(currenta == firsta) /*Special case for first record because it has no previous record*/
- firsta = currenta->next;
- else
- previousa->next = currenta->next;
- free(currenta);
- printf("Account #%d deleted.n", numberToDelete);
- listAll();
- return;
- }
- else
- {
- previousa = currenta;
- currenta = currenta->next;
- }
- }
- printf("Account #%d was not found.n",numberToDelete);
- puts("Nothing deleted");
- }
- }
- #ifndef MODIFYARECORD_H_INCLUDED
- #define MODIFYARECORD_H_INCLUDED
- void modifyARecord(void);
- #endif // MODIFYARECORD_H_INCLUDED
- #include <stdio.h>
- #include "modifyARecord.h"
- #define TRUE 1
- #define FALSE 0
- #include "main.h"
- /*This struct serves as an "object" to hold the numbers and a bool flag*/
- struct accountNumbers{
- int numberToModify;
- int newNumber;
- unsigned int success;
- };
- struct accountNumbers *promptForModify(struct accountNumbers *accountNumbersPtr);
- /**
- *
- *
- * FUNCTION MODIFIES THE RECORD'S ID NUMBER
- *
- *
- */
- void modifyARecord()
- {
- struct account *previousa;
- struct accountNumbers *numbersPtr;
- struct accountNumbers theNumbers;
- theNumbers.newNumber = 0;
- theNumbers.numberToModify =0;
- theNumbers.success = FALSE;
- numbersPtr = &theNumbers;
- promptForModify(numbersPtr);
- if(numbersPtr->success == FALSE)
- {
- printf("CANCELLING...");
- }
- if (firsta == NULL)
- {
- printf("There are no items to modify!");
- return;
- }
- else
- {
- currenta = firsta;
- while(currenta != NULL)
- {
- if(currenta->number == numbersPtr->numberToModify)
- {
- currenta->number = numbersPtr->newNumber;
- printf("Account #%d updated.n", numbersPtr->numberToModify);
- listAll();
- return;
- }
- else
- {
- previousa = currenta;
- currenta = currenta->next;
- }
- }
- printf("Account #%d was not found.n",numbersPtr->numberToModify);
- puts("Nothing deleted");
- }
- }
- /**
- *
- *
- * FUNCTION PROMPTS THE USER FOR THE NUMBER TO MODIFY AND THE NUMBER TO REPLACE WITH
- *
- *
- */
- struct accountNumbers* promptForModify(struct accountNumbers *accountNumbersPtr)
- {
- printf("Enter an ID number of the record you want to modify: ");
- scanf("%d",&accountNumbersPtr->numberToModify);
- printf("You entered %d, now enter the replacement number OR 0 to go back.",accountNumbersPtr->numberToModify);
- getchar();
- scanf("%i",&accountNumbersPtr->newNumber);
- switch(accountNumbersPtr->newNumber)
- {
- case 0:
- accountNumbersPtr->success = FALSE;
- return accountNumbersPtr;
- break; //Not reachable but for semantics
- default:
- accountNumbersPtr->success = TRUE;
- return accountNumbersPtr;
- break; //Not reachable but for semantics
- }
- }
- switch(accountNumbersPtr->newNumber)
- {
- case 0:
- accountNumbersPtr->success = FALSE;
- return accountNumbersPtr;
- break; //Not reachable but for semantics
- default:
- accountNumbersPtr->success = TRUE;
- return accountNumbersPtr;
- break; //Not reachable but for semantics
- }
- break; //Not reachable but for semantics
- newa = (struct account *)malloc(sizeof(struct account));
- newa = (struct account *)malloc(sizeof(struct account));
- /*This line reads each struct from the file and stores in currenta*/
- fread(currenta,sizeof(struct account),1,f);
- /*If we're at the last record, break out of the loop.*/
- if(currenta->next == NULL)
- break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement