Using a structure representing a person's name : Struct Define « Structure « C / ANSI-C

C / ANSI-C
1. assert.h
2. Console
3. ctype.h
4. Data Structure Algorithm
5. Data Type
6. Development
7. File
8. Function
9. Language Basics
10. Macro Preprocessor
11. Math
12. math.h
13. Memory
14. Pointer
15. setjmp.h
16. signal.h
17. Small Application
18. stdio.h
19. stdlib.h
20. String
21. string.h
22. Structure
23. time.h
24. wctype.h
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C / ANSI-C » Structure » Struct DefineScreenshots 
Using a structure representing a person's name

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress

*/


#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define FIRST_NAME_LEN  31
#define SECOND_NAME_LEN 51
#define NUMBER_LEN      16
#define MAX_NUMBERS     50
#define TRUE             1
#define FALSE            0

/* Structure defining a name */
struct Name
{
  char firstname[FIRST_NAME_LEN];
  char secondname[SECOND_NAME_LEN];
};

/* Structure defining a phone record */
struct PhoneRecord
{
  struct Name name;
  char number[NUMBER_LEN];
};

struct Name read_name();               /* Read a name from the keyboard */               
void show(struct PhoneRecord record);  /* Output a phone record         */
int has_name(struct PhoneRecord record, struct Name name)/* Test for a name */

void main()
{
  char answer = 'n';
  struct PhoneRecord records[MAX_NUMBERS];  /* Array of phone records  */
  struct Name aName;                        /* Stores a name           */
  int count = 0;                            /* Number of phone records */
  int found = FALSE;                        /* Records when a name has been found */
  int i = 0;                                /* Loop control variable   */
 
  /* Read an arbitrary number of phone records from the keyboard */  
  do
  {
    records[count].name = read_name();                 /* Read the name */      
    printf("Enter the number for this name: ");
    scanf(" %[ 0123456789]",records[count++].number);  /* Read the number - including spaces */

    printf("Do you want to enter another(y or n)?: ");
    scanf(" %c", &answer);
  }while(count<=MAX_NUMBERS && tolower(answer== 'y');

  /* Search the array of phone records for a number */
  do
  {
    printf("Enter a name for which you want the number.");
    aName =read_name();
    for(i = ; i<count ; i++)
    {
      if(has_name(records[i], aName))                 /* Test for the name */
      {
        if(!found)                                    /* If this is the first time */
        {
          found = TRUE;                               /* Reset found flag       */
          printf("The numbers for this name are:\n")/* and output the heading */
        }
        printf("%s\n", records[i].number);            /* Output the number for the name */
      }
    }
    if(found)                                         /* If the name was found */
      found = FALSE;                                  /* Reset the found flag  */
    else                                              /* Otherwise output message */
      printf("No numbers found for this name.\n");
    printf("Do you want to search for another (y or n)? ");
    scanf(" %c" , &answer);
  }while(tolower(answer== 'y');

  for(i = ; i<count ; i++)
    show(records[i]);
  printf("\n");

}

/* Function to read a name and store it in a Name structure */
struct Name read_name()
{
  struct Name name;
    printf("Enter a first name: ");
    scanf(" %s", &name.firstname);
    printf("Enter a second name: ");
    scanf(" %s", &name.secondname);
  return name;
}

/* Function to output a record */
void show(struct PhoneRecord record)
{
  printf("\n%s %s   %s", record.name.firstname,record.name.secondname, record.number);
}

/* Function to test whether the name is the same as in a record */
int has_name(struct PhoneRecord record, struct Name name)
{
  return (strcmp(name.firstname, record.name.firstname)==&& strcmp(name.secondname, record.name.secondname)==0);
}

 

           
       
Related examples in the same category
1. Using a structure representing a length
2. Using a structure to record the count of occurrences of a word
3. How to use struct
4. Exercising the horse: Structure declaration
5. Pointing out the horses: allocate memory for struct
6. Define a simplest struct
7. Define and use a struct
w_w_w.j___a_v__a___2_s_.__co__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.