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
|
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
//Product class
class Purchase {
public:
std::string prdct_name{};
double unit_price;
int count;
};
// customer class
class Order {
public:
std::string cstr_name{};
std::string cstr_data{};
std::string cstr_address{};
std::vector<Purchase> cstr_vp;
};
int main() try
{
// Open file for both reading and writing
std::fstream myFile("my_order.txt", std::fstream::in | std::fstream::out | std::fstream::trunc);
if (!myFile.is_open()) throw("Can't open file! :(");
std::string name, data, address, product_name;
double unit_prc;
int counts;
std::cout << "Enter customer's info: 'name', 'data' and 'address' each followed by enter: ";
std::getline(std::cin, name);
std::getline(std::cin, data);
std::getline(std::cin, address);
std::cout << "Enter customer's purchases: Product 'name', numerical 'unti price' and numerical"
" 'count', each followed by enter: ";
std::getline(std::cin, product_name);
std::cin >> unit_prc >> counts;
// Writing to the file
myFile << name << '\n' << data << '\n' << address << '\n' << product_name
<< ' ' << unit_prc << ' ' << counts << '\n';
Purchase myPurchase(product_name, unit_prc, counts);
std::vector<Purchase> cp;
cp.push_back(myPurchase);
Order myOrder(name, data, address, cp);
// Reading from the file
std::cout << "\nCustomer's info, 'name', 'data' and 'address' is: ";
std::string str;
while (std::getline(myFile, str))
std::cout << str << '\n';
myFile.close();
system("pause");
return 0;
}
catch (std::exception& e) {
std::cerr << e.what() << '\n';
abort();
}
| |