Hello All,
I hope everyone is doing well. I have a program that is supposed to accept a list of addresses via input out redirection in the command prompt and then output them organized from lowest to highest in zip-code order. The program has a minimum of two entries and no maximum. Here is what i have below. I'm not getting any errors in VS but when i run the program through the command prompt it seems to crash.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct storeaddress
{
char fandlname[50];
char street[50];
char cityandstate[25];
int zip;
};
struct storeaddress*createaddress();
void printaddr(int ptrct, struct storeaddress*ptr[]);
void sortingzip(int ptrct, struct storeaddress*ptr[]);
int main()
{
struct storeaddress *ptr[50];
int ptrct = 0;
char buf[50];
do
{
ptr[ptrct] = createaddress();
ptrct++;
} while (((fgets(buf, 50, stdin) != NULL) && (ptrct < 3)));
{
sortingzip(ptrct, ptr);
printaddr(ptrct, ptr);
}
}
struct storeaddress*createaddress()
{
struct storeaddress*newaddress = (storeaddress*)malloc(sizeof(struct storeaddress));
fgets(newaddress->fandlname, 50, stdin);
fgets(newaddress->street, 50, stdin);
fgets(newaddress->cityandstate, 50, stdin);
scanf("%d", &newaddress->zip);
return newaddress;
};
void printaddr(int ptrct, struct storeaddress *ptr[50])
{
int j;
for (j = 0; j < ptrct; j++)
{
printf("\n%s\n%s\n%s\n%s\n%d", ptr[j]->fandlname, ptr[j]->street, ptr[j]->cityandstate, ptr[j]->zip);
}
};
void sortingzip(int ptrct, struct storeaddress *ptr[])
{
int i, k;
for (i = 0; i < ptrct + 1; i++)
{
for (k = 0; k < ptrct + 1; k++)
{
if (ptr[k]->zip > ptr[k + 1]->zip)
{
struct storeaddress *temp = ptr[k + 1];
ptr[k + 1] = ptr[k];
ptr[k] = temp;
}
}
}
};
I want to thank you in advance for any help/advice you can offer, i appreciate it.