Advertisement
Guest User

Untitled

a guest
May 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. struct Warehouse {
  7.     std::string name;
  8.     double price;
  9.     int quantity;
  10. };
  11. void printInfo(std::vector<Warehouse> arr) {
  12.     for (size_t i = 0; i < arr.size(); i++)
  13.     {
  14.         std::cout << "Name: " << arr[i].name << std::endl;
  15.         std::cout << "Price: " << arr[i].price << std::endl;
  16.         std::cout << "Quantity: " << arr[i].quantity << std::endl << std::endl;
  17.     }
  18. }
  19. void readFile(const std::string fileName, std::vector<Warehouse> &products, std::vector<Warehouse> &deficiency) {
  20.     std::ifstream FILE;
  21.     FILE.open(fileName);
  22.     if (!FILE) {
  23.         std::cout << "Unable to open file";
  24.         exit(1);
  25.     }
  26.     std::string sName;
  27.     double sPrice;
  28.     int sQuantity;
  29.  
  30.     while (FILE >> sName >> sPrice >> sQuantity) {
  31.         Warehouse temp;
  32.         temp.name = sName;
  33.         temp.price = sPrice;
  34.         temp.quantity = sQuantity;
  35.         if (temp.quantity == 0) {
  36.             deficiency.push_back(temp);
  37.         }
  38.         products.push_back(temp);
  39.     }
  40.  
  41.     FILE.close();
  42. }
  43. void writeFile(const std::string fileName, std::vector<Warehouse> deficiency) {
  44.     std::ofstream outData;
  45.     outData.open(fileName);
  46.     if (!outData) {
  47.         std::cout << "Unable to open file";
  48.         exit(1);
  49.     }
  50.     while (deficiency.size() != 0) {
  51.         Warehouse temp = deficiency.back();
  52.         outData << temp.name << std::endl;
  53.         outData << temp.price << std::endl;
  54.         outData << temp.quantity << std::endl;
  55.         deficiency.pop_back();
  56.     }
  57.     outData.close();
  58. }
  59. int main(int argc, char* argv[]) {
  60.     std::vector<Warehouse> products;
  61.     std::vector<Warehouse> deficiency;
  62.     readFile(argv[1], products, deficiency);
  63.     printInfo(products);
  64.     writeFile("out.txt", deficiency);
  65.     std::cin.get();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement