arrange contacts through zipcodes with malloc, array of pointer to struct

so i just started on this, got one week to finish.
and i am absolutly lost.

typedef struct info a;



#include "stdio.h"

struct info {
char name[20];
char address[40];
char citystate[20];
float zip[5];
};

int main()

{
int total=50,i;
info a[50];

for ( i = 0; i <total; i++){

scanf("%s", a[i].name);
scanf ("%s", a[i].address);
scanf ("%s", a[i].citystate);
scanf ("%d", &(a[i].zip));
printf("%s", a[i].name);// folowing 4 lines are added to display the data.
printf ("%s", a[i].address);
printf ("%s", a[i].citystate);
printf ("%d", (a[i].zip));
}
return 0;
}

it wont even read properly.
the we have a text file, that is supposed to open through ioredirection **zipcode.exe<contacts.txt<contactsdone.txt**

but, it wont pickup on the the correct format

its

Mark Ecko
12345 graphi st
los angeles, ca
91345

instead it takes, mark as name, ecko as full address. 12345 as stateandcity, graphi as zip.

and theres like 50 contacts, so i have the for loop there
Have you tried C++ I/O methods rather than C methods?
It is because you are using scanf() to read a line of text. Use fgets() instead.
http://www.cplusplus.com/reference/clibrary/cstdio/fgets/
(You will have to strip the newline from the end of the string you read. Don't use gets().)

You should write yourself a function that reads and returns an info struct. Its prototype should look something like:

struct info read_info( FILE* stream );

And use it like:

a[ i ] = read_info( stdin );

Likewise, you can write a function that prints a properly-formatted info struct.


Other stuff
In C, struct type names must be preceded by the word 'struct'. So the second line of your main() function's body should read:

struct info a[50];


Your struct itself has a very weird definition for zip-code. I think you want:
1
2
3
4
5
6
struct info {
  char name[20];
  char address[40];
  char citystate[20];
  unsigned zip;        /* <-- notice how I changed this to a single integer */
  };

When you read_info() the zip code, first fgets() it into a char[], then use sscanf() or atoi() or some like function to convert it into an integer.


Finally, make sure you are using the correct indirections at the command-line:

zipcode.exe < contacts.txt > contactsdone.txt


Hope this helps.

[edit] kbw -- He is obviously writing a C program; why suggest C++ stuff?
Last edited on
@duoas: This is a C++ forum. That code could compile with a c++ compiler even though it looks like C code. I've seen many beginners writing code that looks more like C but that doesn't mean that they are using a C compiler or that they can't use C++ I/O. Maybe they just don't know. We have no idea unless the OP tells us.
In addition, scanf is so flaky, I'd discourage anyone from using it. There's always a better way (even in C).
You seem pretty cavalier in defining the terms of use for this forum.

The reality is that people post C problems in C++ forums. (Google will find C++ forums when you search for C programming forums.) Don't assume anything that isn't there. His complete program is pure C. It is quite a leap to assume he is compiling it as C++. (C++ compiles C code so that you can use C code in C++ programs -- not to replace C compilers.)


One common affliction I find on forums is that when someone is attempting to do something using one methodology, posters suggest completely different courses of action instead of endeavoring to help or repair the current methodolgy. Since the OP is using C code, help him with C code -- don't suggest he get a better C.

Hence, since we have no idea unless the OP tells us, just stick with what the OP has told us.

That's all I meant -- and nothing more.

makneltek
BTW, I just noticed that first "typedef" line. That declares a new type alias "a" == "struct info". That probably isn't what you meant.

kbw
Agreed.
Last edited on
yeah im doing C but im ubsing visual c++ express.


i dont understand why i need to change zip into a single integer, zip is 5 integers. and i need to later compare those 5 integers.

also, as for fgets i need to declare a file to open and stream from. but my program cannot have a specified
pFile = fopen ("myfile.txt" , "r");

file name, it has to work with diiferent file names that are going to be input into the program through ioredirction, so can i do *.txt?

i just got home, so i will start to modify it with the new suggesstions hopefully i can atleast get it reading correctly today.

duoas
yea i did want the a == struct info.

will this be a problem later, isnt it essentially the same thing, just shorter writing.
Last edited on
closed account (z05DSL3A)
i dont understand why i need to change zip into a single integer, zip is 5 integers. and i need to later compare those 5 integers.

The zip code is 5 digits, if you store the digits in one integer they will be easy to sort and compare.

1
2
3
4
int zip1 = 56342;
int zip2 = 93624;

if (zip1 > zip2)...
ok i have this now, thanks so some feedback

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>

const int TOTAL = 10;

struct info {
	char name[60];
	char address[60];
	char citystate[60];
	char zip[6];
};

struct info a;
void print_out(struct info *p);

int main()

{
	struct info *strptr;
	strptr=&a;
	int total=TOTAL,i;
	info a[TOTAL];


	for ( i = 0; i <total; i++){

 _flushall();
fgets(a[i].name, 60, stdin);
 _flushall();
fgets (a[i].address, 60, stdin);
 _flushall();
fgets (a[i].citystate, 60, stdin);
 _flushall();
fgets (a[i].zip, 6, stdin);
 _flushall();
 
	}

print_out(a);
	return 0;
}
void print_out(struct info *p)
{ int i, total=TOTAL;
	for ( i = 0; i < total; i++)
	{
printf("%s", p[i].name);// folowing 4 lines are added to display the data.
printf ("%s", p[i].address);
printf ("%s", p[i].citystate);
printf ("%s", p[i].zip);
//printf ("\n");
	}
}


while keyboard input works great, .txt redirection is horrible.

it takes the first line and after that just puts the funny looking "I" for a loooong time.
why is it doing this!?!?

also tried writing a function for input, but i cant assign a file name, becuase it must work with random input files sent through dos redirection
You need to pay a little bit more attention to the help given you.

First, I warned you about fgets() leaving the newline in the string you get. You need to get rid of it -- it is the reason your program is failing you.

Since people are so rarely instructed how to get proper input, here is some significant help: use this function instead of fgets().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <string.h>

/* getline()
 *   Read an entire line of text from a given FILE into a char[].
 *   Only the first (size-1) characters of the line are stored -- the rest  
 *   are lost.
 */
void getline(char *result, unsigned size, FILE *stream)
{
	int c;
	fgets(result, size, stream);

	/* either find and get rid of the newline */
	result=strchr(result, '\n');
	if (result) *result = '\0';

	else	/* or skip more input until the newline is read (or EOF) */
		do c=fgetc(stream);
		while ((c!='\n') && (c!=EOF));
}
Make sure to let your professor know that you didn't make this function up yourself -- that you got it online. If you don't he might fail you. However, make sure also that you learn how and why this function works, and why it is the safe way to get input.

Once you have done that, you can get rid of those calls to _flushall() -- they weren't helping you anyway. The first time you called it, all the redirected input was lost. That's why things worked great from the keyboard but not from file. At the keyboard, flushing everything works fine since the keyboard stream wasn't closed and there was no pending input until you typed it. From file, the entire file was pending input -- and was flushed the first time you called flushall.


Lines 13, 19, and 20 you don't need. Please get rid of them.
Line 22 should have caused your compiler to stop. Make sure you turn on all warnings when compiling.

You can use a typedef to avoid typing 'struct' all the time. The common way to do it is like this:
1
2
3
4
5
6
7
struct info {
	char name[60];
	char address[60];
	char citystate[60];
	char zip[6];
};
typedef  struct info  info; 
After that, you can use just the word 'info' anywhere to mean 'struct info', thus making line 22 correct.

You don't need to create a separate 'total' variable -- just use 'TOTAL' directly.


Finally, your main function should look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* Our prototypes */
info read_info();
void print_info(info p);

int main()
{
	int  i;
	info a[TOTAL];

	/* Load the file of postal information */
	for (i=0; i<TOTAL; i++){

		a[i]=read_info();

	}

	/* Print it all back to see that it worked */
	for (i=0; i<TOTAL; i++) {

		print_info(a[i]);

	}
	return 0;
}

The read_info() function reads and returns a single info structure.
Likewise, the print_info() function takes and prints a single info structure.

Hope this helps.
a[i]=read_info();


read_info()..

i cant set paramenters, it tells me it doesnt take, 0, 1, 2, 3.

i actually got it working with the stdin-1 as part of the fgets()

its printing fine, i now need to sort by zipcodes. i made a code, that i saw online about swapping arrrays, i tried it with mine, but it didnt pickup.

i will probably post it later, or keep working on it tommorow, to see if i can make it work.
ok well i read many examples on sorting, and the code im using is exchange sort but mine isnt working well. i used the best way that i see it making sence, but it wont compile. they all say error C2106: '=' : left operand must be l-value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int TOTAL = 3;//27

struct info {
	char name[10];
	char address[30];
	char citystate[30];
	char zip[6];
};

struct info a;
void print_out(struct info *p);

int main()

{
	struct info *strptr;
	strptr=&a;
	int total=TOTAL,i;
	info a[TOTAL];//will be gone

	info *spc; // need to be used sometime
	spc = (info*)malloc (sizeof(info)); //i need to use this at some point, not sure where yet.

	for ( i = 0; i <total; i++){

fgets(a[i].name, sizeof(a[i].name-1), stdin); //taking off the \n :)

fgets (a[i].address, sizeof(a[i].address-1), stdin);

fgets (a[i].citystate, sizeof(a[i].citystate-1), stdin);

fgets (a[i].zip, sizeof(a[i].zip-1), stdin);
	
	}

	int t;
	for (t=0; t<TOTAL; t++)
	{
		int i, total=TOTAL;
		for ( i = t; i < total; i++)
		{
			if ((a[i].zip)>(a[t].zip))
			{
				char temp[6];
				temp=a[i].zip; //error C2106: '=' : //left operand must be l-value
				a[i].zip = a[t].zip; // left operand myst be 1 value
				a[t].zip = temp; // left operand must be 1 value
			}
		}
	}
	print_out(a);
	return 0;
}
void print_out(struct info *p)
{ int i, total=TOTAL;
	for ( i = 0; i < total; i++)
	{
printf("%s", p[i].name);// folowing 4 lines are added to display the data.
printf ("%s", p[i].address);
printf ("%s", p[i].citystate);
printf ("%s", p[i].zip);
//printf ("\n");
	}
}
Last edited on
well i got it done, i just need to break into 2.c and 1.h, or something like that. and need to find out where or how to use malloc.

anyways im posting it for anyone else who might need it in the future or whatever.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
This programs takes input through ioredirection with the structure of
name
address
citystate
zip
and arranges them by zipcode
MAKNELTEK
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const int TOTAL = 18;
struct info {
	char name[30];
	char address[30];
	char citystate[30];
	char zip[30];
}a;

void get_info(struct info *a);
void print_out(struct info *p);
void sortdata( struct info *a);
int main()

{
	//struct info *strptr;
	//strptr=&a;
	info a[TOTAL];
	info *spc; // need to be used sometime
	spc = (info*)malloc (sizeof(info)); //i need to use this at some point, not sure where yet.
	get_info(a);
	sortdata(a);
	print_out(a);
	return 0;
}
void get_info(info *a)
{
	char *p;
	for ( int i = 0; i <TOTAL; i++){
		fgets(a[i].name, sizeof(a[i].name), stdin);
		if ((p = strchr(a[i].name, '\n')) != (NULL))
			*p = '\0';
		fgets (a[i].address, sizeof(a[i].address), stdin);
		if ((p = strchr(a[i].address, '\n')) != (NULL))
			*p = '\0';
		fgets (a[i].citystate, sizeof(a[i].citystate), stdin);
		if ((p = strchr(a[i].citystate, '\n')) != (NULL))
			*p = '\0';
		fgets (a[i].zip, sizeof(a[i].zip),stdin);
		if ((p = strchr(a[i].zip, '\n')) != (NULL))
			*p = '\0';
	}
}
void print_out(struct info *p)
{ 
	int i, total=TOTAL;
	for ( i = 0; i < total; i++)
	{
		printf("%s", p[i].name);// folowing 4 lines are added to display the data.
	printf("\n");
	printf ("%s", p[i].address);
	printf("\n");
	printf ("%s", p[i].citystate);
	printf("\n");
	printf ("%s", p[i].zip);
	printf ("\n");
	}
}

void sortdata(info *a)
{	int t;
	for (t=0; t<TOTAL; t++)
	{
		int i, total=TOTAL;
		for ( i = t; i < total; i++)
		{
			if (-1==(strcmp(a[i].zip,a[t].zip)))
			{
				char temp[30];
				memcpy(temp, a[i].zip, sizeof(a[i].zip));
				memcpy(a[i].zip, a[t].zip, sizeof(a[t].zip));
				memcpy(a[t].zip, temp, sizeof(temp));
				memcpy(temp, a[i].name, sizeof(a[i].name));
				memcpy(a[i].name, a[t].name, sizeof(a[t].name));
				memcpy(a[t].name, temp, sizeof(temp));
				memcpy(temp, a[i].address, sizeof(a[i].address));
				memcpy(a[i].address, a[t].address, sizeof(a[t].address));
				memcpy(a[t].address, temp, sizeof(temp));
				memcpy(temp, a[i].citystate, sizeof(a[i].citystate));
				memcpy(a[i].citystate, a[t].citystate, sizeof(a[t].citystate));
				memcpy(a[t].citystate, temp, sizeof(temp));
			}
		}
	}
}



thanks a lot to Duoas who kinda got impatient with me in the end.
but it took me a while to understand the \n command and its effects throught ioredirection and fgets.

now... anyone know any good websites about linking .c and .h files to finish this off.
Last edited on
Topic archived. No new replies allowed.