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