First C programming class and beginning to learn programming. I am currently learning how to use structures in C and this is a learning task to help understand this process.
The program should accept an input of addresses and organize them by zipcode from lowest zipcode to highest zipcode.
The program and input/output below is what I have so far. I would like to dump the data into a file using file redirection. For example:
ConsoleApplication1.exe < input.txt > testout.txt
Right now all that gets dumped into the test file is:
Code:
Processed 18 sets of data.
Obviously because of the printf statement I have in the 'takedate' function to test the process.
My question is, what would be the best way to take the input, process it, then dump the output in zipcode order into the file? Can anyone kindly recommend a function/statement that can be added in order to achieve this?
Thank you so very much in advance for your help, time, and guidance in getting this program working!
Code:
//header file intiating other headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Person{
char name[50];
char address[50];
char citystate[30];
char zip[10];
}Person;
int takedata(Person *arr[]);
void sortdata(Person * arr[], int noElements);
int main(void)
{
//Intiate program
int noElements = 0;
Person *arr[50];
//call input function
//input(work, &count);
takedata(arr);
//sort files
//call function
//swap(work, &count);
sortdata(arr, &noElements);
//call output function
//output(work, &count);
//end program
return 0;
}
void sortdata(Person * arr[], int noElements) {
/* temporary pointer to Person data type to aid with swapping */
//Person * tempptr = (Person *)malloc(sizeof(Person));
int i, j, compare;
Person * tempptr = NULL;
for (i = 0; i <= (noElements - 1); i++); {
for (j = (i + 1); j <= noElements; j++) {
compare = strcmp(arr[i] -> zip, arr[j] -> zip);
if (compare > 0) {
printf("attempted sort %d times.\n", j);
/* stores value in index i for array inside of temporary pointer */
tempptr = arr[i];
arr[i] = arr[j];
arr[j] = tempptr;
}
}
}
}
int takedata(Person *arr[])
{
/* counter variable */
int i = 0;
char teststring[25];
while ((gets(teststring)) != NULL && i < 50)
{
/* dynamically allocates memory for each index of the array */
arr[i] = (Person *)malloc(sizeof(Person));
/* takes in data from user/ input file */
strcpy(arr[i]->name, teststring);
gets(arr[i]->address);
gets(arr[i]->citystate);
gets(arr[i]->zip);
i++;
}
printf("Processed %d sets of data.\n\n", i);
return (i - 1);
}
Input data being used:
Code:
A1, A220294 Lorenzana Dr
Woodland Hills, CA
91364
B1, B2
19831 Henshaw St
Culver City, CA
94023
C1, C2
5142 Dumont Pl
Azusa, CA
91112
D1, D2
20636 De Forest St
Woodland Hills, CA
91364
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364
F1, F2
20225 Lorenzana Dr
Los Angeles, CA
91111
G1, G2
20253 Lorenzana Dr
Los Angeles, CA
90005
H1, H2
5241 Del Moreno Dr
Los Angeles, CA
91110
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
J1, J2
5135 Quakertown Ave
Thousand Oaks, CA
91362
K1, K2
720 Eucalyptus Ave 105
Inglewood, CA
89030
L1, L2
5021 Dumont Pl
Woodland Hills, CA
91364
M1, M2
4819 Quedo Pl
Westlake Village, CA
91362
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
N1, N2
20044 Wells Dr
Beverly Hills, CA
90210
O1, O2
7659 Mckinley Ave
Los Angeles, CA
90001