Hello. This is my code:
What I have an issue with is loadFile. It doesn't load the file properly, as doesn't scan the itemName and begins with the first line of inputfile.txt. inputfile.txt reads as:Code:#include <stdio.h> #include <stdlib.h> /* Struct transaction – This struct should define an itemNumber, and an itemAmount that are ints, itemPrice which is a float, itemName[20] which is a char, and a next pointer of struct transaction. Typedef – you should use a typedef to rename the struct type to item. */ typedef struct transaction { int itemNumber; int itemAmount; float itemPrice; char itemName[20]; struct transaction *next; } item; typedef item *itemPtr; int printmenu(void); itemPtr loadFile(char *file); void insertNode(itemPtr head); int deleteNode(itemPtr head); void writeFile(char *fPtr, itemPtr head); void printList(itemPtr head); // Main – this function should read in 2 command line arguments. First is the input file’s name. The second is the output file’s name. It should then load the file and create the linked list. You should then start looping the menu which calls the other functions. int main (int argc, char *argv[]) { if (argc != 3) { printf("\nNot enough arguments.\n"); printf("Run format: ./a.out <inputfile.txt> <outputfile.txt>\n\n"); return 0; } itemPtr startPtr = loadFile(argv[1]); int menu = printmenu(); while (menu != 5) { switch (menu) { case 1: // add a transaction insertNode(startPtr); menu = printmenu(); break; case 2: // remove a transaction deleteNode(startPtr); menu = printmenu(); break; case 3: // print transaction list printList(startPtr); menu = printmenu(); break; case 4: // write transactions to file writeFile(argv[2], startPtr); menu = printmenu(); break; default: menu = printmenu(); } } if (menu = 5) { printf("\nProgram terminated.\n\n"); return 0; } } int printmenu(void) { int select; printf("\nSelect what you would like to do: \n"); printf("(1) Add a transaction\n"); printf("(2) Remove a transaction\n"); printf("(3) Print transaction list\n"); printf("(4) Write transactions to file\n"); printf("(5) Exit\n"); printf("\?: "); scanf("%d", &select); return select; } // Loadfile – this function will take in a pointer to a filename and will load the data from the file into a linked list. It will then return the head of the list. The file format should be as follows. The < > will not be present in the file. <Item number> <Item name> <amount of item> <price of item> itemPtr loadFile(char *file) { FILE *fPtr; itemPtr head = malloc(sizeof(item)); // head of linked list itemPtr newPtr = malloc(sizeof(item)); // pointer to new node itemPtr tail = malloc(sizeof(item)); // tail of linked list int i; int noinputs; fPtr = fopen(file, "r"); if (fPtr == NULL) { printf("\nUnable to open file.\n"); } fscanf(fPtr, "%d", &noinputs); for (i = 0; i < noinputs; i++) { if (i == 0) { head = newPtr; newPtr->next = NULL; tail = newPtr; } else { itemPtr newPtr = malloc(sizeof(item)); tail->next = newPtr; fscanf(fPtr, "%d %s %d %lf", &newPtr->itemNumber, newPtr->itemName, &newPtr->itemAmount, &newPtr->itemPrice); newPtr->next = NULL; tail = newPtr; } } rewind(fPtr); fclose(fPtr); return head; } // Insert Node – this function should prompt the user for all the inputs needed to add a node to the list, create a new node and attach it to the end of the linked list. It will take in a pointer to the front of the list. void insertNode(itemPtr head) { // Check this prototype. itemPtr newPtr = malloc(sizeof(item)); // pointer to new node itemPtr prevPtr = malloc(sizeof(item)); // pointer to previous node in list itemPtr currPtr = head; // pointer to current node in list printf("\nEnter the item number for the item: "); scanf("%d", &newPtr->itemNumber); printf("Enter the name for the item: "); scanf("%s", newPtr->itemName); printf("Enter the price for the item (no $):"); scanf("%lf", &newPtr->itemPrice); printf("Enter the quantity of the item: "); scanf("%d", &newPtr->itemAmount); newPtr->next = NULL; while (currPtr->next != NULL) { currPtr = currPtr->next; } if (currPtr->next == NULL) { currPtr->next = newPtr; } } // Delete Node – delete node will take in the head of the linked list. It will then prompt the user for an itemNumber to look for. If it is not found it will display a message to the user and return to the menu. If it finds it, it will remove the node from the list, keeping the rest of the list intact, and then frees the node. int deleteNode(itemPtr head) { // Check this prototype. int search; int found = 0; itemPtr prevPtr = malloc(sizeof(item)); // pointer to previous node in list itemPtr holdPtr = malloc(sizeof(item)); // pointer to new node itemPtr currPtr = head; // pointer to current node in list printf("Enter the transaction number you would like to delete: "); scanf("%d", &search); while (currPtr->next != NULL) { if (currPtr->itemNumber != search) { prevPtr = currPtr; currPtr = currPtr->next; } if (currPtr->itemNumber == search) { found = 1; prevPtr->next = currPtr->next; holdPtr = currPtr; free(holdPtr); return; } } if (currPtr->next == NULL && found == 0) { printf("Item not found!\n"); return; } } // Writefile – this function takes in a pointer to a filename for output filename, and the head of the linked list. It will then print all of the information out to the file and close the file. It should use the same format as Loadfile. void writeFile(char *file, itemPtr head) { FILE *fPtr; itemPtr currPtr = head; // pointer to current node in list // While currPtr->next != NULL, print the items from the linked list into output.txt. fPtr = fopen(file, "w+"); // Check this function. if (fPtr == NULL) { printf("\nUnable to open file.\n"); } while (currPtr->next != NULL) { fprintf(fPtr, "%d %s %d %lf", &currPtr->itemNumber, currPtr->itemName, &currPtr->itemAmount, &currPtr->itemPrice); fprintf(fPtr, "\n"); currPtr = currPtr->next; } rewind(fPtr); fclose(fPtr); } // printList – this functions takes in a pointer to the head of the list and prints all the information in the list out to the screen. This should use the same format as Loadfile. void printList(itemPtr head) { itemPtr currPtr = head; // pointer to current node in list printf("\nIn the format: <Item Number> <Item Name> <Quantity> <Price>\n\n"); // while not the end of the list while (currPtr->next != NULL) { printf("%d\t%s\t%d\t$%.2f\n", currPtr->itemNumber, currPtr->itemName, currPtr->itemAmount, currPtr->itemPrice); currPtr = currPtr->next; } }
Thanks for your help.Code:6 1 widgets 20 0.5 2 gadgets 250 0.25 3 whizbangs 1000 2.00 4 servos 100 3.75 5 pencils 5000 0.25 6 hamhawks 15 5.99