Array of Objects(Shop Items), how to set a price tag for each?

Hello guys. I'm pretty new at C++. I have to make this Shop-type of program, by using Array of Objects. Problem is i can't come with idea for 2 of the functions.
- to set price tag for each item;
- to search item by name;
I am looking to implement some ideas that could work... (LINES 56-57)

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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

class MyItems
{
public:
	string item;
	bool state;
	int price;

	MyItems() //Constructor without arguments
	{
		item = "";
		state = true;
		price = 0;
	}
	void read()
	{

		cout << item << endl;
		if (state)
		{
			(this + 1)->read();
		}
	}
};

void showListItems() {
	
	const int n = 10;
	string itemsArray[n] = { "Sword","Axe","Bow","Quiver","Armour","Gauntlets","Helmet","Spear","Dagger","Crossbow" };

	MyItems items[n];
	items[n - 1].state = false;
	cout << "The current items available in our Shop are: " << endl << endl;
	for (int k = 0; k<n; k++)
	{
		items[k].item = itemsArray[k];
	}
	items[0].read();
	cout << endl;

}
int main()
{
	int option;

	cout << " ::: WELCOME TO OUR MEDIEVAL SHOP::: " << endl;
	cout << "----------------------------------------------------" << endl << endl;

	cout << ":::MENU:::\n\n";
	cout << "\n 1. Check our full list of available items..."
		<< "\n 2. Search for an item by name..."
		<< "\n 3. Set/Change price of an item..."
		<< "\n 4. Exit..."
		<< "\n *** Make your choice *** "; cin >> option;

	system("cls");

	switch (option)

	{
	case 1: cout << "Our full list of available items: \n\n";
		showListItems();
		break;
	case 2:cout << "Search for an item by name: \n\n";
		// No IDEA
		break;
	case 3:cout << "Set or Change an item's price: \n\n";
		// No IDEA
		break;
	case 4:cout << "Time to say Goodbye! \n";
		break;

		system("pause");
		return 0;
	}
}
Last edited on
You can do a simple linear search using a for loop.
1. loop through each "item"
2. compare the item name with the input string
3. if a match is found then change the value (item.price =)


Does this help? I can go more in detail if needed.
Hi,

I'll be brief and will only use pseudo code to give you some hints for you to work out

for searching for item by name

1
2
3
4
5
//get the name of item user wants to search

//a lovely for loop to scan the array itemsArray[]
           //if the name user entered matches display details/index and get out of loop
           //if there's no match print the required message 



for setting/changing price of item

1
2
3
4
//use previous function to get a item

//if there's no price set set its price (a ctor may help you with default value)
//if the price is set then just update it... this should not be a problem 
Line 26: You're trying to read into the next instance of MyItems. This is VERY UNSAFE. An instance of a class should NOT make assumptions that it is immediately followed by another instance. Yes, you set the state of the last item [n-1] to false, but consider if the items are in a non-sequential container such as a std::set or std::list. What if an instance gets copied? It's no longer in the same place.

Line 36: items is a local array that goes out of scope when showListItems() exists. items belongs in main(). Pass the array as an argument to showListItems().

Line 70: You can't search the array because it is local to showListItems(). As stated above, move the array to main and pass it to a search function. If you find the item, what are you going to do with it? Search() really should be a helper function.

Line 72: Create an updatePrice() function that is a member of your class. Prompt the user for the name of the object and the new price. Use the search function to locate the desired instance and then change the price.

Consider initializing your array from a file that contains object names and prices.

Consider using a std::vector instead of an array. Much safer.
Last edited on
Thank you, guys. I will mark the thread as solved. Cheers.
welcome :)
Topic archived. No new replies allowed.