Accessing any element on a list stl c++

How do i access a part of the list if there's no initial value ?

This is the program I'm working with:
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
#include <iostream>
#include <string>
#include <list>
using namespace std;

int main ()
{
	
	string item, desc, remove;
	int num, qty;
	list <string> items;
	list <string> description;
	list <int> quantity;
	list <string> :: iterator it1 = items.begin();
	list <string> :: iterator it2 = description.begin();
	list <int> :: iterator it3 = quantity.begin();
	list <string> :: iterator show;
	list <int> :: iterator showInt;
	
	main:
	system("cls");
	cout << "Inventory Management System\n\n";
	cout << "[0]Exit\n";
	cout << "[1]Add Item\n";
	cout << "[2]Remove Item\n";
	cout << "[3]View an Item\n";
	cout << "[4]View All\n\n";
	cout << "Input: ";
	cin >> num;
	
	switch (num)
	{
		case 0:
			system("cls");
			return 0;
			break;
			
		case 1:
			system("cls");
			cout << "Enter the Item Name: ";
			cin >> item;
			items.push_front(item);
			cout << "Enter the Item Description: ";
			cin >> desc;
			description.push_front(desc);
			cout << "Enter the Item Quantity: ";
			cin >> qty;
			quantity.push_front(qty);
			cout << "\nItem has been added successfully.\n";
			system("pause");
			goto main;
			break;
			
		case 2:
			system("cls");
			cout << "Enter the name of the item you want to remove: ";
			cin >> remove;
			items.remove(remove);
			cout << endl;
			system("pause");
			goto main;
			break;
			
		case 3:
			system("cls");
			cout << "Enter the name of the Item you want to view: ";
		
		case 4:
			system("cls");
			cout << "List of Items\n\n";
			for (show = items.begin(); show != items.end(); show++) {
				cout << "\t" << "Item Name: " << *show << endl;
			}
			cout << endl;
			system("pause");
			goto main;
	}
	
}
Last edited on
How do i access a part of the list if there's no initial value ?

What do you mean? If there's nothing in the list, there's nothing to access.

If the list is empty, items.begin() == items.end().

BTW, trying to maintain multiple parallel lists seems like an error-prone headache. Why not have a single list of structs?
Ah, lets say i ran the program and i have 3 or more element in the list.
Uhm, is it possible to organize the output using a single list ?
What does "organise the output" mean?
Hello kramsuiluj,

Yes it could be done, but it is more work than you need.

"goto" statements are nice, but should be avoided. They can be replaced with a while or do/while loop.

This is one possible solution.
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
98
99
100
101
102
103
104
105
#include <iostream>
#include <limits>  // <--- Added.
#include <string>
#include <list>

using namespace std;

struct Items
{
	Items(std::string itemName, std::string itemDes, int quantity)
	{
		s_itemName = itemName;
		s_itemDes = itemDes;
		s_quantity = quantity;
	}

	std::string s_itemName;
	std::string s_itemDes;
	int s_quantity{};
};

int main()
{
	bool cont{ true };  // <--- Added.
	string item, desc, remove;
	int num, qty;
	list<Items> items; // <--- list of structs.

	// <--- Not worked on these yet.
	//list <string> items;
	//list <string> description;
	//list <int> quantity;
	//list <string> ::iterator it1 = items.begin();
	//list <string> ::iterator it2 = description.begin();
	//list <int> ::iterator it3 = quantity.begin();
	//list <string> ::iterator show;
	//list <int> ::iterator showInt;

//main:

	do
	{
		system("cls");
		cout << "Inventory Management System\n\n";
		cout << "[0]Exit\n";
		cout << "[1]Add Item\n";
		cout << "[2]Remove Item\n";
		cout << "[3]View an Item\n";
		cout << "[4]View All\n\n";
		cout << "Input: ";
		cin >> num;

		switch (num)
		{
			case 0:  // <--- Changed.
				system("cls");
				cont = false;  // <--- Changed. Added.
				break;

			case 1:
				system("cls");
				cout << "Enter the Item Name: ";
				std::getline(std::cin, item);
				//items.push_front(item);
				cout << "Enter the Item Description: ";
				std::getline(std::cin, desc);
				//description.push_front(desc);
				cout << "Enter the Item Quantity: ";
				cin >> qty;
				std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears the input buffer before next "std::getline".
				//quantity.push_front(qty);
				items.emplace_front(item, desc, qty);
				cout << "\nItem has been added successfully.\n\n\n";
				system("pause");
				//goto main;
				break;

			case 2:
				system("cls");
				cout << "Enter the name of the item you want to remove: ";
				cin >> remove;
				//items.remove(remove);
				cout << endl;
				system("pause");
				//goto main;
				break;

			case 3:
				system("cls");
				cout << "Enter the name of the Item you want to view: ";

			case 4:
				system("cls");
				cout << "List of Items\n\n";
				//for (show = items.begin(); show != items.end(); show++) {
				//	cout << "\t" << "Item Name: " << *show << endl;
				//}
				cout << endl;
				system("pause");
				//goto main;
		}  //End switch.
	} while (cont);

	return 0;  // <--- Not required, but makes a good break point.
}

In case 0 the "return" statement is the wrong place to end the program, although it will work. Also because of the "return" the "break" statement is never reached.

In case 1 the use of "std::getline" is a better choice as formatted input of cin >> item; will stop at the first white space leaving whatever is left in the input buffer for the next "std::cin", So, in "Item 1" "item" will be put into the variable "item" leaving the "1" in the input buffer for cin >> desc; to extract.

For now this is as far as I have progressed. First I like to get the code that populates the list, or whatever it may be, working so that I have something to work with for the rest of the program.

Hope that helps,

Andy
Topic archived. No new replies allowed.