Person_Car

#include<stdio.h> #include<stdlib.h> #include<strings.h> // Car Struct struct Car { char brand[256]; char model[256]; int year; }; // Person Struct struct Person { char name[256]; struct Car car; }; // Global Array Declarations struct Person P[3]; // Person Array struct Car C[3]; // Car Array // MAIN int main() { int choice; int i; // Initializing Arrays for(i = 0; i <= 2; i++) { strcpy(P[i].name, ""); strcpy(C[i].brand, ""); } // ------------------- // Loop while(1) { printf("Press Any Key To Continue!\n"); getch(); system("cls"); // Menu printf("1.Add Person\n"); printf("2.Add Car\n"); printf("3.Assign Car to Person\n"); printf("4.Display\n"); printf("5.Exit\n"); printf("Enter Your Choice\n>"); fflush(stdin); scanf("%d", &choice); // Switch switch(choice) { case 1: // Add Person { system("cls"); if(EmptyCheck("Car") == 0) // If Car array empty { printf("Please Add at least one Car before adding a Person!\n"); break; } int empty_element = EmptyCheck("Person"); // Used to determine which element is empty in the Person Array char person_name[256]; // Name of the person if(empty_element <= 2) { addPerson(empty_element); } else { int index; char c; printf("Person Space Full!\n"); printf("Do you want to overwrite a person?(Y/N)"); c = getch(); if(c == 'Y' || c == 'y') { printf("Select the Person you want to overwrite\n"); Display("Person"); printf(">"); scanf("%d", &index); if(index > 2 || index < 0) { printf("Wrong Input!\n"); break; } else addPerson(index); } else if(c == 'N' || c == 'n') { break; } else { printf("Wrong Input!\n"); break; } } break; } case 2: // Add Car { char c; int index; if( EmptyCheck("Car") == 3) { printf("Car Space Full!\nDo you want to overwrite a car?(Y/N)\n"); c = getch(); if(c == 'Y' || c == 'y') { printf("Select the Car you want to overwrite\n"); Display("Car"); printf(">"); scanf("%d", &index); if(index > 2 || index < 0) { printf("Wrong Input!\n"); break; } else addCar(index); } else if(c == 'N' || c == 'n') { break; } else { printf("Wrong Input!\n"); break; } } else { index = EmptyCheck("Car"); addCar(index); } break; } case 3: // Assign Car to Person { if(EmptyCheck("Person") == 0) { printf("Please add at least 1 Person!\n"); break; } if(EmptyCheck("Car") == 0) { printf("Please add at least 1 Car!\n"); break; } else { char c; int index; printf("Select the Person\n"); Display("Person"); c = getch(); if(c >= '0' && c <= '2') { index = c - 48; assignCarToPerson(index); } } break; } case 4: // Display Car or Person { char c; system("cls"); printf("1.Display Person\n"); printf("2.Display Car\n"); c = getch(); if(c == '1') Display("Person"); else if(c == '2') Display("Car"); else printf("Wrong Input!\n"); break; } case 5: // Exit exit(0); default: // Wrong Input printf("Wrong Input!\n"); } } } // EmptyCheck Function to check which Array(Person or Car) or which Index of the Array is empty int EmptyCheck(char struct_to_check[10]) { if(strcmp(struct_to_check, "Person") == 0) { int i; for(i = 0; i <=2; i++) { if(strcmp(P[i].name, "") == 0) break; } // printf("pi = %d", i); return i; } else if((strcmp(struct_to_check,"Car") == 0) ) { int i; for(i = 0; i <= 2; i++) { if(strcmp(C[i].brand, "") == 0) break; } // printf("ci = %d", i); return i; } } // Add Person void addPerson(int index) { char name[256]; printf("Enter Person's Name\n"); fflush(stdin); gets(name); strcpy(P[index].name, name); assignCarToPerson(index); } // Assign Car to Person void assignCarToPerson(int index) { char choice; int car_index; printf("Which Car do you want to assign?\n"); Display("Car"); choice = getch(); if(choice > '2' && choice < '0') printf("Wrong Input!\n"); else { car_index = choice - 48; P[index].car = C[car_index]; } return; } // Add Car void addCar(int index) { char brand[256], model[256]; int year; printf("Enter Brand\n"); fflush(stdin); gets(brand); printf("Enter Model\n"); fflush(stdin); gets(model); printf("Enter Year\n"); fflush(stdin); scanf("%d", &year); strcpy(C[index].brand, brand); strcpy(C[index].model, model); C[index].year = year; } void Display(char struct_to_display[10]) { if(strcmp(struct_to_display, "Person") == 0) { int i; if(EmptyCheck("Person") == 0) { printf("Nothing to Display!\nAdd a Person First!\n"); return; } for(i = 0; i <= 2; i++) { if(EmptyCheck("Person") == i) break; else { printf("%d.Name = %s Car = %s\n",i, P[i].name, P[i].car.brand); } } } else if(strcmp(struct_to_display, "Car") == 0) { int i; if(EmptyCheck("Car") == 0) { printf("Nothing to Display!\nAdd a Car First!\n"); return; } for(i = 0; i <= 2; i++) { if(EmptyCheck("Car") == i) { break; } else { printf("%d.Brand = %s Model = %s Year = %d\n",i, C[i].brand, C[i].model, C[i].year); } } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.