Loop comparing 2 strings?

This is a discussion on Loop comparing 2 strings? within the C Programming forums, part of the General Programming Boards category; I am currently a student learning c, and I had to make a struct loop to collect info for 10 ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    28

    Loop comparing 2 strings?

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

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    You need strcmp() to compare strings (or write your own function).
    Comparing with the == operator compares the pointers, not the contents.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    THANK YOU!!! It worked perfectly (after I realized that 'custArray[i].state == stateArray[i]' needed to be replaced with 'i < NAME_LEN' ).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing strings?
    By jh294 in forum C Programming
    Replies: 5
    Last Post: 04-09-2011, 01:04 AM
  2. Comparing Strings
    By aplst3 in forum C Programming
    Replies: 27
    Last Post: 05-11-2010, 05:19 PM
  3. comparing strings & dodgy forever loop - help!
    By Know_Your_Role in forum C++ Programming
    Replies: 6
    Last Post: 04-18-2009, 02:30 AM
  4. help comparing strings
    By molleman in forum C Programming
    Replies: 6
    Last Post: 03-07-2008, 07:09 AM
  5. Comparing 2 Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2002, 07:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21