By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,654 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,654 IT Pros & Developers. It's quick & easy.

Array values are not being output nor being passed to functions

P: 2
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 */
Apr 2 '18 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 2,400
Arrays Revealed should help you pass arrays to and from functions.

You should enclose your source code in CODE TAGS (select the lines of code and then click on the button labeled "[CODE/]".
Apr 2 '18 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Have you tried to step though this using your debugger? The debugger will show you what the contents of a variable is directly or through its address.
Apr 2 '18 #3

Post your reply

Sign in to post your reply or Sign up for a free account.