Я новичок и пытаюсь составить программу инвентаризации для школьного проекта, но мои знания c ++ очень скудны.Как я могу обновить определенное поле данных / удалить определенное поле данных, если хранилище данных в моем текстовом файле разделено запятыми?Это код, который я получил до сих пор.
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <sstream>
using namespace std;
string ItemID,
description,
name,
category,
manufacturer,
SellingPrice,
cost,
StoreUnit,
UnitSold,
year,
month,
day;
int choice=0;
void mainmenu();
void adddata();
void savedata();
void updatedata();
float txtline(int);
void searchdata();
void showdata();
vector<string> split(string strToSplit, char delimeter)
{
stringstream ss(strToSplit);
string item;
vector<std::string> splittedStrings;
while (getline(ss, item, delimeter))
{
splittedStrings.push_back(item);
}
return splittedStrings;
}
vector <string> list;
//-----------------------------------------------------------------------//
int main()
{
mainmenu();
while (choice==1)
{
adddata();
mainmenu();
}
while (choice==2)
{
updatedata();
}
while (choice==2)
{
updatedata();
}
while (choice==5)
{
showdata();
mainmenu();
}
while (choice==6)
{
return 0;
}
}
void mainmenu()
{
cout << "___________________________________________________\n"
"| |\n"
"| *** Inventory Management Program *** |\n"
"| 1 - Add New Record |\n"
"| 2 - Update Data |\n"
"| 3 - Delete Data field |\n"
"| 4 - Delete Record |\n"
"| 5 - Show Record |\n"
"| 6 - Exit |\n"
"|_________________________________________________|\n"
" Please Select Your Next Choice: ";
cin >> choice;
cout << endl;
}
//-------------------------------------------------------------------------//
void adddata()
{
cout << "*** Inventory Management Program *** \n"
<< endl
<< "Please Enter A New Record:\n";
cout << endl;
cout << "Item ID: ";
cin.ignore();
getline(cin,ItemID);
cout << "Item Name: ";
getline(cin,name);
cout << "Item Description: ";
getline(cin,description);
cout << "Category: ";
getline(cin,category);
cout << "Manufacturer: ";
getline(cin,manufacturer);
cout << "Selling Price: RM";
getline(cin,SellingPrice);
cout << "Cost Price: RM";
getline(cin,cost);
cout << "Units in Store: ";
getline(cin,StoreUnit);
cout << "Units Sold: ";
getline(cin,UnitSold);
cout << "Year of Date First Introduced: ";
getline(cin,year);
cout << "Month of Date First Introduced: ";
getline(cin,month);
cout << "Day of Date First Introduced: ";
getline(cin,day);
savedata();
cout << endl;
cout << "All your data have been successfully stored.";
cout << endl;
}
//------------------------------------------------------------------
void savedata()
{
ofstream file;
file.open("Milestone2.txt", fstream::app);
file << ItemID << " , " << name << " , " << description << " , "
<< category << " , " << manufacturer << " , " << SellingPrice <<
" , " << cost << " , " << StoreUnit << " , " << UnitSold <<
" , " << year << " , " << month << " , " << day <<"\n";
file.close();
}
//---------------------------------------------------------------------------
void showdata()
{
string getcontent;
ifstream openfile ("Milestone2.txt");
if(openfile.is_open())
{
while(! openfile.eof())
{
getline(openfile, getcontent);
cout << getcontent << endl;
}
}
}
Внутри моего файла Milestone2.txt будет что-то вроде этого после ввода значений.
1. 1,1,1,1,1,1,1,1,1,1
2. MU0001, Mr.Rabbit, Plush Toy, Toy, Kids Wonderland, 10.00, 5.00, 100, 33, 2017, 4, 25
Я находил решениячасами, но я не могу понять, как ...