HELP! Structures!!

I have this assignment, and the teacher said that it is open internet, open book, etc. I cannot figure out how to do the last part!

1.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;

2.
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.

3.
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.


Sample Output

Sample (user input in blue, 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 what I have so far:

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#define SIZE 2

typedef struct {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
int zip[8];
char phone[15];
int accountId;
} Customer;

int main()
{

Customer customer[10];
int x;
for (x = 0; x < 10; x++) {

printf("Enter Data For Customer %d", x);
printf("\n");


printf("Enter First, Last, Phone: ");
fgets(customer[x].firstName,10,stdin);
customer[x].accountId= strlen(customer[x].firstName) - 1;
customer[x].firstName[customer[x].accountId] = '\0';

fgets(customer[x].lastName,10,stdin);
customer[x].accountId= strlen(customer[x].lastName) - 1;
customer[x].lastName[customer[x].accountId] = '\0';

fgets(customer[x].phone,10,stdin);
customer[x].accountId= strlen(customer[x].phone) - 1;
customer[x].phone[customer[x].accountId] = '\0';

printf("Enter Address (Street, City, State, Zip): ");

fgets(customer[x].street,10,stdin);
customer[x].accountId= strlen(customer[x].street) - 1;
customer[x].street[customer[x].accountId] = '\0';

fgets(customer[x].city,10,stdin);
customer[x].accountId= strlen(customer[x].city) - 1;
customer[x].city[customer[x].accountId] = '\0';

fgets(customer[x].state,10,stdin);
customer[x].accountId= strlen(customer[x].state) - 1;
customer[x].state[customer[x].accountId] = '\0';

fgets(customer[x].zip,10,stdin);
customer[x].accountId= strlen(customer[x].zip) - 1;
customer[x].zip[customer[x].accountId] = '\0';
}


system("pause");
return 0;

}


_______________

I just cant figure out how to do the last question!! Please HELP ME!!!
Hi, akemikanegae !

I started with an example :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#define SIZE 2

typedef struct {
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[3];
char phone[15];
int zip;
int accountId;
}Customer;

int main()
{

Customer customer[10];
char yes[100];
for (int x = 0; x < 10; x++) {
system("cls");
//////////////////////////////////////////////////////////////////////////////
printf("\t\t\tEnter Required information for Customer %d\n\n",x + 1);
printf("\tFirst name : ");
//cin >> customer[x].firstName;
scanf("%s",customer[x].firstName);
printf("\n");
printf("\tLast name : ");
//cin >> customer[x].lastName;
scanf("%s",customer[x].lastName);
printf("\n");
printf("\tStreet : ");
//cin >> customer[x].street;
scanf("%s",customer[x].street);
printf("\n");
printf("\tCity : ");
//cin >> customer[x].city;
scanf("%s",customer[x].city);
printf("\n");
printf("\tTwo-character state code : ");
//cin >> customer[x].state;
scanf("%s",customer[x].state);
printf("\n");
printf("\tPhone : ");
//cin >> customer[x].phone;
scanf("%s",customer[x].phone);
printf("\n");
printf("\tZip : ");
//cin >> customer[x].zip;
scanf("%d",&customer[x].zip);
printf("\n");
printf("\tAccount Id : ");
//cin >> customer[x].accountId;
scanf("%d",&customer[x].accountId);
printf("\n");
////////////////////////////////////////////////////////////
system("cls");
printf("/////////////////////////////////////////////////////\n");
printf("\t\tReview the Account information (%d)\n",x + 1);
printf("\tFirst name : %s\n",customer[x].firstName);
printf("\tLast name : %s\n",customer[x].lastName);
printf("\tStreet : %s\n",customer[x].street);
printf("\tAddress : %s\n",customer[x].city);
printf("\tState : %s\n",customer[x].state);
printf("\tPhone : %s\n",customer[x].phone);
printf("\tZip : %d\n",customer[x].zip);
printf("\tAccount Id : %d\n\n",customer[x].accountId);
printf("\tAGREE (N : Re-enter) : ");scanf("%s",yes);
if(yes[0] == 'N' || yes[0] == 'n')x--;}

system("cls");
printf("\tEnough! The interview has been closed. Thanks all!!!\n");
system("pause");
return 0;

}


Any question?
<iostream.h> is not in my directory, and I cannot use it in this class. I have to find another way. Do you know of another way?
Try removing
<iostream.h>
would you then still be about to use cout and cin? It says that I cannot use those.
The example, does it run properly? Or compiling errors ?
What the compiler are you using?
You said "You want to use cout and cin" ? Or "The compiler doesn't accept cout and cin" ?
Last edited on
the compiler does not accept cout and cin, and the teacher does not either. I'm using C++
Look at the example, I used
printf - scanf
(no cin - cout)

If you want to use cout or cin, add
#include <iostream>
using namespace std;
i tried it, and it worked kind of. I 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.

It should look like this:

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

It just shows all the people that live in the state you prompted the user to type in within accounts 0-9.
At :

system("cls");
printf("\tEnough! The interview has been closed. Thanks all!!!\n");
system("pause");
return 0;

}

Change to :

system("cls");
printf("\t\tEnter 2-character state code : \n"); scanf("%s",yes);system("cls"); //yes : char[100]
for(int i = 0; i < 10; i++){
if(stricmp(yes,customer[i].state) == 0) //Right
{
printf("\t\tData for Customer %d\n",i);
printf("\tAccount : %d\n",customer[i].accountId);
printf("\tName : %s %s\n",customer[i].firstName, customer[i].lastName);
printf("\tAddr : %s %s %d\n", customer[i].city, customer[i].state, customer[i].zip);
printf("\tPhone : %s\n\n",customer[i].phone);
}
}
system("pause");

return 0;

}


Do you understand? Any question?
Last edited on
I tried that, and it doesnt print anything. After is asks for the 2 character state code, it ends the program.
Give your last input - output that you have already entered and received. (I need Customer "state" information and the final output search)
This is what i used to get the state information

fgets(customer[x].state,10,stdin);
customer[x].accountId= strlen(customer[x].state) - 1;
customer[x].state[customer[x].accountId] = '\0';

And i used this to try and answer the last question

printf("Enter 2-character state code : ");
scanf("%s",yes);
for(x = 0; x < 10; x++)
{
if(strcmp(yes,customer[x].state) == 0)
{
printf("Data for Customer %d",x);
printf("Account : %d\n",customer[x].accountId);
printf("Name : %s %s\n",customer[x].firstName, customer[x].lastName);
printf("Addr : %s %s %d\n", customer[x].city, customer[x].state, customer[x].zip);
printf("Phone : %s\n",customer[x].phone);
}


I deleted the system clear because it would delete everything and close my program. This way works better, it just doesnt say anything after i give the 2 character code. Sorry that I am not understanding...this is all very new to me.
Make sure you entered state information correctly. Log (print) state information by do a 10-time loop which only prints 10 customer's state before 'find customer state code', then post it here (Included your state search word).
Last edited on
Im sorry, I do not quite understand what you are saying. I have a ten time loop for the entire function, but do i need one just for the state?
You give your whole program output (But I need Customer 'state' information) - customer[x].state... Print (cout) them, and post them here with your state search word.
I ended up having to submit it as it was, but i got it to work, for the most part. Thank you SO much!!!
Topic archived. No new replies allowed.