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

1. Values for all component variables in each of the Customer variables.
2. State code to match

Output

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

>I am having a hard time writing a program to do the following.........
Please tell us where you're having problems with.

The program pretty much is not running at all....every time i try to compile it gives me an error related to an undeclared variable that has already been declared in typedef.

The program pretty much is not running at all....every time i try to compile it gives me an error related to an undeclared variable that has already been declared in typedef.

Can you also post all the errors you're having?

Here's one problem:

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);
}

Why are you trying to fill cust ten time's over? I don't even see cust declaired. Maybe I red your problem wrong, but don't you wan't to put the information in custDB?
Good luck.

You have a semi-colon at the end of your for statement in your code - that will prevent the prompting and gathering of more than one set of data.

You declare a variable called custDB - I think that should be cust.

You're applying the address-of operator (&) for the char array members of your data structure - that's a NO.

You're not providing an index for your cust array of type Customer when setting your data.

That being said, I have modified your code example to the point where you should be able to tweak it to suit the requirements of your assignment. It will prompt for two "sets of customer data" and print out a subset of that data. I have "chucked" everything into the main function - it will be your exercise to refactor that code into the functions required by your assignment. The rest is up to you.

#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;


int main(void) {

    Customer cust[SIZE]; // an array of Customer type of size SIZE
    int i;

    for (i = 0; i < SIZE; ++i) {

        printf("\n");
        printf("Enter Data for Customer %d\n", i);
        printf("Enter First Last Phone: ");

        scanf("%s %s %s", cust[i].firstName, cust[i].lastName, cust[i].phone);
        printf("Address (Street City State ZIP): ");
        scanf("%s %s %s %d", cust[i].street, cust[i].city, cust[i].state, &cust[i].zip);
    }

    for (i = 0; i < SIZE; ++i) {
        printf("Customer #%d\n", i);
        printf("First = %s\n", cust[i].firstName);
        printf("Last = %s\n", cust[i].lastName);
        printf("Phone = %s\n\n", cust[i].phone);
    }

    return (0);
}

Cheers,
JD

commented: Very solid post :) +18

Thank you very much sir! Now i can hopefully figure out the second part today!

@RobBrown:
>was rather unhelpful, and honestly not very bright...

I'm very sorry that my eyes aren't good enough to see the error messages on your computer's monitor.
Do you maybe expect that I'm clairvoyant?

I told you the error was related to an undeclared variable.....two other people understood and were able to offer some great help. I guess it just goes to show that experience doesnt mean much on this forum all the time.....

I told you the error was related to an undeclared variable.....two other people understood and were able to offer some great help. I guess it just goes to show that experience doesnt mean much on this forum all the time.....

"related to an undeclared variable" is not an error message I've ever seen a compiler generate. In the IT field, you give exacly what the compiler is outputing.

Saying "was rather unhelpful, and honestly not very bright..." is pretty mean to say. His intentions was to help you understand the error better, push you in the right direction, and help you fix it yourself.
I decided to do something worse, and just point to what your vague error's was saying (stuff about undecared things).
yellowSnow gave you the whole thing, and left you to try to fix an irelevant problem. Don't get me wrong yellowSnow, you tried to do the right thing, and it was helpful, but OP may not learn to fix his original problem. I did the same thing when I started posting here not too long ago.

I apologize to all who i have offended here. I thought what i had put down for the error i was getting was enough as i was on another computer at the time not looking at my compiler. I am new to this forum and programming so i guess i am a little defensive at how i am responded to. Sorry to all, my bad.

Ive moved onto the second part of my program now, and seem to be missing one part (the key part) of code to make this do what it needs to. I can get the program to print all the data in the database, however i cannot get it to just show the data that corresponds to a specific state code. I think it has something to do with the loop i am using to compare, but i am not sure. Here is my code, any suggestions?

#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)
{
Customer cust;
char firstName2[30];
printf("Enter Data for Customer %d\n", a);
printf("Enter First Last Phone: ");
scanf("%s %s %s", cust.firstName, cust.lastName, cust.phone);
printf("Enter Address (Street City State ZIP): ");
scanf("%s %s %s %d", cust.street, cust.city, cust.state, &cust.zip);
printf("\n");
cust.accountId = a;
return cust;
} 
  
       void printCustDB(Customer cust) 
{
printf("Data for Customer %d\n", cust.accountId);
printf("Name: %s %s\n",cust.firstName, cust.lastName);
printf("Address: %s %s %s %d\n", cust.street, cust.city, cust.state, cust.zip);
printf("Phone: %s\n",cust.phone); 
printf("\n");
} 
       
  
      int main(void) {
  
       
  
      Customer custDB[SIZE]; // an array of Customer type of size SIZE
  
      int i;
  
       
  
      for (i = 0; i < SIZE; ++i) 
{
      
      custDB[i] = getCustInfo(i);
      
}

{
      printf("Enter 2-character state code: ");
      scanf(" %c",&i);
      
}
      for (i = 0; i < SIZE; ++i)
{

      printCustDB(custDB[i]);
}

      system("pause");
      exit(0);
  
}

Well The Solution to your Second part is pretty simple.
first get the State Code u want

char reqState [3];
scanf("%s",&reqState);

then u need to loop through your Customer Database to get the Required customer in the array whose state equals the required state

i.e:
int i;
for(i=0;i<SIZE;i++)
{
if(custDB.state == reqState)
{
printCustDB(custDB);
}
}

I hope u find that informative :)

Your for statment is over writting i.

Your scanf is completly wrong. i is an integer. Try: char state[3] = {0}; scanf("%c%c", state[0], state[1]); if all state codes are two characters long.

You have swerly brackets around your printf and scanf statements.

Your code is formatted wierdly.

You closed your program with:

system("pause");
      exit(0);

Those are just the problems on the lower part of your code. I'm too lazy to find the rest.

No offence, but it looks like you have no idea what your doing.
Think for a second! Befor you write random code, you need to think. The objective is to get the user to input a state code, and output anything that has a matching stade code. So you need to scan through all the entries. If custDB.state is the same as the user input, print it off to the sceen.

EDIT: I didn't see your post TheCo3der, but use code tag's.

Your rigth i have no idea what im doing. the class i am taking is supposed to be intro to c. the teacher gives horrible lectures and offers little help with code snippets. We are expected to take what we learn in the book, which is usually a fairly basic example, and use it in a way that we have never seen done before. Most of my class is barely treading water right now, which according to most people i know who have taken this class in the past is how this class works.

Your rigth i have no idea what im doing. the class i am taking is supposed to be intro to c. the teacher gives horrible lectures and offers little help with code snippets. We are expected to take what we learn in the book, which is usually a fairly basic example, and use it in a way that we have never seen done before. Most of my class is barely treading water right now, which according to most people i know who have taken this class in the past is how this class works.

I sympathize with you. I've seen lots of bad teachers. Go through these to learn C, use google to learn something you don't understand and use daniweb when you need assistance. Good luck.

Okay so i'm still having problems....This is the part that still not working, this is what im working with:

int main(void) 
{
  
Customer custDB[SIZE]; // an array of Customer type of size SIZE
int i;
char stateCode[3];
  
for (i = 0; i < SIZE; ++i) 
{
custDB[i] = getCustInfo(i);
}
      
printf("Enter 2-character state code: ");
scanf(" %c",&stateCode);
    
for(i=0;i<SIZE;i++) 
{if(custDB[i].state == stateCode[3]) 
printCustDB(custDB[i]);
}  
      
system("pause");
return (0);
  
}

instead of looping around to find a match in the DB and printing as it should by calling the printCustDB(custDB) function it is just skipping and going to the end of the program...if you want to run and see here is the whole code:

#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 )
{
Customer cust;
char firstName2[30];
printf(" Enter Data for Customer %d\n", a);
printf("Enter First Last Phone: ");
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;
} 
  
void printCustDB(Customer cust) 
{
printf("Data for Customer %d\n", 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(void) 
{
  
Customer custDB[SIZE]; // an array of Customer type of size SIZE
int i;
char stateCode[3];
  
for (i = 0; i < SIZE; ++i) 
{
custDB[i] = getCustInfo(i);
}
      
printf("Enter 2-character state code: ");
scanf(" %c",&stateCode);
    
for(i=0;i<SIZE;i++) 
{if(custDB[i].state == stateCode[3]) 
printCustDB(custDB[i]);
}  
      
system("pause");
return (0);
  
}

I solved my own problem, i was missing some valuable code, it seems to work fine now. Thanks to all who have helped with this!!

commented: Good for you :) +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.