inventory
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#include
void addItem ();
void searchItem ();
void listItem();
void totalValue();
void saveFile();
void readIn();
using namespace std;
class invItem
{
public:
string name;
string number;
double price;
int quantity;
};
int item=0;
int numberToDisplay;
double total, subtotal;
int main()
{
subtotal=0;
int n;
cout << "1) Add an item to memory" << endl;
cout << "2) Search memory for an item" << endl;
cout << "3) List what's in memory" << endl;
cout << "4) Total value on hand" << endl;
cout << "5) Save to a file" << endl;
cout << "6) Read in from a file" << endl;
do
{
cout << "Please enter a number from 1 to 6:" << endl;
cin >> n;
if (n==1)
addItem ();
else if (n==2)
searchItem ();
else if (n==3)
listItem();
else if (n==4)
totalValue();
else if (n==5)
saveFile();
else if (n==6)
readIn();
cout << "Number of items is: " << item << endl;
}
while (n==1 || n==2 || n==3 || n==4 || n==5 || n==6);
return 0;
}
void addItem ()
{
string name, number;
double price;
int quantity;
invItem someitem;
ofstream invOutFile("invent1.txt", ios::app); /// declare and open for append
cout << "Please enter am item name: " << endl;
cin >> name;
=name;
cout << "Please enter an item number: " << endl;
cin >> number;
someitem.number=number;
cout << "Please enter an item price: " << endl;
cin >> price;
someitem.price=price;
cout << "Please enter an item quantity: " << endl;
cin >> quantity;
someitem.quantity=quantity;
item++;
cout << "Item = " << << " [" << someitem.number <<"]" << endl;
cout << "Quantity = " << someitem.quantity << " Price = " << someitem.price << endl;
ofstream invOutFile2("invent1.txt", ios::app); /// declare and open for append
invOutFile2 << << endl;
invOutFile2 << someitem.number << endl;
invOutFile2 << someitem.price << endl;
invOutFile2 << someitem.quantity<< endl;
invOutFile2.close();
total=someitem.price * someitem.quantity;
subtotal=subtotal+total;
}
void searchItem ()
{
ifstream invFile("invent1.txt");
string itemName;
string line;
string front;
string back;
ifstream sheet("invent1.txt");
int pos;
if (sheet.fail())
{
cout << "Not found" << endl;
exit(1);
}
while (!sheet.eof())
{
getline(sheet, line);
if (!sheet.eof())
{
cout << "[" << line << "]" << endl;
cout << "Please enter an item number to look for: " << endl;
double lookingfor;
cin >> lookingfor;
pos = line.find(lookingfor);
cout << "The number is found at pos " << pos << endl;
getline(invFile, itemName);
cout << itemName << endl;
while (pos != string::npos)
{
front = line.substr(0, pos);
line = line.substr(pos+1, string::npos);
cout << "The front is " << front << endl;
cout << "The back is " << line << endl;
pos = line.find(lookingfor);
}
}
}
}
void listItem()
{
fstream itemFile;
string goods;
itemFile.open("invent1.txt");
cout << "Enter how many rows to display:";
cin >> numberToDisplay;
for (int i=0; i
getline(itemFile, goods);
cout << goods << endl;
if (i % 15 == 14)
{
// Windows specific commands
system("pause");
system("cls");
}
}
}
void totalValue()
{
cout << "The totalvalue is " << subtotal << endl;
}
void saveFile()
{
invItem items[1000];
ofstream invOutFile2("invent1.txt", ios::app); /// declare and open for append
invOutFile2 << items[0].name << endl;
invOutFile2 << items[0].number << endl;
invOutFile2 << items[0].price << endl;
invOutFile2 << items[0].quantity<< endl;
invOutFile2.close();
}
void readIn()
{
string itemName;
string itemNumber;
double itemPrice;
int itemQuantity;
string inputBuffer;
ifstream invFile("invent1.txt"); /// declares and opens
getline(invFile, itemName);
getline(invFile, itemNumber);
getline(invFile, inputBuffer);
itemPrice = atof(inputBuffer.c_str());
getline(invFile, inputBuffer);
itemQuantity = atoi(inputBuffer.c_str());
cout << "Item = " << itemName << "[" << itemNumber <<"]" << endl;
cout << "Quantity = " << itemQuantity << " @ " << itemPrice
<< " = " << itemPrice * itemQuantity;
invFile.close();
}