473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array values are not being output nor being passed to functions

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
2 1627
donbock
2,426 Expert 2GB
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
9,208 Expert Mod 8TB
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

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

Similar topics

10
by: Geoff Cox | last post by:
Hello, I have a series of pages each of which creates an array of values. How do I keep all the array values until the last page? Cheers Geoff
6
by: brian_mckracken | last post by:
This might not be the right group for this question, since its kind of a pure html question... Given the html construct: <form action='index.php?expand=0,10000' method='post'> Email: <input...
10
by: Evangelista Sami | last post by:
hello i haven't touch any C code for long time and i dont remember how to declare an array of pointers on functions. i have tried this : ----------------------- 35 void firing_1 36 (firing f,...
1
by: cmrchs | last post by:
Hi, I have a function that creates an array and want to use that array in the client. but what is the syntax in C++.NET 2005 to specify an array as output param of a function ?? ref class Test...
1
by: DavidB | last post by:
I am working with a database and I would like to be able to populate an array at run time and then use the values that were pushed into the array as criteria for a select query. It seems that this...
7
by: sanjeevcis | last post by:
Hi, Below code is regarding to insert an array values into mysql db using php. i dont know why the code is not inserting array values into database properly. If is there any thing wrong with the...
1
by: Fareast Adam | last post by:
Anyone know how to sorting array values from least to greatest. Here the code; function mulsort($a) { sort($a); $c = count($a); for($i = 0; $i < $c; $i++) if...
1
by: shbabu | last post by:
my code is for(i=0;i<5;i++) { <input type="text" name="date1"> <input type="text" name="date2"> } i want to compare between two dates(which are in loops above) through the javascript...
5
by: Geethu03 | last post by:
Hi i have a array values in hidden format <form name="form2" method="post" action="test.php"> <input type="hidden" name="date_val" value="<%= date %>"> <input type="hidden" name="name_val"...
4
by: pano pano | last post by:
I'm really stuck with this. I have this script: $alpha = array(6=>8, 1=>5, 2=>9); ksort($alpha); foreach ($alpha as $key => $val) {echo "$val<br />\n";} result is
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.