Question
c++ programming : everything is done, except when you enter (a ) in F option , it does not work. here is the program. #include
c++ programming :
everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program.
#include
#include
#include
#include
#include
using namespace std;
#define MAX 1000
class Inventory {
private:
long itemId;
string itemName;
int numberOfItems;
double buyingPrice;
double sellingPrice;
double storageFees;
public:
void setItemId(long id) {
itemId = id;
}
long getItemId() {
return itemId;
}
void setItemName(string name) {
itemName = name;
}
string getItemName() {
return itemName;
}
void setNumberOfItems(int nOT) {
numberOfItems = nOT;
}
int getNumberOfItems() {
return numberOfItems;
}
void setBuyingPrice(double bPrice) {
buyingPrice = bPrice;
}
double getBuyingPrice() {
return buyingPrice;
}
void setSellingPrice(double sp) {
sellingPrice = sp;
}
double getSelligPrice() {
return sellingPrice;
}
void setStorageFees(double sf) {
storageFees = sf;
}
double getStorageFees() {
return storageFees;
}
};
void menu(Inventory in[], int n) {
while (true) {
char ch;
cout << "----Menu----" << endl;
cout << "a. Print Items Information" << endl;
cout << "b. Print General Information" << endl;
cout << "c. PrintItems Table" << endl;
cout << "d. Edit an Item Informaton" << endl;
cout << "e. Save information in file" << endl;
cout << "f. exit" << endl;
cout << "Enter your choice: ";
cin >> ch;
switch (ch) {
case 'a':
for (int i = 0; i < n; i++) {
cout << "----Item" << i + 1 << "----" << endl;
cout << std::fixed << std::setprecision(2);
cout << "Item id:" << in[i].getItemId() << endl;
cout << "Item Name:" << in[i].getItemName() << endl;
cout << "Number of Items: " << in[i].getNumberOfItems() << endl;
cout << "Selling Price:" << in[i].getSelligPrice() << endl;
cout << "Buying Price:" << in[i].getBuyingPrice() << endl;
cout << "Storage Fee: " << in[i].getStorageFees() << endl;
}
break;
case 'b': {
int total = 0;
double totalP = 0;
double Sp = 0, Bp = 0;
for (int i = 0; i < n; i++) {
total += in[i].getNumberOfItems();
Sp += in[i].getSelligPrice();
Bp += in[i].getBuyingPrice();
totalP += (in[i].getNumberOfItems()
* (in[i].getSelligPrice()
- (in[i].getBuyingPrice()
+ in[i].getStorageFees())));
}
cout << std::fixed << std::setprecision(2);
cout << "Total Number of Items: " << total << endl;
cout << "Total Profit: " << totalP << endl;
cout << "Average Profit: " << totalP / n << endl;
cout << "Average Buying Price: " << Bp / n << endl;
cout << "Average Selling Price: " << Sp / n << endl;
}
break;
case 'c':
//for(int i=0;)
cout
<< "Item Id\tItem Name\tProfit\tSelling Price\tBuying Price\tStorage Fee"
<< endl;
cout << std::fixed << std::setprecision(2);
for (int i = 0; i < n; i++) {
double p = (in[i].getNumberOfItems()
* (in[i].getSelligPrice()
- (in[i].getBuyingPrice()
+ in[i].getStorageFees())));
cout << in[i].getItemId() << "\t" << in[i].getItemName()
<< "\t\t" << p << "\t\t" << in[i].getSelligPrice()
<< "\t\t" << in[i].getBuyingPrice() << "\t\t"
<< in[i].getStorageFees() << endl;
}
break;
case 'd': {
long id;
cout << "enter item id: ";
cin >> id;
int noi;
string name;
double sp, sf, bp;
for (int i = 0; i < n; i++) {
if (id == in[i].getItemId()) {
cout << "Enter new item details" << endl;
cout << "Item Name: ";
cin.ignore(numeric_limits < streamsize > ::max(), ' ');
getline(cin, name);
cout << "Selling Price: ";
cin >> sp;
cout << "Buying Price: ";
cin >> bp;
cout << "Storage Fees: ";
cin >> sf;
cout << "Number of Items: ";
cin >> noi;
in[i].setItemName(name);
in[i].setSellingPrice(sp);
in[i].setBuyingPrice(bp);
in[i].setStorageFees(sf);
in[i].setNumberOfItems(noi);
cout << "Data for item " << id << " Updated sucessfully"
<< endl;
break;
}
}
}
break;
case 'e': {
ofstream out;
out.open("output.txt");
out
<< "Item Id\tItem Name\tSelling Price\tBuying Price\tStorage Fee"
<< endl;
for (int i = 0; i < n; i++) {
out << std::fixed << std::setprecision(2) << in[i].getItemId()
<< "\t" << in[i].getItemName() << "\t\t"
<< in[i].getSelligPrice() << "\t\t"
<< in[i].getBuyingPrice() << "\t\t"
<< in[i].getStorageFees() << endl;
}
out.close();
}
break;
case 'f':
//return 0;
exit(0);
break;
default:
cout << "Invalid choice!!" << endl;
}
}
}
int main() {
char ch;
int n, noi;
long id;
double sp, bp, sf;
string iname;
cout << "How would you like to populate the data (M) Manual, (F) File: ";
cin >> ch;
if (ch == 'M') {
cout << "How many items would you ike to add: ";
cin >> n;
Inventory in[n];
for (int i = 0; i < n; i++) {
cout << "----Item" << i + 1 << "----" << endl;
cout << "Item id:";
cin >> id;
cin.ignore(numeric_limits < streamsize > ::max(), ' ');
cout << "Item Name:";
getline(cin, iname);
cout << "Number of Items: ";
cin >> noi;
cout << "Selling Price:";
cin >> sp;
cout << "Buying Price:";
cin >> bp;
cout << "Storage Fee: ";
cin >> sf;
in[i].setItemId(id);
in[i].setItemName(iname);
in[i].setNumberOfItems(noi);
in[i].setSellingPrice(sp);
in[i].setBuyingPrice(bp);
in[i].setStorageFees(sf);
}
menu(in, n);
} else if (ch == 'F') {
ifstream fin;
fin.open("input.txt");
Inventory in[MAX];
int i = 0;
while (fin >> id >> iname >> noi >> sp >> bp >> sf) {
in[i].setItemId(id);
in[i].setItemName(iname);
in[i].setNumberOfItems(noi);
in[i].setSellingPrice(sp);
in[i].setBuyingPrice(bp);
in[i].setStorageFees(sf);
i++;
}
n = i;
menu(in, n);
} else {
cout << "Please enter a valid option(M/F)" << endl;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started