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 :
Customer
Enter by User
- C Program to Enter an ODD number between 1 and 49.
- C Program to Calculation of One Number Raised to Another
- C Program to Count Vowels, Consonants and Spaces in a string
- C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle.
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to find result where value and power are user given
- C Program That in a Given Natural Number x Insert Figure c at Position p
- C Program To Find Prime Number And If Number Is Not Prime Then Find Factors
- C program that lets the user choose if he wants to add a item to the shopping list or print it out
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to Input Number by the user between 1-100
- How to Gets User Input For Height in C Programming
- Loudness Program: Gets loudness level from user; chooses a response And prints it using logical and operative statements
- C Program Exchange User Entered Text Between 2 Modules
- C Program to accept n numbers from user & find out the maximum element out of them by using dynamic memory allocation
- C Program to accept string from user & convert all lower case letters into upper case letters & vice versa
- C Program to accept m*n matrix from user and display the elements of given matrix using function
- C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers
- C program to accept n number from user,store these numbers into an array & count the no of occurrences of each number
- C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
- C Program to accept a string from user, delete all vowels from that string & display the result
- Write a c program to accept a string from user & generate following pattern (e.g, input "abcd")
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array
First
Number
- Find out the perfect number using c program
- Write a c program to find out H.C.F. of two numbers.
- Check the given number is armstrong number or not using c program.
- Write a c program to find largest among three numbers using conditional operator
- FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.
- FIND PRIME FACTORS OF A NUMBER USING C PROGRAM
- How To Write a C program that generates two random numbers ?
- Write a C program to find maximum or equal between two numbers ?
- How to Write a C program to find maximum between two numbers ?
- Write a C program to perform math operations on two input whole numbers. The operations are:
- Write a C program to find maximum between three numbers ?
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program to Enter an ODD number between 1 and 49.
- C program acquires keyboard 10 numbers for each of these numbers to determine if it is a first issue, by printing immediately message should at the end, if none of the numbers you entered was a first issue, print an appropriate message.
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Reverse Of Any Digit Number
- C Program To Find The Frequency Of A Number
- C Program To Print Prime Numbers Upto The Number You Want
- C Program To Print Sum Of n Digit Number
- C Program To Reverse A Number
- C Program To Search A Number Inside The Array