Code:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
using namespace std;
class Inventory {
private:
int qty_stock, reord_lev;
string ProdID, ProdDesc, manu;
float unitcost;
public:
Inventory();
Inventory(string, string, string, float, int, int);
void listcurinv();
string get_desc();
string get_id();
string get_manu();
float get_unitcost();
int get_qty_stock();
int get_reord_lev();
int add_qty();
int rmv_qty();
void add_stock();
void rmv_stock();
void add_prod();
void rmv_prod();
void calc_inv();
void list_reord();
};
Inventory::Inventory(){}
Inventory::Inventory(string id, string desc, string manufac, float cost, int qty, int r_level) {
ProdID = id;
ProdDesc = desc;
manu = manufac;
unitcost = cost;
qty_stock = qty;
reord_lev = r_level;
}
void Inventory::listcurinv() {
cout << setw(10) << ProdID;
cout << setw(30) << ProdDesc;
cout << setw(15) << manu;
cout << setw(10) << unitcost;
cout << setw(5) << qty_stock;
cout << setw(5) << reord_lev << endl;
}
string Inventory::get_desc() {
return ProdDesc;
}
string Inventory::get_id() {
return ProdID;
}
string Inventory::get_manu() {
return manu;
}
float Inventory::get_unitcost() {
return unitcost;
}
int Inventory::get_qty_stock() {
return qty_stock;
}
int Inventory::get_reord_lev() {
return reord_lev;
}
int Inventory::add_qty() {
int additem;
cout << "Enter # of items to add to this product: ";
cin >> additem;
cin.ignore();
qty_stock += additem;
return qty_stock;
}
int Inventory::rmv_qty() {
int rmvitem;
cout << "Enter # of items to remove from this product: ";
cin >> rmvitem;
cin.ignore();
while (rmvitem > qty_stock) {
cout << "Can't remove this amount of items. Re-Enter amount: ";
cin >> rmvitem;
cin.ignore();
}
while (rmvitem <= 0) {
cout << "Number entered must be greater than 0. Re-Enter amount: ";
cin >> rmvitem;
cin.ignore();
}
qty_stock -= rmvitem;
return qty_stock;
}
int main() {
char choice;
int flag = 0;
Inventory Product[100], spare;
ifstream infile("c:\\sportsstore.txt");
char Values[3][100];
char numvalues[3][100];
char eoln; // ensures that final character of each line is read
int p,j,k;
string prodnum;
int additem;
p=0;
while(1) {
infile.getline(Values[0], 10, ',');
if (infile.eof()) break;
infile.getline(Values[1], 31, ',');
infile.getline(Values[2], 15, ',');
infile.getline(numvalues[0], 10, ',');
infile.getline(numvalues[1], 5, ',');
infile.getline(numvalues[2], 5, '\n');
Inventory TempProduct (Values[0], Values[1], Values[2], atof(numvalues[0]), atoi(numvalues[1]), atoi(numvalues[2]));
Product[p] = TempProduct;
p++;
}
infile.close();
while (flag == 0) {
cout << "\t\t\n\n" << "**************************************************";
cout << "\t\n\n" << "Sports Store Management Application";
cout << "\t\n\n" << "**************************************************";
cout << "\t\n\n" << "[L] List all the inventory sorted by product description.";
cout << "\t\n" << "[I] List all the inventory sorted by product ID.";
cout << "\t\n" << "[A] Add items to the stock of an existing product.";
cout << "\t\n" << "[R] Remove items from the stock of an existing product.";
cout << "\t\n" << "[C] Create an entry for a new product.";
cout << "\t\n" << "[D] Delete all information for a product.";
cout << "\t\n" << "[T] Show the total value of the entire inventory.";
cout << "\t\n" << "[M] List those products needing to be reordered.";
cout << "\t\n" << "[Q] Quit.";
cout << "\t\n\n" << "Choice (enter lowercase letter option): ";
choice = getche();
switch(choice) {
case 'l':
system("cls");
for (j = 0; j < p - 1; j++) {
for (k = 0; k < p-1; k++) {
if (Product[k].get_desc() > Product[k+1].get_desc()) {
spare = Product[k];
Product[k] = Product[k+1];
Product[k+1] = spare;
}
}
}
for (j = 0; j < p; j++) {
Product[j].listcurinv();
}
getche();
system("cls");
break;
case 'i':
system("cls");
for (j = 0; j < p - 1; j++) {
for (k = 0; k < p - 1; k++) {
if (Product[k].get_id() > Product[k+1].get_id()) {
spare = Product[k];
Product[k] = Product[k+1];
Product[k+1] = spare;
}
}
}
for (j = 0; j < p; j++) {
Product[j].listcurinv();
}
getche();
system("cls");
break;
case 'a':
system("cls");
cout << "Enter a product ID number to add items to: ";
cin >> prodnum;
// cin.ignore();
for (j = 0; j < p; j++) {
if (prodnum == Product[j].get_id()) {
Product[j].add_qty();
break;
}
}
system("cls");
break;
case 'r':
system("cls");
cout << "Enter a product ID number to remove items from: ";
cin >> prodnum;
cin.ignore();
for (j = 0; j < p; j++) {
if (prodnum == Product[j].get_id()) {
Product[j].rmv_qty();
break;
}
}
system("cls");
break;
case 'q':
flag = 1;
break;
default:
cout << "\nInvalid selection. Press a key to return to main menu.";
getche();
system("cls");
}
if (flag == 1) {
break;
}
}
ofstream outfile("c:\\sportsstore.txt");
for (j = 0; j < p; j++) {
outfile << Product[j].get_id();
outfile << "," << Product[j].get_desc();
outfile << "," << Product[j].get_manu();
outfile << "," << Product[j].get_unitcost();
outfile << "," << Product[j].get_qty_stock();
outfile << "," << Product[j].get_reord_lev() << endl;
}
return 0;
}
I do have one question though. How can I error check for when a user enters an alphanumeric when it should be a numeric value?