Hey, I\'m trying to write a program that uses an array of nested structs to stor
ID: 3871513 • Letter: H
Question
Hey, I'm trying to write a program that uses an array of nested structs to store the addresses for a store’s customers.
Here is the criteria:
Each customer has a name and two addresses: home address and business address.
Each address has a street, city, state, and zip code.
1. Data structure
a. Define an Address struct with street, city, state and zip fields
b. Define a Customer struct with lastNm and firstNm fields, plus homeAddr and busAddr fields of type Address
c. Declare an array of type Customer
2. Use a menudriven program with the following selections:
a. Enter new customer
b. Display all customers
c. Display a particular customer
d. Exit the program
3. Define the following 5 functions
a. int displayMenu(); Outputs the menu selections Inputs the users selection Validates that the user has entered a valid selection
b. Customer getCustomer(); Asks the user to input the customer’s first name, last name and the two addresses and stores in a single Customer struct
c. void showCustomer(Customer); Outputs the information for a single Customer struct
d. Address getAddress(); Asks the user to enter each component of the address (street, city, state and zip) and stores in a single address struct Note that street will contain embedded blanks, so you will need to use getline. Since you are mixing cin and getline, you will need to use cin.ignore to skip over the last endline character in the input prior to using getline.
e. void findCust(Customer[], int); Asks the user to enter a customer’s first and last names Searches the array of Customers for a match If there is a match, prints out all information for the particular customer If not match, prints an error message.
I have some code so far but I'm still trying to get the basics and I figured I'd just post the whole assignment here. Thank you.
EDIT: Replying to the commenter, I believe it's for the total number of stored values? Would you use a for loop to scan through the Customer array?
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define STRING_SIZE 50
typedef struct addressType
{
char street[STRING_SIZE];
char city[STRING_SIZE];
char state[STRING_SIZE];
char zip[6];
}address;
typedef struct customerType
{
char firstNm[STRING_SIZE];
char lastNm[STRING_SIZE];
address homeAddr;
address busAddr;
}customer;
int displayMenu()
{
int choice = 0;
printf(" ");
printf(" _____________________________________________________________________ ");
printf("| | ");
printf("| PLEASE SELECT THE OPERATION FROM THE FOLLOWING LIST | ");
printf("|_____________________________________________________________________| ");
printf("| | ");
printf("| 0. EXIT From The Application | ");
printf("| 1. Enter Customer Record | ");
printf("| 2. Display All Customers | ");
printf("| 3. Display Perticular Customer | ");
printf("|_____________________________________________________________________| ");
printf(" Enter your choice here: ");
scanf("%d", &choice);
return choice;
}
address getAddress(){
address add;
printf("Street : ");
gets(add.street);
printf("City : ");
gets(add.city);
printf("State : ");
gets(add.state);
printf("Zip : ");
gets(add.zip);
return add;
}
customer getCustomer()
{
customer c;
// getchat is used to clear the buffer
getchar();
printf(" First Name: ");
gets(c.firstNm);
printf("Last Name: ");
gets(c.lastNm);
printf(" Enter Home Address ");
c.homeAddr = getAddress();
printf(" Enter Business Address ");
c.busAddr = getAddress();
return c;
}
void showCustomer(customer c){
printf(" First Name: %s", c.firstNm);
printf(" Last Name: %s",c.lastNm);
printf(" Enter Home Address");
printf(" Street : %s",c.homeAddr.street);
printf(" City : %s", c.homeAddr.city);
printf(" State : %s", c.homeAddr.state);
printf(" Zip : %s", c.homeAddr.zip);
printf(" Enter Business Address ");
printf(" Street : %s",c.busAddr.street);
printf(" City : %s", c.busAddr.city);
printf(" State : %s", c.busAddr.state);
printf(" Zip : %s", c.busAddr.zip);
}
void displayAll(customer customers[], int size){
int i;
printf(" List of All Customers ");
for(i = 0; i < size; i++){
customer c = customers[i];
printf(" _____________________________________________________________________ ");
printf(" Customer : %d", i+1);
printf(" _____________________________________________________________________ ");
showCustomer(c);
}
}
void findCustomer(customer customers[], int size){
int i;
char firstName[STRING_SIZE];
char lastName[STRING_SIZE];
getchar();
printf(" First Name: ");
gets(firstName);
printf("Last Name: ");
gets(lastName);
for(i = 0; i < size; i++){
customer c = customers[i];
if(strcmp(firstName, c.firstNm) == 0 && strcmp(lastName, c.lastNm) == 0){
printf(" _____________________________________________________________________ ");
printf(" Customer found = >", i+1);
printf(" _____________________________________________________________________ ");
showCustomer(c);
return;
}
}
printf(" No Record found for the entered customer ");
}
int main() {
customer customers[50];
int size = 0;
// first time menu option
int option = displayMenu();
while(option > 0 && option < 4){
switch(option){
case 1:{
//to get customer from the input
customer c = getCustomer();
customers[size] = c;
size++;
break;
}
case 2:{
//show all customers
displayAll(customers, size);
break;
}
case 3:{
//search for perticular customer
findCustomer(customers, size);
break;
}
default:{
break;
}
}
//ask for option again
option = displayMenu();
}
printf(" You must have entered wrong choice or 0.. Exiting ");
return 0;
}