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 <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
struct Tools {
std::string code;
std::string name;
size_t qty {};
};
int main()
{
std::fstream inFile("tools.txt");
if (!inFile)
return (std::cout << "Error opening file.\n"), 1;
std::vector<Tools> tools;
for (Tools tool; std::getline(inFile >> std::ws, tool.code, ',') && std::getline(inFile, tool.name, ',') && inFile >> tool.qty; tools.push_back(tool));
bool again {};
do {
std::string code;
size_t qty {};
std::cout << "Enter tool code: ";
std::getline(std::cin, code);
if (const auto itr {std::find_if(tools.begin(), tools.end(), [code](const auto& t) {return t.code == code; })}; itr != tools.end()) {
std::cout << itr->name << " " << itr->qty << '\n';
if (itr->qty > 0) {
std::cout << "Enter quantity to check out: ";
std::cin >> qty;
if (qty > itr->qty)
std::cout << "Only " << itr->qty << " can be checked out\n";
itr->qty -= std::min(qty, itr->qty);
}
} else
std::cout << "Code not found\n";
std::cout << "Again (1 yes, 0 no): ";
std::cin >> again;
std::cin.ignore();
} while (again);
inFile.clear();
inFile.seekp(0);
for (const auto& [code, name, qty] : tools)
inFile << code << ',' << name << ", " << qty << '\n';
}
|