Thread: problem with printing a structed array using for loop

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    6

    problem with printing a structed array using for loop

    hey im using a for loop to print some records from an array. When the records get printed, they all print but after the results come up, it just prints Garbage, i was just wondering how i can fix that, here is the program below. (THE for loop with the problem is at the bottom)
    Thanx.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #define MAX 10
    /* size of structure array */
    
    typedef struct {
    char lastname[ 20 ];
    char firstname [ 10 ];
    char phone [ 15 ]; // type defination Structure called Customer
    int studentid;
    } customerData;
    
    void instruction (void); // instructoin function
    void main_menu (void);
    void bubblesort(customerData[], int);
    void printrecord(customerData[] , int);
    int main()
    {
    customerData customer[MAX];
    customerData hold;
    int i = 0, pass, element;
    char targetName[ 20 ];
    int s;
    // **************************************************
    ****************************************
    instruction();
    
    main_menu();
    
    printf("\t\t\t *****************************\n");
    printf("\t\t\t ******ENTER YOUR RECORDS*****\n");
    printf("\t\t\t *****************************\n\n");
    printf("Enter students records (names and phone no.)\n");
    printf("\nThey will then be sorted by their lastname.\n\n");
    
    printf( "Enter student number Eg. 27135 ( 0 to end input )\n? " );
    scanf( "%d", &customer[i].studentid );
    while ( customer[i].studentid != 0 )
    {
    printf( "Enter lastname, firstname and phone number\n? " );
    scanf( "%s%s%s", customer[i].lastname, customer[i].firstname,
    customer[i].phone );
    system("cls");
    i++;
    printf( "Enter Student ID number Eg. 27135 (0 to end input)\n? " );
    scanf( "%d", &customer[i].studentid );
    }
    // **************************************************
    ****************************************
    for ( pass = 1; pass <= 3 - 1; pass++ ) /* passes */
    for ( i = 0; i <= 3 - pass -1; i++ ) /* one pass */
    if ( strcmp( customer[i].lastname, customer[i+1].lastname ) == 1) {
    hold = customer[i];
    customer[i] = customer[i+1];
    customer[i+1] = hold;
    }
    system("cls");
    
    printf(" - 2. To View Results in order\n");
    
    printf("\nRecords in name order:\n\n");
    printf("Student ID LastName Firstname Phonenumber\n\n");
    for ( i = 0; i <= MAX - 1; i++) {
    
    printf("%-20d%-15s%-10s%12s%\n",customer[i].studentid, customer[i].lastname, customer[i].firstname, customer[i].phone );
    }
    Code tags added by kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    In the furture please use Code Tags.

    Information on code tags may be found at:

    http://www.cprogramming.com/cboard/s...threadid=13473
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >for (i = 0; i <= MAX - 1; i++)
    This loop is printing the whole array. If the user only enters 4 of the total 10 entries, the last 6 are going to contain junk. Just count how many entries the user inputs, and loop that many times to print the results.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. problem with large array
    By supi in forum C Programming
    Replies: 2
    Last Post: 07-06-2008, 10:06 PM
  3. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM