I'm totally new to programming and taking an intro class. When i prompt the user for a state code the program just exits, nothing in my book really goes over this and all im given is crappy youtube videos for reference. Any help is greatly appreciated.

     #include <stdio.h>
    #include <stdlib.h>
    #include <string.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("%s",stateCode);

    for(i=0;i<SIZE;i++)
    {if (custDB[i].state==stateCode)
    printCustDB(custDB[i]);
    }

    system("pause");
    return (0);

    }

line 64: you can not compare two character arrays using the == operator, you have to call strcmp()

if ( strcmp(custDB[i].state,stateCode) == 0)

Further to the above by Ancient Dragon ...

you may like to recall that C strings are pointers to ...

(a '\0' terminated block of)

char ...

(i.e. addresses)

So ... do you wish to ask ...

if the addresses are the same?

Probably not ... most of the time :)

Also ... you will have much more joy in your coding in C when you find some regular ways to easily handle the problem of char's left in the input stream ...

After input ... often left are char's like the '\n' char at the end of a line.

These 'left over' char's ARE a BIG problem for new coders ... so the sooner you see the easy fixes, the sooner you will enjoy coding in C :)

(These 'left-over' char's ... will mess-up you next attempt to enter anything ... since those 'left-over' char's are ALL-READY there ... in the input stream, before you enter anything else ... they will be taken for the next input.)

When inputing (SINGLE word - NO spaces) strings, using scanf, you need to specify a max field width ... (so that you do not overflow the string size into other memory and crash the program or something?)

If spaces may occur in your input strings, use fgets to get the whole line ... and then fix that input by stripping off the '\n' char at the end ... or flushing the input stream if the buffer (size) did NOT hold the whole line ... (if that is what you want.)

Also, when passing struct's that hold more bytes than the bytes in the address size you are using ... pass the address (pass by reference ... NOT by value) ... to save making a copy of ALL that data in the struct ... (unless you really need that copy!)

See the example code below:

/*  structCustomer.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.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;


char* fixedFgets( char* s, size_t bufSize, FILE* fin )
{
    if( fgets( s, bufSize, fin ) )
    {
        char *p = strchr( s, '\n' ),
             c ;
        if( p ) *p = 0; /* strip off '\n' at end ... IF it exists */
        else while( (c = fgetc( fin )) != '\n'  &&  c != EOF ) ; /* flush... */
        return s;
    }
    else return NULL;
}

void getCustInfo( Customer* cust, int i )
{
    printf( "Enter Data for Customer <%d> "
            "(Avoid spaces in first name, last, or phone!)\n", i+1 );
    printf("   Enter First Last Phone: ");
    scanf("%29s %29s %14s", cust->firstName, cust->lastName, cust->phone);

    while( getchar() != '\n' ) ; /* 'eat' chars ... especially '\n' */

    printf( "Enter Addess Data:\n" );
    printf("   Street(max 34 chars): ");
    fixedFgets( cust->street, 35, stdin );

    printf("   City(max 19 chars): ");
    fixedFgets( cust->city, 20, stdin );

    printf("   State(2 chars only): ");
    fixedFgets( cust->state, 3, stdin );

    printf("   ZIP(number): ");
    scanf("%d", &cust->zip);
    while( getchar() != '\n' ) ; /* 'eat' chars ... especially '\n' */

    cust->accountId = i+1;
}

void printCustDB( const 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)
    {
        getCustInfo( &custDB[i], i);
    }

    printf("Enter 2-character state code: ");
    scanf("%s", stateCode);

    for(i=0;i<SIZE;i++)
    {
        if (  strcmp( custDB[i].state, stateCode ) == 0 )
            printCustDB( &custDB[i] );
    }

    printf( "\nPress 'Enter' to continue/ecit ... " );
    getchar();

    return (0);

}
commented: agree +14
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.