I have written a program, but I am getting unusual errors and I do not know why.
ID: 3639161 • Letter: I
Question
I have written a program, but I am getting unusual errors and I do not know why. Please help me find my errors and show me how to correct them?Default constructor It provides default values of zero for each numeric data member.
An overloading constructor It accepts an ID number, quantity on hand and price of the product, and uses them to initialize all data members.
Destructor It checks if the quantity is 0. If yes, display a message “Sold out”, else display “In stack”.
GetPrice( ) It reads an ID number from the keyboard and display the price of this product. If the ID does not exist, print “No such item”.
Buy(int n) If there are not enough products left, i.e., if quantity < n; print out message that says “No enough items”. Otherwise, it display the total cost for buying n such products, and decrease the quantity of this product.
Display( ) It displays all values of a product object in an appropriate format.
Create 3 products with ID being 100, 200 and 300 and some values (given by you) for quantity and price.
Ask an ID from user and call GetPrice( ) to display the price.
Ask for input of an integer number n and use all the 3 objects to Call buy(n).
Call Display( ) at the end for each product.
#include<iostream>
#include<iomanip>
using namespace std;
class Product{
private:
int id_number; //member variables
int qty;
float price;
public: // member functions
Product(); //constructor sets all value to zero
Product(int, int, float); //overloading constructor
~Product(); // destructor
double getPrice(); //mutator
int Buy(int n);
int Display();
};
int main(){
int id;
int n;
Product product1(100,10,5.00), //created 3 objects
product2(200,20,1.00), //that show id,qty,&
product3(300,30,2.00); //price
cout<<" Enter an ID number: "<<endl;
cin>>id;
product1.getPrice(); //a call to display the price
cout<<" Enter an input for integer n: ";
cin>>n;
//product1.Buy(n),
//product2.Buy(n),
//product3.Buy(n);
int Display();
}
Explanation / Answer
please rate - thanks
I've fixed your errors, however I believe this should be done with arrays, and getPrice()
should do the input, not get the information from the main.
If you'd like me to fix this completely, not just fix your errors, which I did. rate and message me with a link to the post
#include<iostream>
#include<iomanip>
using namespace std;
class Product{
private:
int id_number; //member variables
int qty;
float price;
public: // member functions
Product() //constructor sets all value to zero
{qty=0;
price=0;
id_number=0;
}
Product(int id, int q, float p)
{qty=q;
price=p;
id_number=id;
} //overloading constructor
~Product() // destructor
{if(qty==0)
cout<<"Sold out ";
else
cout<<"In stack ";
}
double getPrice()
{return price;
} //mutator
int Buy(int n)
{if(qty<n)
cout<<"Not enough items ";
else
{cout<<"cost for the "<<n<<" items is $"<<n*price<<endl;
qty-=n;
}
}
int Display()
{cout<<"ID: "<<id_number<<endl;
cout<<"Quantity in stoc: "<<qty<<endl;
cout<<"Item cost: $"<<price<<endl<<endl;
}
};
int main(){
int id;
int n;
Product product1(100,10,5.00), //created 3 objects
product2(200,20,1.00), //that show id,qty,&
product3(300,30,2.00); //price
cout<<" Enter an ID number: "<<endl;
cin>>id;
product1.getPrice(); //a call to display the price
cout<<" Enter an input for integer n: ";
cin>>n;
product1.Buy(n),
product2.Buy(n),
product3.Buy(n);
int Display();
}