I am currently a student learning c, and I had to make a struct loop to collect info for 10 customers, then prompt the user to input a two-digit state code, and then have the program output every entry that used that state code. I was able to do the first part, but the second just isn't working. I know I'm missing something (probably simple), but I don't know what.
I've tried building an array and comparing values (that didn't work), and also setting up a simple char value and comparing that (and that didn't work). What am I missing?
Code:
#include <stdio.h>
#include <stdlib.h>
#define NAME_LEN 10
int main() {
typedef struct
{
char firstName[30];
char lastName[30];
char street[35];
char city[20];
char state[4];
int zip [15];
char phone[15];
int accountId[40];
} Customer;
int myIntArray[NAME_LEN] = {0};
Customer custArray[NAME_LEN];
int i = 0, n = 0, lenOfStr = 0;
char state, stateArray [4];
for(i = 0; i < NAME_LEN; ++i)
{
printf("Enter Information for Customer %d\n", i + 1);
printf ("First Name: ");
fgets (custArray[i].firstName, 30, stdin);
lenOfStr = strlen (custArray[i].firstName);
custArray[i].firstName[lenOfStr - 1] = '\0';
printf ("Last Name: ");
fgets (custArray[i].lastName, 30, stdin);
lenOfStr = strlen (custArray[i].lastName);
custArray[i].lastName[lenOfStr - 1] = '\0';
printf ("Address: ");
fgets (custArray[i].street, 35, stdin);
lenOfStr = strlen (custArray[i].street);
custArray[i].street[lenOfStr - 1] = '\0';
printf ("City: ");
fgets (custArray[i].city, 20, stdin);
lenOfStr = strlen (custArray[i].city);
custArray[i].city[lenOfStr - 1] = '\0';
printf ("Two-Digit State Code (ex. CO for Colorado, OR for Oregon): ");
fgets (custArray[i].state, 4, stdin);
lenOfStr = strlen (custArray[i].state);
custArray[i].state[lenOfStr - 1] = '\0';
printf ("Zip Code: ");
fgets(custArray[i].zip, 15, stdin);
lenOfStr = strlen (custArray[i].zip);
custArray[i].zip[lenOfStr - 1] = '\0';
printf("Phone Number: ");
fgets (custArray[i].phone, 15, stdin);
lenOfStr = strlen (custArray[i].phone);
custArray[i].phone[lenOfStr - 1] = '\0';
printf("Account Number: ");
fgets (custArray[i].accountId, 40, stdin);
lenOfStr = strlen (custArray[i].accountId);
custArray[i].accountId[lenOfStr - 1] = '\0';
}
printf("Enter a Two-Digit State Code (ex. CO for Colorado, OR for Oregon):");
scanf("%c", &state);
//fgets (stateArray, 4, stdin);
//lenOfStr = strlen (stateArray);
//stateArray[lenOfStr - 1] = '\0';
//for (i = 0; custArray[i].state == stateArray[i]; i++)
for (i = 0; custArray[i].state == state; i++)
{
printf("Customer: %s, %s \nState: %s", custArray[i].lastName, custArray[i].firstName, custArray[i].state);
}
return(0);
}