include iostream include string using namespace std union _weight int

 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
#include <iostream>
#include <string>
using namespace std;
union _weight {
int decim;
double floating;
void init() {
cin >> floating;
if ((int) floating == floating) {
decim = (int) floating;
}
}
double get() {
if (floating != NULL) return floating;
return decim;
}
};
struct products {
string name;
int price;
string shop;
_weight weight;
};
int main() {
products product;
cout << "Enter name: ";
cin >> product.name;
cout << "Enter price: ";
cin >> product.price;
cout << "Enter shop name: ";
cin >> product.shop;
cout << "Enter weight: ";
product.weight.init();
cout << "Entered data" << endl;
cout << "name = " << product.name << "; price = " << product.price << "; shop name = " << product.shop
<< "; weight = " << product.weight.get() << ";";
return 0;
}