rendered paste body/* * File Name: Assignment.c * * Author: Your Name * Creation Date: Thursday, April 03 2008, 05:24 * Last Modified: Tuesday, May 06 2008, 15:11 * * File Description: * */#include <stdlib.h>#include <stdio.h>#include <string.h>typedef struct{ char * AgentName; char * PropertyAddress; int NoOfRooms; float Price; int Features;} Catalogue;void SearchMenu(){ printf("\n\n***************************************************\n"); printf("* Search Menu *\n"); printf("***************************************************\n"); printf("* *\n"); printf("* 1. Search by Property address. *\n"); printf("* *\n"); printf("* 2. Search by estate agent's first name. *\n"); printf("* *\n"); printf("* 3. Search by estate agent's last name. *\n"); printf("* *\n"); printf("* 4. Search by number of bedrooms. *\n"); printf("* *\n"); printf("* 5. Search by Price. *\n"); printf("* *\n"); printf("* 6. Search by Features. *\n"); printf("* *\n"); printf("* 7. Exit without Searching. *\n"); printf("* *\n"); printf("***************************************************\n\n");}void Quit(){ printf("\n\nThank you for running this program\n");}void SaveCatalogue(int Total, Catalogue *property, FILE *myfile){ int Iterator = 0; char *filename = (char*)malloc(sizeof(char) * 10); printf("\nWhat file would you like to Save to: "); scanf("%9s", filename); myfile = fopen(filename, "w+"); fprintf(myfile, "%d\n", Total); while(Iterator < Total) { fprintf(myfile, "%40s", (property[Iterator].PropertyAddress)); fprintf(myfile, "%20s", (property[Iterator].AgentName)); fprintf(myfile, "%d\n", (property[Iterator].NoOfRooms)); fprintf(myfile, "%lf\n", (property[Iterator].Price)); fprintf(myfile, "%d\n", (property[Iterator].Features)); Iterator++; } if(fclose(myfile) == 0) { printf("The file was written and closed succesfuly\n\n"); }}void SortDisplay(){} char LoadCatalogue(FILE *myfile, Catalogue *property){ int randomnum = 0; int Count = 0; char * temp = (char *) malloc(sizeof (char) * 20);; char * filename = (char *) malloc(sizeof (char) * 15); do { printf("\nWhat file would you like to load: "); scanf("%14s", filename); if((myfile = fopen(filename, "r")) == NULL) { printf("\nThe file failed to open, please cheak the name and that the file exsists and try again"); } } while(myfile == NULL); if(!feof(myfile)) { fscanf(myfile, "%d", &randomnum); fgets(temp, 19, myfile); } while(!feof(myfile)) { property[Count].PropertyAddress = (char *) malloc(sizeof (char) * 40); property[Count].AgentName = (char *) malloc(sizeof (char) * 20); fgets((property[Count].PropertyAddress), 40, myfile); printf("PropertyAddress: %s", property[Count].PropertyAddress); fgets((property[Count].AgentName), 20, myfile); printf("Agent Name: %s", property[Count].AgentName); fscanf(myfile, "%d", &(property[Count].NoOfRooms)); printf("NoOfRooms: %d\n", property[Count].NoOfRooms); fscanf(myfile, "%f\n", &(property[Count].Price)); printf("Price: %lf\n", property[Count].Price); fscanf(myfile, "%d\n", &(property[Count].Features)); printf("Features: %d\n", property[Count].Features); Count++; } printf("\nThe file was loaded successfully\n"); return Count;}int SearchCatalogue(int Count, Catalogue *property){ int DecimalValue = 0; int Selection = 0; int Iterator = 0; char *SearchString = (char *) malloc(40); int SearchInt = 0; do { SearchMenu(); printf("Enter Selection: "); scanf("%d", &Selection); if((Selection < 1) || (Selection > 7)) { printf("Your selection is invalid, please try again"); } } while((Selection < 0) || (Selection > 8)); switch(Selection) { case(1): printf("Enter Property Address: "); scanf("%40s", SearchString); do { if(strcmp(property[Iterator].PropertyAddress, SearchString) == 0) { return Iterator; } else { Iterator++; } } while(Iterator < Count); break; case(2): printf("Enter agent's first name to search by: "); scanf("%15s", SearchString); do { if(strcmp(property[Iterator].AgentName, SearchString) == 0) { return Iterator; } else { Iterator++; } } while(Iterator < Count); break; case(3): printf("Enter agent's last name to search by: "); scanf("%15s", SearchString); do { if(strcmp(property[Iterator].AgentName, SearchString) == 0) { return Iterator; } else { Iterator++; } } while(Iterator < Count); break; case(4): printf("Enter number of rooms to search by: "); scanf("%d", &SearchInt); do { if(property[Iterator].NoOfRooms == SearchInt) { return Iterator; } else { Iterator++; } } while(Iterator < Count); break; case(5): printf("Enter price to search by: "); scanf("%d", &SearchInt); do { if(property[Iterator].Price == SearchInt) { return Iterator; } else { Iterator++; } } while(Iterator < Count); break; case(6): DecimalValue = 0; SearchString = ""; printf("Are you looking for a property with a garage? (Y/N): "); scanf("%1s", SearchString); if((strncmp(SearchString, "y", 1) == 0) || (strncmp(SearchString, "Y", 1) == 0)) { DecimalValue = DecimalValue + 1; } SearchString = ""; printf("Are you looking for a property with an ensuite? (Y/N): "); if((strncmp(SearchString, "y", 1) == 0) || (strncmp(SearchString, "Y", 1) == 0)) { DecimalValue = DecimalValue + 2; } SearchString = ""; printf("Are you looking for a property with a dishwasher? (Y/N): "); scanf("%1s", SearchString); if((strncmp(SearchString, "y", 1) == 0) || (strncmp(SearchString, "Y", 1) == 0)) { DecimalValue = DecimalValue + 4; } SearchString = ""; printf("Are you looking for a property with an air condishiner? (Y/N): "); scanf("%1s", SearchString); if((strncmp(SearchString, "y", 1) == 0) || (strncmp(SearchString, "Y", 1) == 0)) { DecimalValue = DecimalValue + 8; } SearchString = ""; printf("Are you looking for a property with heating? (Y/N): "); scanf("%1s", SearchString); if((strncmp(SearchString, "y", 1) == 0) || (strncmp(SearchString, "Y", 1) == 0)) { DecimalValue = DecimalValue + 16; } SearchString = ""; printf("Are you looking for a property with a study? (Y/N): "); scanf("%1s", SearchString); if((strncmp(SearchString, "y", 1) == 0) || (strncmp(SearchString, "Y", 1) == 0)) { DecimalValue = DecimalValue + 32; } do { if(property[Iterator].Features == DecimalValue) { return Iterator; } else { Iterator++; } } while(Iterator < Count); break; case(7): printf("Now Exiting the Search Engine"); return Count; } return Iterator;}void Print(int Total, Catalogue *property){ int Count = 0; if(Total != 0) { while(Count < Total) { Count = SearchCatalogue(Total, property); printf("%s", property[Count].PropertyAddress); printf("%s", property[Count].AgentName); printf("%d\n", property[Count].NoOfRooms); printf("%lf\n", property[Count].Price); printf("%d\n", property[Count].Features); } }}void Delete(int Total, Catalogue *property){ int Count = 0; int Iterator = 0; printf("Total: %s", Total); if(Total != 0) { while(Count < Total) { Count = SearchCatalogue(Total, property); Iterator = Count; while(Iterator < (Total - 1)) { property[Iterator].PropertyAddress = property[Iterator + 1].PropertyAddress; property[Iterator].AgentName = property[Iterator + 1].AgentName; property[Iterator].NoOfRooms = property[Iterator + 1].NoOfRooms; property[Iterator].Price = property[Iterator + 1].Price; property[Iterator].Features = property[Iterator + 1].Features; Iterator++; } property[Iterator].PropertyAddress = ""; property[Iterator].AgentName = ""; property[Iterator].NoOfRooms = 0; property[Iterator].Price = 0; property[Iterator].Features = 0; } } Count--;}void DeleteMenu(){ printf("\n\n***************************************************\n"); printf("* Delete Menu *\n"); printf("***************************************************\n"); printf("* *\n"); printf("* 1. Specify the property address. *\n"); printf("* *\n"); printf("* 2. Specify real estate agent first name. *\n"); printf("* *\n"); printf("* 3. Specify real estate agent surname. *\n"); printf("* *\n"); printf("***************************************************\n\n");}void MainMenu(){ printf("\n\n***************************************************\n"); printf("* Menu *\n"); printf("***************************************************\n"); printf("* *\n"); printf("* 1. Load catalogue from file. *\n"); printf("* *\n"); printf("* 2. Delete property from catalogue. *\n"); printf("* *\n"); printf("* 3. Save catalogue to file. *\n"); printf("* *\n"); printf("* 4. Search property. *\n"); printf("* *\n"); printf("* 5. Sort and display catalogue. *\n"); printf("* *\n"); printf("* 6. Quit. *\n"); printf("* *\n"); printf("***************************************************\n");}int main( int argc, char *argv[] ){ Catalogue *property = malloc(sizeof(Catalogue) * 30); FILE *myfile = NULL; int NoOfProperties = 0; int Selection = 0; do { do { Selection = 0; MainMenu(); printf("\nPlease make your selection, it must be between 1 and 6: "); scanf("%d",&Selection); printf("You got here."); fflush(stdin); if((Selection < 1) || (Selection > 6)) { printf("\nYour input was not valid, please try again."); } } while((Selection < 1) || (Selection > 6)); switch(Selection) { case(1): NoOfProperties = LoadCatalogue(myfile, property); break; case(2): Delete(NoOfProperties, property); break; case(3): SaveCatalogue(NoOfProperties, property, myfile); break; case(4): Print(NoOfProperties, property); break; case(5): SortDisplay(); break; case(6): Quit(); break; } } while((Selection > 0) && (Selection < 6)); return 0;}