1

The purpose of my program is to take in user input by io redirection, sort it, and output without duplicate records. All this using an array of pointers to structs dynamically allocated. Im having a little bit of trouble with the output. It repeats the last line multiple most likely as more structs. Thanks in advance.

functions file

#include "lab67h.h"


void getinput(address *temp[], int *s)
{
    char c[25];

    gets(c);
    while (c[0] != '\0' && *s < size)
        {
        temp[*s] = (address *) malloc(sizeof(address));

        strcpy(temp[*s]->name, c);
        gets(c);
        strcpy(temp[*s]->street, c);
        gets(c);
        strcpy(temp[*s]->city, c); 
        gets(c);
        strcpy(temp[*s]->zipcode, c); 

        (*s)++;
        gets(c); 
        }
}
void sort(address *temp[], int s)
{
    address *tempi;

    for (int a = 0; a <= s; a++)
        {
        for (int b = 0; b < (s - 1); b++)
            {
            if (convert(temp, b) > convert(temp, b + 1))
                {
                tempi = temp[b];
                temp[b] = temp[b + 1];
                temp[b + 1] = tempi;
                }
            }
        }
}
int convert(address *tempp[], int c)
{
    int i = -1, num = 0;
    char str[10];
    strcpy(str, &tempp[c]->zipcode);
    while (str[++i] != '\0')
        num = num * 10 + (str[i] - '0');
    return num;
}
void output(address *temp[], int s)
{
    for (int i = 0; i < s; i++)
    {
        puts(temp[i]->name);
        puts(temp[i]->street);
        puts(temp[i]->city);
        puts(temp[i]->zipcode);

        while (temp[i]->name == temp[i + 1]->name && temp[i]->street == temp[i + 1]->street && temp[i]->city == temp[i + 1]->city && temp[i]->zipcode == temp[i + 1]->zipcode)
        {
            free(temp[i]);
            i++;
        }
        free(temp[i]);
        i++;

    }
}

main file

#include "lab67h.h"

int  main(void)
{
    int a = 0;
    address *p[size];

    getinput(p, &a);
    sort(p, a);
    output(p, a);

    system("pause");
    return 0;
}

header file

#ifndef LAB67H_H_INCLUDED
#define LAB67H_H_INCLUDED
#define size 50
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

typedef struct
{
    char name[25];
    char street[25];
    char city[25];
    char zipcode[25];

} address;

void getinput(address*[], int*);
void sort(address*[], int);
int convert(address*[], int);
void output(address*[], int);

#endif

input file

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

Output I'm getting

Press any key to continue . . . 
K1, K2
720 Eucalyptus Ave 105 
Inglewood, CA
89030
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
90001
G1, G2
20253 Lorenzana Dr
Los Angeles, CA
90005  
H1, H2
5241 Del Moreno Dr
Los Angeles, CA
91110
C1, C2
5142 Dumont Pl
Azusa, CA
91112
M1, M2
4819 Quedo Pl
Westlake Village, CA
913 62
D1, D2
20636 De Forest St
Woodland Hills, CA
91364
E1, E2
4851 Poe Ave
Woodland Hills, CA
91364
B1, B2
19831 Henshaw St
Culver City, CA
94023
I1, I2
5332 Felice Pl
Stevenson Ranch, CA
94135
  • it isn't showing all the lines... – user2736738 Dec 15 '15 at 04:26
  • I would suggest you have not exhausted your debugging options. And that should always be done before turning to SO. For example, have you printed out the data before calling `sort` to see whether it was ok at that point? Have you tried with a smaller data set and stepped through the execution with a debugger? Debugging is a valuable skill to learn and gain experience in and should not be lightly outsourced to SO. – kaylum Dec 15 '15 at 04:34
  • See [Why the `gets()` function is so dangerous it should never be used?](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) for a discussion on why `gets()` should not be used. – Jonathan Leffler Dec 15 '15 at 04:42
  • Thanks you both for your replies. I realized it wasn't showing all the lines even after deleting duplicates. I tried using smaller data and yes it seemed to work but when I tried your first option, I got a similar output to one on my post. It seems the error is in the input function. What do you suggest I do from here? Debugging with small data shows it works. – John Caliski Dec 15 '15 at 04:44

2 Answers2

1

Your code loops because you don't test whether the code has reached EOF.

See Why the gets() function is so dangerous it should never be used? for a discussion on why gets() should not be used.

You need something more like:

char c[25];

while (fgets(c, sizeof(c), stdin) != NULL && c[0] != '\0' && *s < size)
{
}

You would also need to use, and test the result of, fgets() in the body of the loop. fgets() returns NULL when it detects EOF or an error. (See also while (!feof(file)) is always wrong for a discussion on why the code does not use, or need to use, feof().) You could avoid an extra copy operation by reading directly into the elements of the structure. You will need to decide how to remove the newline that fgets() keeps and gets() does not. You also need to consider what to do if a line is too long — perhaps discard the excess. That would be better handled by a function called from within your loop.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

Your input code is run size number of times, populating the remaining structs with the last read value (90001). Find a way to stop the while loop when the input data has run out.

Dylan Kirkby
  • 1,427
  • 11
  • 19