I am working on an assignment identical to another post from a couple years ago, for reference here is the thread:
array of pointers to structures sorting addresses by zip code
They way it is written on that thread is almost identical to the way the teacher implied to have it done (only wrote part of the input block) and that thread helped me understand a lot more what was actually going on. But I am having an error:
When it gets to the output section it outputs then next name along with the zip code... I tried strncpy and strxfrm but both cause more problems than they did help.
The last part of the project is to have the output put out in order of least zip code to most zip code (00000<99999), so this is causing me a real problem and I do not see what exactly is making this happen.
Here is my code (we dont HAVE to use gets but professor suggested using it for this assignment, next lab is to rewrite this using files and fgets rather than I/O redirection):
header.h
Code:
#ifndef lab_6b_7b_Header_h
#define lab_6b_7b_Header_h
//header file intiating other headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char name[25];
char street[25];
char state[25];
char zip[5];
}INFORMATION;
typedef INFORMATION *INFOPTR;
//function prototypes
void INPUT(INFORMATION *work[], int *);
void OUTPUT(INFORMATION *work[], int *);
#endif
main.c
Code:
#include "header.h"
int main(void)
{
//Intiate program
int count=0;
INFORMATION *work[50];
//call input function
INPUT(work, &count);
//sort files
//call function
//call output function
OUTPUT(work, &count);
//end program
return 0;
}
input.c
Code:
#include "header.h"
void INPUT(INFORMATION *work[], int *count)
{
//initiate struct pointer and a blank sting for reading position]
char location[25];
*count=0;
INFOPTR INFO;
//while loop to get persons information and move it into a malloc array via pointers
while ((gets(location)!=NULL)&&(*count<50)) {
INFO=(INFOPTR)malloc(sizeof(INFORMATION));
strcpy(INFO->name,location);
getchar();
gets(INFO->street);
getchar();
gets(INFO->state);
getchar();
gets(INFO->zip);
getchar();
//counter for array position
work[(*count)++]=INFO;
}
return;
}
output.c
Code:
#include "header.h"
void OUTPUT(INFORMATION *work[], int *count)
{
int i=0;
for (i=0;i<(*count);i++) {
printf("%s\n%s\n%s\n%s\n", work[i]->name, work[i]->street, work[i]->state, work[i]->zip);
}
}
And here is my input:
A1, A2
20294 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
And here is what I get back:
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364B1, B2
B1, B2
19831 Henshaw St
Culver City, CA
94023C1, C2
C1, C2
5142 Dumont Pl
Azusa, CA
91112D1, D2
D1, D2
20636 De Forest St
Woodland Hills, CA
91364A1, A2
A1, A2
20294 Lorenzana Dr
Woodland Hills, CA
91364E1, E2
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364F1, F2
F1, F2
20225 Lorenzana Dr
Los Angeles, CA
91111G1, G2
G1, G2
20253 Lorenzana Dr
Los Angeles, CA
90005H1, H2
H1, H2
5241 Del Moreno Dr
Los Angeles, CA
91110I1, I2
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135J1, J2
J1, J2
5135 Quakertown Ave
Thousand Oaks, CA
91362K1, K2
K1, K2
720 Eucalyptus Ave 105
Inglewood, CA
89030L1, L2
L1, L2
5021 Dumont Pl
Woodland Hills, CA
91364M1, M2
M1, M2
4819 Quedo Pl
Westlake Village, CA
91362I1, I2
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135I1, I2
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135N1, N2
N1, N2
20044 Wells Dr
Beverly Hills, CA
90210O1, O2
O1, O2
7659 Mckinley Ave
Los Angeles, CA
90001
I have not started the sorting code because I cannot get past this, but once I have proper zip codes I am sure I can make a sort function no problem.
I am using xcode with some breaks to read variables as various points and do not notice anything wrong until it makes it to the output functions, although this page briefly pops up between input and output functions when the breaks are up:
Any help will be much appreciated...