I have to write a C program that takes input of clients for a business. I am using a structure with an array to store the data, then passing the array to functions to be sorted. The issue is that the data is not being passed. Here is the code.
#include <stdio.h>
#include <string.h>
#define CLIENT 5
struct clients
{
int client_num;
int account_num;
int account_2;
int temp_acc;
float ballence[5];
char last_name[31];
char temp;
};
int sort_account_num (struct clients [], int);
void sort_last_name (struct clients []);
void safer_gets (char[], int);
int main (void)
{
struct clients info[2];
int x = 0, i = 0;
char first_acc_num = 'F';
char c;
printf("Enter account number, last name, and balance.");
while (x < 2)
{
printf("\n? ");
scanf("%d" , &info[x].account_num);
while ((c = getchar()) != '\n' && c != EOF); /* For clear input buffer. */
if (info[x].account_num < 1 ||info[x].account_num > 1000)
{
printf("*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***\n");
continue;
}
else if (info[0].account_num == -999)
{
first_acc_num = 'T';
return 0;
}
else if (first_acc_num == 'T')
{
return 0;
}
else if (info[x].account_num == -999)
{
break;
}
printf("\n? ");
safer_gets (&info[x].last_name[x], 30);
printf("\n? ");
scanf("%f" , &info[x].ballence[x]);
while ((c = getchar()) != '\n' && c != EOF); /* For clear input buffer. -*/
if (info[x].ballence[x] <= -999.99)
{
printf("*** Invalid balance amount. Please enter a positive value. ***\n");
continue;
}
x++;
}
for (x = 0; x < 2; x++)
{
printf("%i" , info[x].account_num);
}
sort_account_num (info, 6);
sort_last_name(info);
getchar();
return 0;
}
/* Begins sort data function. */
/* -------------------------- */
int sort_account_num (struct clients acc_num[], int input_num)
{
char changed = 'T';
int x;
while (changed == 'T')
{
changed = 'F';
for (x = 0; x < 2; x++)
{
if (acc_num[x].account_num > acc_num[x + 1].account_num)
{
acc_num[x].temp_acc = acc_num[x].account_num;
acc_num[x + 1].account_num = acc_num[x].account_num;
acc_num[x + 1].account_num = acc_num[x].temp_acc;
changed = 'T';
}
}
}
for (x = 0; x < 3; x++)
{
printf("%i\t\n" , acc_num[x].account_num);
}
return acc_num[x].account_num;
}
void sort_last_name (struct clients last_name[])
{
int i;
int j = 0;
for (i = 0; i - 1; i++)
{
for (j = 0 + 1; j < CLIENT; j++)
{
if (strcmp(&last_name[i].last_name[i], &last_name[j].last_name[i]) > 0)
{
last_name[i].temp = last_name[i].last_name[i];
last_name[i].last_name[i] = last_name[j].last_name[j];
last_name[j].last_name[j] = last_name[i].temp;
}
}
}
for (i = 0; i < CLIENT; i++)
{
printf("%s" , last_name[i].last_name);
}
}
void safer_gets (char array[], int max_chars)
{
/* Declare variables. */
/* ------------------ */
int i;
/* Read info from input buffer, character by character, */
/* up until the maximum number of possible characters. */
/* ------------------------------------------------------ */
for (i = 0; i < max_chars; i++)
{
array[i] = getchar();
/* If "this" character is the carriage return, exit loop */
/* ----------------------------------------------------- */
if (array[i] == '\n')
break;
} /* end for */
/* If we have pulled out the most we can based on the size of array, */
/* and, if there are more chars in the input buffer, */
/* clear out the remaining chars in the buffer. */
/* ---------------------------------------------------------------- */
if (i == max_chars )
if (array[i] != '\n')
while (getchar() != '\n');
/* At this point, i is pointing to the element after the last character */
/* in the string. Terminate the string with the null terminator. */
/* -------------------------------------------------------------------- */
array[i] = '\0';
} /* end safer_gets */