pointer to the array of a struct

Am still pretty new to all this and being a very mature student the old brains cells seem to work much slower!!
My program needs to
1. create a pointer to point to an array of a struct!
2. Get imput from the user for each element and iterate over the array using the pointer and print the results out.
3. print out the total value of each item and then the grand total of the value of all stock.

I believe i have created the array of the strcut correctly! and have just set the array at 2 elements.

I am not sure if the pointer syntax is correct?
Also when the results print out it prints out the results once and then ask for input again and prints out the array results -can someone help explain why and possibly what i can do to amend this - thank Andy
code below


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
  
#include <iostream>

void createItem(int);
void returnsItemDetails( int);
void totalItemsCost(int);

//main function 
int main()

{
	createItem(0);
	returnsItemDetails(0);

	for (int createNewItem = 0; createNewItem < 2 ; createNewItem++)
	{
		createItem(createNewItem);
	}

	for (int createNewItem = 0; createNewItem < 2 ; createNewItem++)
	{
		returnsItemDetails(createNewItem);
	}
	
	totalItemsCost(0);

	
	system ("pause"); 
	
	return 0;
}

struct items
{
	char name[10];
	float cost;
	int stock;

}itemDetails[2];

items* fetchItems = itemDetails;



void createItem( int item)
{
	std::cout << "Enter item name: " << std::endl;
	std::cin >> fetchItems[item].name;
	std::cout << "Enter item cost: " << std::endl;
	std::cin >> fetchItems[item].cost;
	std::cout << "Enter stock quantity: " << std::endl;
	std::cin >> fetchItems[item].stock;

}


void returnsItemDetails( int item)
{
	std::cout << "Item name is: " << fetchItems[item].name;
	std::cout << " |  Item cost is: "<<(char)156 << fetchItems[item].cost;
	std::cout << " |  Item stock qty is: " << fetchItems[item].stock << std::endl;

}

void totalItemsCost(int item)
{
	float item1 = fetchItems[0].cost * fetchItems[0].stock;
	float item2 = fetchItems[1].cost * fetchItems[1].stock;

	std::cout << "item: " << fetchItems[0].name << " Total cost: " << (char)156 << item1 << std::endl;
	std::cout << "item: " << fetchItems[1].name << " Total cost: " << (char)156 << item2 << std::endl;
	std::cout << "Total cost of all items is: " <<(char)156 << item1 + item2 << std::endl;
}
Your loops start at 0, so you don't need lines 12 and 13.
thanks for the reply - all sorted now
Topic archived. No new replies allowed.