display records held in a struct

This is a discussion on display records held in a struct within the C Programming forums, part of the General Programming Boards category; Hi Guys, Im trying to write a small function that when called will print out any records currently held in ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    33

    display records held in a struct

    Hi Guys,


    Im trying to write a small function that when called will print out any records currently held in the struct.

    If there are no records being held then a message will say no records.

    If there are records then they will be displayed.

    The code I have attempted runs but loops through all the code and displays the Max records all blank.........

    I wondered if anyone could guide me a little.

    Im using Borland 4.5 On WinXP

    Regards as ever
    C

    Code:
    void display_all(void)
    {
     int key;
     int loop=0;
    
        while(loop < MAX)
      {
    
         if(my_database[loop].reg_no !="\0")
        {
        printf("\nReg ID        : %s\n",my_database[loop].reg_no);
        printf("\nFirst Name    : %s\n",my_database[loop].fname);
        printf("\nSurname       : %s\n",my_database[loop].sname);
        printf("\nCredits       : %d\n",my_database[loop].credit);
        printf("--------------------------------------------- ");
        printf("\n");
        }
    loop++;
    }
    
    key=getchar();
    }

  2. #2
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    Code:
    if(my_database[loop].reg_no !="\0")
    Isn't that suppose to be

    Code:
    if(my_database[loop].reg_no[0] !='\0')
    You should get an error with the first one

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    void display_all(int n)
    {
      int key;
    
      if (n == 0)
        printf("No records\n");
      else {
        int loop;
    
        for(loop = 0; loop < n; loop++)
        {
          if(my_database[loop].reg_no !="\0")
          {
            printf("\nReg ID        : %s\n",my_database[loop].reg_no);
            printf("\nFirst Name    : %s\n",my_database[loop].fname);
            printf("\nSurname       : %s\n",my_database[loop].sname);
            printf("\nCredits       : %d\n",my_database[loop].credit);
            printf("--------------------------------------------- ");
            printf("\n");
          }
        }
      }
    
      key=getchar();
    }
    In the code that calls this function, you would keep a count of how many records there are. Just pass that to display_all and the correct number of records will be printed (or an appropriate message will be displayed if there are none).
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    33
    Thanks Prelude

    That seems to have worked a treat.

    Many thanks

    C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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