Thread: For loop ; printing records

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

    For loop ; printing records

    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
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I haven't really looked at your code, but I'd suggest search the board for scanf & fscanf, see if that helps, I think it just might.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    Code:
    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;
    }
    It looks to me as if you have left out some of these { } in your for loops.
    strcmp(); returns an int
    0 when strings are equal
    a value less than 0 if string 1 is less than string 2
    and a value greater than 0 if string 1 is greater than string 2

    so using 'if(strcmp(...) == 1)' may not produce the results you need.
    try if(strcmp(...) > 0)

    >customer[i] = customer[i+1];
    I'm not sure if you can copy one struct to another like this, I'll need to look up my book, but if anyone else could elaborate?
    If you can then I assume they have to be the same type of struct.

    hobo

  4. #4
    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/...&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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  3. reading a file into a block
    By mickey0 in forum C++ Programming
    Replies: 19
    Last Post: 05-03-2008, 05:53 AM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM