Trying to get this if statement to work but I can't seem to compare on variable to another that is in and array

Viewed 46

Here I'm just making a little and simple database.

Currently written code vv.

#include <stdio.h>
struct Customer{
 char firstName[30];
 char lastName[30];
 char street[35];
 char city[20];
 char state[3];
 int zip;
 char phone[15];
 int accountId;
};

int main()
{
    int size=1;// this number can be changed though would recommend leaving on 3 or something like that for quick testing
    struct Customer s[size];
    {
        int Count;
        for(Count=0; Count < size;Count++)
        {
            printf("Enter Data for Customer %i\n",Count);
            printf("Enter First Last Phone: ");
            scanf("%s   %s  %s",s[Count].firstName,s[Count].lastName,s[Count].phone);
            printf("Enter Address (Street City State ZIP): ");
            scanf("%s   %s  %s  %d",s[Count].street,s[Count].city,s[Count].state,&s[Count].zip);
            printf("\n");
        }
    }
    char locstate[3];
    int fail=0;
    char checkstate[3];
    int check;
    int check2;
    printf("which state would you like to locate customers in:");
    scanf("%s",locstate);
    for(check=0;check < size;check++)
    {
        {
            checkstate=s[check].state;
            //printf("%s",checkstate);
            if(locstate==checkstate)
            {
                printf("data for Customer %i",check);
                printf("Account: %i",check);
                printf("name: %s %s",s[check].firstName,s[check].lastName);
                printf("addr: %s %s %s %d",s[check].street,s[check].city,s[check].state,s[check].zip);
            }
            else
            {
               fail=fail+1; 
            }
        }
    }
    if(fail==size)
    {
        printf("there was no one in that state.");
    }
    else
    {
        return(0);    
    }
    
}

and if I instead remove the check state codes and try to directly compare locate to s.[check].state the if statement always fails regardless if both locstate and s.[check].state have received teh same variable form the user.

2 Answers

You have made several errors dealing with strings.

It looks like you want this statement to copy the value of state to checkstate.

checkstate=s[check].state

The way to do this is with strcpy.

strcpy(checkstate, s[check].state);

It looks like you want this statement to compare two strings.

if(locstate==checkstate)

The way to do this is with strcmp.

if(strcmp(locstate, checkstate) == 0)

As an aside, it's not good practice to have magic numbers all over your code, such as the 3 that defines the size of the char arrays state, locstate and checkstate. One way to handle this is with a defined constant.

#define STATE_LEN 3
...
char state[STATE_LEN];
...
char locstate[STATE_LEN];
char checkstate[STATE_LEN];

Also, your code does not protect against scanf reading too many characters into the various char arrays you use for fields in the struct. You can protect against reading more than the arrays can hold like this:

scanf("%2s", s[Count].state);

The error you are making is that, you are trying to compare two addresses. In C, there is no data-type as string unlike in C++ of Java.

Let's say there were two arrays int arr[] = {2, 3}; and int arr1[] = {3, 5}; and you wanted to compare whether the two arrays are same or not, would you use if(arr = arr1)? I guess you would run a loop and check element by element.

In c, the arrays are implemented as pointers and array names represent the address of the starting location of array i.e. arr[1] and *(arr + 1) are the same thing.

Also, I can see that you had this line commented printf("%s",checkstate);. What did you get as output after you ran the code. I guess some large number starting with 0x........ That's the starting location of the address of the char array allocated in the memory.

Now, to correct your code, you can either manually run a loop till array size and compare character by character or use strcpy and then strcmp.

Related