Help with array of structs

Hello all,

I am creating a program for a final class project and I have come across an issue.
I have an array of structs and I am currently working on a function that would allow the user to add another struct to the array. However, when trying to use the Add function, the program will stop working if I add a number for the input on the first line but if I add a character, it continues to work. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

void Add(Inv Catalog[], int CatalogCount)
{
	int X;
	X=CatalogCount+1;

	printf("Item Number:\n");
	scanf("%d", Catalog[X].ItemNumber);
	printf("Brand:\n");
	scanf("%s", Catalog[X].Brand);
	printf("Object:\n");
	scanf("%s", Catalog[X].Object);
	printf("Quantity:\n");
	scanf("%d", Catalog[X].Quantity);
	printf("Cost:\n");
	scanf("%f", Catalog[X].Cost);
	printf("Price:\n");
	scanf("%f", Catalog[X].Price);

	return;
}


CatalogCount is supposed to keep track of the current number of structs in the array. I would really appreciate any type of help as this project is going to be a rather large part of my grade. I understand that some people might recommend std::vector(I think that's it!) but I do not understand how to use them quite yet (we didn't cover that section) and with the project being due soon, I am trying to put something like that off until after the semester when I have more time to grasp the concept. Thanks all!
Show us how you are initializing the array. I suspect you are not expanding the dynamic memory you've allocated for it (you are using dynamic memory, are you not?) and just writing off into nowhere.
Here is the entire code(under construction):

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

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

#define ArrSize 25

typedef struct Store{
	char Brand[ArrSize];
	char Object[ArrSize];
	int ItemNumber;
	int Quantity;
	double Cost;
	double Price;
}Inv;

void PrintCatalog(Inv Catalog[], int CatalogCount);
void Add(Inv Catalog[], int CatalogCount);

int main()
{
	Inv Catalog[ArrSize]; int CatalogCount=6;

	Catalog[0].ItemNumber=101;
	strcpy(Catalog[0].Brand, "Nike");
	strcpy(Catalog[0].Object, "Shoes");
	Catalog[0].Quantity=4;
	Catalog[0].Cost=39.99;
	Catalog[0].Price=59.99;

	Catalog[1].ItemNumber=102;
	strcpy(Catalog[1].Brand, "Nike1");
	strcpy(Catalog[1].Object, "Shoes1");
	Catalog[1].Quantity=4;
	Catalog[1].Cost=39.99;
	Catalog[1].Price=59.99;

	Catalog[2].ItemNumber=103;
	strcpy(Catalog[2].Brand, "Nike2");
	strcpy(Catalog[2].Object, "Shoes2");
	Catalog[2].Quantity=4;
	Catalog[2].Cost=39.99;
	Catalog[2].Price=59.99;

	Catalog[3].ItemNumber=104;
	strcpy(Catalog[3].Brand, "Nike3");
	strcpy(Catalog[3].Object, "Shoes3");
	Catalog[3].Quantity=4;
	Catalog[3].Cost=39.99;
	Catalog[3].Price=59.99;

	Catalog[4].ItemNumber=105;
	strcpy(Catalog[4].Brand, "Nike4");
	strcpy(Catalog[4].Object, "Shoes4");
	Catalog[4].Quantity=4;
	Catalog[4].Cost=39.99;
	Catalog[4].Price=59.99;

	Catalog[5].ItemNumber=106;
	strcpy(Catalog[5].Brand, "Nike5");
	strcpy(Catalog[5].Object, "Shoes5");
	Catalog[5].Quantity=4;
	Catalog[5].Cost=39.99;
	Catalog[5].Price=59.99;

	//PrintCatalog(Catalog, CatalogCount);

	Add(Catalog, CatalogCount);

	printf("%d\n", Catalog[6].ItemNumber);

	return 0;
}

void Add(Inv Catalog[], int CatalogCount)
{
	int X;
	X=CatalogCount+1;

	printf("Item Number:\n");
	scanf("%d", Catalog[X].ItemNumber);
	printf("Brand:\n");
	scanf("%s", Catalog[X].Brand);
	printf("Object:\n");
	scanf("%s", Catalog[X].Object);
	printf("Quantity:\n");
	scanf("%d", Catalog[X].Quantity);
	printf("Cost:\n");
	scanf("%f", Catalog[X].Cost);
	printf("Price:\n");
	scanf("%f", Catalog[X].Price);

	return;
}


We are not supposed to be required to use dyn mem right now as that was not covered before the project was introduced to us and is actually an extra credit question for the final. The array is supposed to max out at 25. Like I said, when I add a character the Add function will go to the next section (Brand) but a number will cause the program to stop working.

Edit: Also, I have hard coded six entries into the array to begin with.
Last edited on
scanf takes pointers to where you want to store the data, not the variables themselves. Also, you need to use %lf to store a double, not %f, etc.

http://www.cplusplus.com/reference/cstdio/scanf/?kw=scanf
If your program isn't working it's probably because you're writing to an invalid address that happens to be the result of casting the numbers you've given to scanf.
I am not sure what you are saying. I find it strange that when I enter a character the program continues to run, but if it is a number it stops working. What should that be indicating?
That indicates the character happened to be an address you can write to without the processor throwing an error.

Really randomish behaviour is often the result of either accessing uninitialised memory or memory that doesn't belong to you (the latter will also cause a lot of crashes).
Do you even know what a pointer is?

http://www.cplusplus.com/doc/tutorial/pointers/
Topic archived. No new replies allowed.