C Program that first prompts you for the number of customers you would like to enter in

How to Write a program that first prompts you for the number of customers you would like to enter in.


The program will then allow you to enter the customer information, namely:
names(first and last),
address,
phone numbers(home and cell).

Solution:
/*Write a program that first prompts you for the number of customers you would like to enter in.

The program will then allow you to enter the customer information, namely:
names(first and last),
address,
phone numbers(home and cell).

Your program should then print out the customers, sorted in order by lastname, and then phone number.
You should store the customers as a dynamic array of structures.
The strings for the names etc. should be dynamically allocated also.Use of pointers absolutely
extensively and throughout your solution is required*/

#include <string.h>
#include <stdio.h>

typedef struct {
char * firstName;
char * lastName;
char * address;
int homePhone;
int cellPhone;
} customer;

void newCustomer(customer * cust);
void printCustomer(customer * cust);
char *getString();
int getNumber();

int main()
{


// Ask for number of customers to input
printf("How many customers would you like to input?   ");
int numCust = getNumber();
printf("You have %i customers to input\n\n", numCust);

// Create array of customers
customer *customerList = (customer *)malloc(numCust * sizeof(customer));

// Ask for information for the selected number of customers
for (int i = 0; i < numCust; i++)
{
printf("Customer %i of %i", i, numCust);
newCustomer(&customerList[i]);
}

// Sort the list of customers by lastname then by (home) phone number

// Print the list of customers
for (int i = 0; i < numCust; i++)
{
printf("Customer %i of %i", i, numCust);
printCustomer(&customerList[i]);
}
}

/* Enter Customer Data*/
void newCustomer(customer *cust) {
printf("First Name:   ");
cust->firstName = getString();
printf("Last Name:   ");
cust->lastName = getString();
printf("Address:   ");
cust->address = getString();
printf("Home Phone:   ");
cust->homePhone = getNumber();
printf("Mobile Phone:   ");
cust->cellPhone = getNumber();
}

void printCustomer(customer *cust) {
printf("First Name:   %s\n", cust->firstName);
printf("Last Name:   %s\n", cust->lastName);
printf("Address:   %s\n", cust->address);
printf("Home Phone:   %i\n", cust->homePhone);
printf("Mobile Phone:   %i\n\n", cust->cellPhone);
}

/* Get an integer number from the user*/
int getNumber() {

int num = 0;

while (num == 0) {

//Ask the user for a number
if (scanf_s("%i", &num) != 1) {

//If there were spaces or Non-numeric characters
printf("That is not an integer number.\n");
printf("Please enter an integer number:   ");
num = 0;

}

//Clear buffer
while (getchar() != '\n');
}

return num;
}

/* Get a string from the user*/
char *getString() {
char input[100];
scanf_s("%s", input);
char * result = (char *)malloc(sizeof(char)*(strlen(input) + 1));

//If there isn't enough memory
if (!result) {
printf("Out of Memory");
exit(1);
}

strcpy_s(result, input, "");
return *result;
}


Learn More :