I am having a hard time writing a program to do the following:
Your program should create an array that contains ten (10) variables of the following type:
typedef struct {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} Customer;You need to prompt the user for values for each of the variables in each structure, i.e. you will be asking for 10 firstName values, 10 lastName values, and so forth. Hint: You should think about creating a function to input values for a structure, and then create a loop to call this function 10 times.
After all of the values for the structure variables have been input, you need to prompt the user for a state string, and then display the values of all variables in each structure where the state string matches the user's input. Hint: You should think about creating a function to print all of the values for a structure, and then call the function for those structures that meet your criteria.
You can assume that the user always enters proper data - no input validation is required.
Input1. Values for all component variables in each of the Customer variables.
2. State code to matchOutput
Values of the Customer variables that contain the desired state code
Sample (user input in blue italics, program output in magenta)Enter Data for Customer 0
Enter First Last Phone: Doug Oregon 123-456-7890
Enter Address (Street City State ZIP): Main Portland OR 12345
Enter Data for Customer 1
Enter First Last Phone: Doug Washington 123-456-7890
Enter Address (Street City State ZIP): Main Portland WA 12345
Enter Data for Customer 2
Enter First Last Phone: Doug California 123-456-7890
Enter Address (Street City State ZIP): Main Portland CA 12345
Enter Data for Customer 3
Enter First Last Phone: Doug Nevada 123-456-7890
Enter Address (Street City State ZIP): Main Portland NV 12345
Enter Data for Customer 4
Enter First Last Phone: Doug Colorado 123-456-7890
Enter Address (Street City State ZIP): Main Portland CO 12345
Enter Data for Customer 5
Enter First Last Phone: Another Colorado 123-456-7890
Enter Address (Street City State ZIP): Main Portland CO 12345
Enter Data for Customer 6
Enter First Last Phone: Doug Arizona 123-456-7890
Enter Address (Street City State ZIP): Main Portland AZ 12345
Enter Data for Customer 7
Enter First Last Phone: Doug Florida 123-456-7890
Enter Address (Street City State ZIP): Main Portland FL 12345
Enter Data for Customer 8
Enter First Last Phone: Doug Georgia 123-456-7890
Enter Address (Street City State ZIP): Main Portland GA 12345
Enter Data for Customer 9
Enter First Last Phone: Doug Jones 123-456-7890
Enter Address (Street City State ZIP): Main Portland CO 12345
Enter 2-character state code: CO
Data for Customer 4
Account: 4
Name: Doug Colorado
Addr: Main Portland CO 12345
Phone: 123-456-7890
Data for Customer 5
Account: 5
Name: Another Colorado
Addr: Main Portland CO 12345
Phone: 123-456-7890
Data for Customer 9
Account: 9
Name: Doug Jones
Addr: Main Portland CO 12345
Phone: 123-456-7890
This is a incomplete code doodle posted by my teacher:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 2 // initially to test kept it to SIZE 2
typedef struct {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
} Customer; // this is a new data type that you created.
// declaring it here makes it global to your program
// and hence accessible to all the functions in your program
Customer getCustInfo(int a ); // prototype which is one way of declaring functions
// the complete definition follows after main
void printCustDB(Customer cust) // another way of defining a function
// declaring and defining it in one go
{
printf("Data for Customer %d", cust.accountId);
printf(" First Last Phone: %s %s %s \n", cust.firstName, cust.lastName, cust.phone);
printf(" Address (Street City State ZIP): %s %s %s %d\n", cust.street, cust.city, cust.state, cust.zip);
}
int main() {
Customer custDB[SIZE]; // an array of Customer type of size SIZE
int i;
for(i = 0; i < SIZE; ++i)
{
// get information for each customer
// and fill your custDB array
custDB[i] = getCustInfo(i);
}
/*
provide the functionality to
1. prompt user for a state code
2. search the database against the state code
3. print customer info if match/matches found
*/
/*
* a helper function to print customer info
* using it here in a for loop prints info for
* all the customers in the database
* but could also be used to print info for a
* single customer as well
*/
for(i = 0; i < SIZE; ++i)
{
printCustDB(custDB[i]);
}
system("pause");
exit(0);
}
Customer getCustInfo(int a )
{
Customer cust;
char firstName2[30];
printf(" Enter Data for Customer %d\n", a + 1);
printf("Enter First Last Phone: \n");
scanf("%s %s %s", cust.firstName, cust.lastName, cust.phone);
printf("Enter Address (Street City State ZIP): \n");
scanf("%s %s %s %d", cust.street, cust.city, cust.state, &cust.zip);
cust.accountId = a + 1;
return cust;
}
This is what i have so far:
I am just working up to the part of collecting info and storing in it the corresponding array, however i am running into lots of issues that i dont quite understand. one of the issues it gives me is that the variables have not been declared in the function. I am using bloodshed vs for this if that makes a difference.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 2 // initially to test kept it to SIZE 2
typedef struct
{
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip;
char phone[15];
int accountId;
}
Customer;
Customer getCustInfo(int a);
void printCustDB(Customer cust)
{
printf("Data for Customer %d", cust.accountId);
printf(" First Last Phone: %s %s %s \n", cust.firstName, cust.lastName, cust.phone);
printf(" Address (Street City State ZIP): %s %s %s %d\n", cust.street, cust.city,
cust.state, cust.zip);
}
int main()
{
Customer custDB[SIZE]; // an array of Customer type of size SIZE
int i;
for(i = 0; i < SIZE; ++i);
{
printf("Enter Data for Customer %d\n");
printf("Enter First Last Phone: ");
scanf("%s %s %s", &cust.firstName, &cust.lastName, &cust.phone);
printf("Address (Street City State ZIP): ");
scanf("%s %s %s %d", &cust.street, &cust.city, &cust.state, &cust.zip);
}
system("pause");
return (0);
}
Any help that you could offer would be much appreciated...