Question

c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

c++ programming :

everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program.

#include <iostream>

#include <string>

#include <bits/stdc++.h>

#include <iomanip>

#include <fstream>

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(), '\n');

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(), '\n');

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;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <string>

#include <bits/stdc++.h>

#include <iomanip>

#include <fstream>

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(), '\n');

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(), '\n');

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");

if(!fin) // chcek if file exist or not

{

cout<<"no file";


exit(0);

}

Inventory in[MAX];

int i = 0;

while (!fin.eof()) //change in file accession

{

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;

}

}

// it is working fine now

Add a comment
Know the answer?
Add Answer to:
c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

  • THIS IS FOR C++ PROGRAMMING USING VISUAL STUDIO THE PROGRAM NEEDS TO BE IN C++ PROGRAMMING #inclu...

    THIS IS FOR C++ PROGRAMMING USING VISUAL STUDIO THE PROGRAM NEEDS TO BE IN C++ PROGRAMMING #include "pch.h" #include #include using namespace std; // Function prototype void displayMessage(void); void totalFees(void); double calculateFees(int); double calculateFees(int bags) {    return bags * 30.0; } void displayMessage(void) {    cout << "This program calculates the total amount of checked bag fees." << endl; } void totalFees() {    double bags = 0;    cout << "Enter the amount of checked bags you have." << endl;    cout <<...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using...

    I NEED A PSEUDOCODE ALGORITHM FOR THIS CODE PLEASE C++: #include #include #include #include using namespace std; int NumOfEmployees(); int TotDaysAbsent(int); double AverageAbsent(int, int); int main() {         cout << endl << "Calculate the average number of days a company's employees are absent." << endl << endl;      int numOfEmployees = NumOfEmployees();         TotDaysAbsent(numOfEmployees);    return 0; } int NumOfEmployees() {    int numOfEmployees = 0;     cout << "Please enter the number of employees in the company: ";         cin >> numOfEmployees;     while(numOfEmployees <= 0)     {            ...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Trying to figure out how to complete my fourth case 4 (option exit), write a function...

    Trying to figure out how to complete my fourth case 4 (option exit), write a function that display a “Good Bye” message and exits/log out from user’s account displaying a menu (sign in, balance, withdraw and exit.. #include<iostream> #include <limits> using namespace std; void printstar(char ch , int n); double balance1; int main() { system("color A0"); cout<<"\n\t\t ========================================="<< endl; cout<<"\t\t || VASQUEZ ATM SERVICES ||"<< endl; cout<<"\t\t ========================================\n\n"<< endl; int password; int pincode ; cout<<" USERS \n"; cout<<" [1] -...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT