Question
Hello. I have a question about how to put string, double and int object into a constructer in a class function by user-input. I am
Hello. I have a question about how to put string, double and int object into a constructer in a class function by user-input. I am now practicing pointer, array, and class and try to build a program up by my self. However, I am stacked.
my code is here :
#include
#include
using namespace std;
class InventoryItem {
private:
string description; // item description
double cost; // item cost
int units; // number of units on hand
int MAX_NUMBER = 10; // limit of times to ask
public :
// default constructor
InventoryItem() {
description = "";
cost = 0.0;
units = 0;
}
string getDescription() {
return description;
}
double getCost()
{
return cost;
}
int getUnits() {
return units;
}
void setDescription(string d) {
description = d;
}
void setCost(double c)
{
cost = c;
}
void setUnits(int u)
{
units = u;
}
virtual void display() const {
cout << "type of this prodct is : " << description << " ";
cout << " price is : " << cost << " " << "$" << " ";
cout <<" the number of this product is " << units << endl;
}
void checkNum(int inputNumber )
{
if (inputNumber == MAX_NUMBER || inputNumber >= MAX_NUMBER )
throw " Error: this number is too big ";
}
};
int main() {
int howMany;
cout << "how many times you want to enter data " << endl;
cin >> howMany;
// try that imput number ot times is valid or nor
try {
InventoryItem item;
item.checkNum(howMany);
}
catch (const char * msg)
{
cout << msg << endl;
}
for (int i = 0; i < howMany; i++) {
InventoryItem item1;
}
system("pause");
return 0;
}
I made inside for loop empty:
What I want to do in this program is that fisrt, ask to an user how many times s/he wants to enter data: once the user put the number ( for example, int howmany = 4times ) , then the user is repeatedly (4 times ) requeired input data 1: the name of prodcut (string) , 2: the cost of the prodcut ( double ) , 3: the number of units of that ( int ) .
if the user put ( TV, 500.0, 1) ( Pen, 1.0 , 10 ) ( IPod, 120.25, 5 ) ( hand cream , 18.0 , 8 ) then this program dispay these data by using virtual void display() in InventoryItem class .
how can I put the data of user-input ?
Could you show me a solution ?
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