Coders gtfih

FunnyVALENTINE

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.


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:
  • Hmm...
Reactions: Gudliferr
1782205052501

atleast use this
 
From a professional standpoint rate this code out of a 10.

This is an inventory i made.


#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::eek:fstream 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 == "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 == "Name: "+jt_item)
{
lines.erase(lines.begin() + i);

if(i < lines.size())
{
lines.erase(lines.begin() + i);
}
break;
}

}

}
std::ifstream 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;
}
print("hello") World
 
I ain't slaving for free niggah
 

Similar threads

FunnyVALENTINE
Replies
21
Views
95
User28823
User28823
FunnyVALENTINE
Replies
0
Views
25
FunnyVALENTINE
FunnyVALENTINE
FunnyVALENTINE
Replies
16
Views
79
FunnyVALENTINE
FunnyVALENTINE
infraorbidal
Replies
1
Views
21
MrCrig
MrCrig
eyes33
Replies
9
Views
47
dogedogedoge
dogedogedoge

Users who are viewing this thread

Back
Top