FunnyVALENTINE
STAND USER
- Joined
- Apr 19, 2026
- Posts
- 1,324
- Reputation
- 1,166
From a professional standpoint rate this code out of a 10.
This is an inventory i made.
This is an inventory i made.
Code:
#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>
#include<cctype>
#include<filesystem>
#include<algorithm>
#include<random>
#include<bitset>
#include<set>
#include<string_view>
#include<memory>
#include<cmath>
#include<fstream>
using std::vector;
using std::string;
using std::unordered_map;
using std::bitset;
using std::cout;
using std::cin;
using std::cerr;
bool item_exists{false};
bool existingItemupdate(const string & name, int &quantity);
int new_quantity{};
class Item
{
public:
string name{};
int quantity{};
void add()
{
cout<<"Enter the name of the item: ";
cin>>name;
if(existingItemupdate(name,quantity))
{
cout<<"Item found\n";
new_quantity = quantity;
}
else
{
cout<<"Enter the quantity of item: ";
cin>>quantity;
}
}
};
class Inventory
{
public:
vector<Item> arr{};
};
int main()
{
Item a;
a.add();
Inventory b;
b.arr.push_back(a);
std::ofstream abc{"database.txt", std::ios::app};
if(!abc)
{
cerr<<"Error cannot open file abc for writing\n";
}
for(const Item& a: b.arr)
{
if(item_exists == false)
{
abc<<"Name: "<<a.name<<'\n';
abc<<"Quantity: "<<a.quantity<<'\n';
}
abc.close();
vector<string> lines{};
string line{};
std::ifstream edf{"database.txt"};
while(std::getline(edf, line))
{
lines.push_back(line);
}
for(int i = 0 ; i < lines.size(); i++)
{
if (lines[i] == "Name: "+a.name)
{
lines[i + 1] = "Quantity: " + std::to_string(a.quantity);
}
}
string yesNo{};
cout<<"Want to erase a certain item: ";
cin>>yesNo;
if(yesNo == "yes")
{
string jt_item{};
cout<<"type the name of the item: ";
cin>>jt_item;
for(int i = 0; i < lines.size() ; i++)
{
if (lines[i] == "Name: "+jt_item)
{
lines.erase(lines.begin() + i);
if(i < lines.size())
{
lines.erase(lines.begin() + i);
}
break;
}
}
}
std::ofstream tyu{"database.txt"};
for(const string& s : lines)
{
tyu << s << '\n';
}
}
return 0;
}
bool existingItemupdate(const string & name, int &quantity)
{
string existing_name{};
std::ifstream def{"database.txt"};
while(std::getline(def, existing_name))
{
if(existing_name == "Name: "+name)
{
item_exists = true;
cout<<"Enter number to update the quantity of item: ";
cin>>quantity;
return true;
}
}
return false;
}
Last edited: