Struct halp.. :p

This is a discussion on Struct halp.. :p within the C Programming forums, part of the General Programming Boards category; I have just finished more than half of my assignment for school. But i am completely stumped on the second ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    56

    Struct halp.. :p

    I have just finished more than half of my assignment for school. But i am completely stumped on the second problem.
    But now i need to prompt the user for the state string.

    So far i have this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define SIZE 2
    #define PERSN_SZE 9
    
    
    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()
    {
        Customer customer[SIZE];
        int i;
        for ( i = 0; i < 9; i++)
        {
            customer[i].accountId = i + 1;
            printf("\nEnter Data for Customer %d", i + 1);
            printf("\nEnter First Last Phone: ");
            scanf("%s %s %s", customer[i].firstName, customer[i].lastName, customer[i].phone);
            printf("\nEnter Address (Street City State ZIP): ");
            scanf("%s %s %s %d", customer[i].street, customer[i].city, customer[i].state, &customer[i].zip);
        }
        for ( i = 0; i < 9; i++)
        {
            printf("=============================================================\n");
            printf("\nData for Customer %d\n", customer[i].accountId);
            printf("Account: %d\n", customer[i].accountId);
            printf("Name: %s %s\n", customer[i].firstName, customer[i].lastName);
            printf("Addr: %s %s %s %d\n", customer[i].street, customer[i].city, customer[i].state, customer[i].zip);
            printf("Phone: %s\n", customer[i].phone);
        }
        system("pause");
    }
    When you compile and enter the inputs it looks like this.
    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
    But now i need to prompt the user for the state string. Which should give me output like this:
    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    So, it's just a for loop through your array, and printing out all the entries where
    strcmp( customer[i].state, "CO") returns 0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Quote Originally Posted by Salem View Post
    So, it's just a for loop through your array, and printing out all the entries where
    strcmp( customer[i].state, "CO") returns 0.
    You are correct about the for loop.
    But "CO" was actually an example of the user inputting the state, and then my program searching through all the other user input for it. Is there a function that can do that?
    Within <string.h>

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    So, you already know how to read strings into variables (char arrays).

    Just declare a string (say char stateToSearchFor[10]) and use it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    56
    Do i use that to compare to the other char arrays?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    You've already demonstrated knowledge of arrays, for loops and scanf.

    The only remaining thing is strcmp, which is in string.h (again, you know this).

    All that remains is for you to pull it together.

    > for ( i = 0; i < 9; i++)
    And this should be
    for ( i = 0; i < SIZE; i++)
    to match your array size.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Music database using Linked Data Structures. halp!
    By Chucker in forum C Programming
    Replies: 1
    Last Post: 03-28-2012, 04:34 PM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. Replies: 7
    Last Post: 09-14-2010, 04:24 PM
  4. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  5. Ned Halp Wit Getng Input
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 03-18-2005, 02:50 PM

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