Print map - help


I dont get it how can i print this map to look like Invoice with stock object in it

I have to make class Invoice witch must contain map with key value class Stock, and value its price. I can't figure out where I'm wrong in printing Invoice with the Stock. I don't know how it can work.


The class Stock - cpp file
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "Stock.h"
#include <iostream>
Stock::Stock() {
	code = 0;
	name = "";
	valuePerUnit = 1;
	numberUnits = 1;
}
Stock::Stock(int c, std::string n, double v, int nu)
{
	code = c;
	name = n;
	valuePerUnit = v;
	numberUnits = nu;
}

Stock& Stock::setCode(int c)
{
	code = c;
	return *this;
}

Stock& Stock::setName(std::string n)
{
	name = n;
	return *this;
}

Stock& Stock::setValuePerUnit(double v)
{
	valuePerUnit = v;
	return *this;
}

 Stock& Stock::setNumberUnits(int n)
{
	numberUnits = n;
	return *this;
}

int Stock::getCode() const
{
	return code;
}

std::string Stock::getName() const 
{
	return name;
}

double Stock::getValuePerUnit() const 
{
	return valuePerUnit;;
}

double Stock::getnumberUnits() const
{
	return numberUnits;;
}

void Stock::input()
{
	std::cout << "\nEnter code : ";
	std::cin >> code;

	std::cout << "Enter name : ";
	std::cin >> name;

	std::cout << "Enter value per unit : ";
	std::cin >> valuePerUnit;

	std::cout << "Enter number of units : ";
	std::cin >> numberUnits;

	return;
}

double Stock::calcAmount() const
{
	return getnumberUnits() * getValuePerUnit();
}

std::ostream& operator << (std::ostream &out, const Stock &s)
{
	out << "\nCode : " << s.getCode();
	out << "\nName : " << s.getName();
	out << "\nNumber Of Units : " << s.getnumberUnits();
	out << "\nCost of Unit : " << s.getValuePerUnit();
	out << "\nTotal : " << s.calcAmount() << std::endl;
	
	return out;
}

std::istream& operator >> (std::istream& in, Stock& s)
{
	std::cout<<"\nEnter Stock Code ";
	in>>s.code;
	
	std::cout<<"Enter Stock Name ";
	in>>s.name;
	
	std::cout<<"Enter value per Unit : ";
	in>>s.valuePerUnit;

	std::cout<<"Enter Number of Units : ";
	in>>s.numberUnits;
	
	return in;
}

std::string Stock::toString() const
{
	std::ostringstream os;
	os.width(4);
	os << "Stock Code: " << code;
	os.width(4);
	os <<" Stock Name: " << name;
	os.width(4);
	os << " Value Per Unit: " << valuePerUnit;
	os.width(4);
	os <<" Number Unit: " << numberUnits;
		
	return os.str();
}

	bool Stock::operator==(const Stock& second)const
	{
	return  code <= second.code;
	}

	bool Stock::operator!=(const Stock& second) const
	{
		return   !(*this == second);
	}

	bool Stock::operator<(const Stock& second) const
	{
		return code < second.code;
	}

	bool Stock::operator>(const Stock& second) const
	{
		return  !(*this < second);
	}

Invoice class - cpp file
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
#include "Invoice.h"
Invoice::Invoice()
{
}


Invoice::Invoice(int _numberInvoice, int _dateIssue, std::map<Stock, double> _goods)
{
	numberInvoice = _numberInvoice;
	dateIssue = _dateIssue;
	goods = _goods;
}

int Invoice::getNumberInvoice() const
{
	return numberInvoice;
}

int Invoice::getDateIssue() const
{
	return dateIssue;
}

std::map<Stock, double> Invoice::getGoods() const
{
	return goods;
}

void Invoice::setNumberInvoice(int _numbInvoice)
{
	numberInvoice = _numbInvoice;
}

void Invoice::setDateIssue(int _dIssue)
{
	dateIssue = _dIssue;
}

void Invoice::setGoods(std::map<Stock, double> _goods)
{
	 goods = _goods;
}


void Invoice::addGood(Stock s, double num)
{
	goods[s] += num;
}
void Invoice::display() const
{
	std::cout << "\nInvoice " << numberInvoice << '\n';
	std::cout << "Date of issue " << dateIssue << '\n';
	std::cout << "Goods:\n";
	std::cout << "Code\t" << "Name\t" << "VPU\t" << "Num\t" << "Value\n";


}


Main invoice
1
2
3
4
Stock s1{ 1589, "Beer", 0.80, 2 };
	Invoice i1{ 2, 1,std::map<Stock, double> { {s1,2.20} } };
	i1.addGood(s1, 2);
	i1.display(); 
This will display the data:

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <map>

class Stock {
public:
	Stock();
	Stock(int c, const std::string& n, double v, int nu);
	Stock& setCode(int c);
	Stock& setName(const std::string& n);
	Stock& setValuePerUnit(double v);
	Stock& setNumberUnits(int n);
	int getCode() const;
	std::string getName() const;
	double getValuePerUnit() const;
	int getnumberUnits() const;
	void input();
	double calcAmount() const;
	std::string toString() const;
	bool operator==(const Stock& second) const;
	bool operator!=(const Stock& second) const;
	bool operator<(const Stock& second) const;
	bool operator>(const Stock& second) const;

	friend std::istream& operator >> (std::istream& in, Stock& s);

private:
	int code {};
	std::string name;
	double valuePerUnit {1};
	int numberUnits {1};
};

Stock::Stock() { }
Stock::Stock(int c, const std::string& n, double v, int nu) : code(c), name(n), valuePerUnit(v), numberUnits(nu) {}

Stock& Stock::setCode(int c)
{
	code = c;
	return *this;
}

Stock& Stock::setName(const std::string& n)
{
	name = n;
	return *this;
}

Stock& Stock::setValuePerUnit(double v)
{
	valuePerUnit = v;
	return *this;
}

Stock& Stock::setNumberUnits(int n)
{
	numberUnits = n;
	return *this;
}

int Stock::getCode() const
{
	return code;
}

std::string Stock::getName() const
{
	return name;
}

double Stock::getValuePerUnit() const
{
	return valuePerUnit;;
}

int Stock::getnumberUnits() const
{
	return numberUnits;;
}

void Stock::input()
{
	std::cout << "\nEnter code : ";
	std::cin >> code;

	std::cout << "Enter name : ";
	std::cin >> name;

	std::cout << "Enter value per unit : ";
	std::cin >> valuePerUnit;

	std::cout << "Enter number of units : ";
	std::cin >> numberUnits;
}

double Stock::calcAmount() const
{
	return getnumberUnits() * getValuePerUnit();
}

std::ostream& operator << (std::ostream& out, const Stock& s)
{
	out << "\nCode : " << s.getCode();
	out << "\nName : " << s.getName();
	out << "\nNumber Of Units : " << s.getnumberUnits();
	out << "\nCost of Unit : " << s.getValuePerUnit();
	out << "\nTotal : " << s.calcAmount() << std::endl;

	return out;
}

std::istream& operator >> (std::istream& in, Stock& s)
{
	std::cout << "\nEnter Stock Code ";
	in >> s.code;

	std::cout << "Enter Stock Name ";
	in >> s.name;

	std::cout << "Enter value per Unit : ";
	in >> s.valuePerUnit;

	std::cout << "Enter Number of Units : ";
	in >> s.numberUnits;

	return in;
}

std::string Stock::toString() const
{
	std::ostringstream os;

	os.width(4);
	os << "Stock Code: " << code;
	os.width(4);
	os << " Stock Name: " << name;
	os.width(4);
	os << " Value Per Unit: " << valuePerUnit;
	os.width(4);
	os << " Number Unit: " << numberUnits;

	return os.str();
}

bool Stock::operator==(const Stock& second)const
{
	return  code <= second.code;
}

bool Stock::operator!=(const Stock& second) const
{
	return   !(*this == second);
}

bool Stock::operator<(const Stock& second) const
{
	return code < second.code;
}

bool Stock::operator>(const Stock& second) const
{
	return  !(*this < second);
}

class Invoice {
public:
	Invoice() {}
	Invoice(int _numberInvoice, int _dateIssue, const std::map<Stock, double>& _goods);
	int getNumberInvoice() const;
	int getDateIssue() const;
	std::map<Stock, double> getGoods() const;
	void setNumberInvoice(int _numbInvoice);
	void setDateIssue(int _dIssue);
	void setGoods(const std::map<Stock, double>& _goods);
	void addGood(const Stock& s, double num);
	void display() const;

private:
	int numberInvoice {};
	int dateIssue {};
	std::map<Stock, double> goods;
};

Invoice::Invoice(int _numberInvoice, int _dateIssue, const std::map<Stock, double>& _goods) : numberInvoice(_numberInvoice), dateIssue(_dateIssue), goods(_goods) {}

int Invoice::getNumberInvoice() const
{
	return numberInvoice;
}

int Invoice::getDateIssue() const
{
	return dateIssue;
}

std::map<Stock, double> Invoice::getGoods() const
{
	return goods;
}

void Invoice::setNumberInvoice(int _numbInvoice)
{
	numberInvoice = _numbInvoice;
}

void Invoice::setDateIssue(int _dIssue)
{
	dateIssue = _dIssue;
}

void Invoice::setGoods(const std::map<Stock, double>& _goods)
{
	goods = _goods;
}


void Invoice::addGood(const Stock& s, double num)
{
	goods[s] += num;
}

void Invoice::display() const
{
	std::cout << "\nInvoice " << numberInvoice << '\n';
	std::cout << "Date of issue " << dateIssue << '\n';
	std::cout << "Goods:\n";
	std::cout << "Code\t" << "Name\t" << "VPU\t" << "Num\t" << "Value\n";

	for (const auto& [stock, val] : getGoods())
		std::cout << stock.getCode() << '\t' << stock.getName() << '\t' <<
			stock.getValuePerUnit() << '\t' << stock.getnumberUnits() << '\t' << val << '\n';
}

int main()
{
	Stock s1 {1589, "Beer", 0.80, 2};
	Invoice i1 {2, 1, std::map<Stock, double> { { s1, 3.30 } }};

	i1.addGood(s1, 2);
	i1.display();
}



Invoice 2
Date of issue 1
Goods:
Code    Name    VPU     Num     Value
1589    Beer    0.8     2       5.3

Thank you but Visual Studio gives me an error here:
1
2
3
4
 for (const auto& [stock, val] : getGoods())
		std::cout << stock.getCode() << '\t' << stock.getName() << '\t' <<
			stock.getValuePerUnit() << '\t' << stock.getnumberUnits() << '\t' << val << '\n';
Leo2505 wrote:
Visual Studio gives me an error here

And the error is?

Please post the EXACT error message you are getting. Sad to say our crystal balls are busted and we ain't mind readers. You have to tell us.
Ops, im sorry my bad
It does not accept [stock, val] parameters
Topic archived. No new replies allowed.