C PROGRAM This program builds on the code below include incl
C++ PROGRAM
This program builds on the code below:
#include <iostream>
#include <fstream>
#include \"shirtType.h\"
using namespace std;
void getInput(shirtType s[],ifstream& infile, int& numShirts);
int main()
{
int numShirts;
shirtType shirts[500];
ifstream infile;
infile.open(\"h:\\\\shirtlab.txt\");
getInput(shirts,infile,numShirts);
for(int i =0; i<numShirts;i++)
shirts[i].print();
infile.close();
return 0;
}
void getInput(shirtType s[], ifstream& infile, int& numShirts)
{
int id, q,index=0;
string c,size;
double p;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
while(infile)
{
s[index].setAll(id, c, size, p, q);
index++;
infile>>id;
infile.ignore(100,\'\ \');
getline(infile,c);
infile>>size>>p>>q;
}
numShirts=index;
}
See also the description of the class shirtType below:
#include <iostream>
#include <string>
using namespace std;
class shirtType
{
private:
int shirtID;
string color;
string size;
double price;
int quantity;
public:
void setAll(int i,string c,string s, double p, int q);
void print() const;
void newQuan();
shirtType();
shirtType(int,string,string,double,int);
};
shirtType::shirtType(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
shirtType::shirtType()
{
shirtID=0;
color=\"\";
size=\"\";
price=0.0;
quantity=0;
}
void shirtType::setAll(int i,string c,string s, double p, int q)
{
color = c;
size = s;
if(i>0)
shirtID=i;
else
shirtID=0;
if(p>0.0)
price = p;
else
price=0.0;
if (q>=0)
quantity=q;
else
quantity = 0;
}
void shirtType::newQuan()
{
cout<<\"Enter the new quantity of shirts: \"<<endl;
cin>>quantity;
}
void shirtType::print() const
{
cout<<\"Shirt ID: \"<<shirtID<<endl;
cout<<\"Color: \"<<color<<endl;
cout<<\"Size: \"<<size<<endl;
cout<<\"Price: $\"<<price<<endl;
cout<<\"Quantity: \"<<quantity<<endl;
}
The main program will declare an array of shirtType objects (no more than 500), get file input for some number of array objects, and determine how many array objects were input.
Next, the program presents the user with a menu of choices: a – Add a shirt
s – Sell a shirt
r – Restock a shirt
p – Print a list of all shirts
q – Quit
The program reads the user’s choice, calls a function to do the user’s choice, then displays the menu again. The program loops and does the user’s choices until the user selects q (Quit).
Required functions in main( ):
getInput function to read file input into the array of shirtType objects
Menu function: displays the menu and gets input of the user’s choice
Add a shirt function: gets keyboard input from the user for shirtID, color, size, price, and number of shirts on hand; the next available shirtType object in the array accesses its member function setAll with these values as parameters, then the number of shirts in the array is incremented.
Sell a shirt function: Asks the user to input a shirt ID of the shirt to be sold, then finds the corresponding shirt object in the array (or reports “not found” to the user and exits the function). Checks that the quantity on hand is greater than 0 and if so, the shirt object accesses the class member function that modifies the quantity on hand (newQuan) to reduce the quantity on hand by the number sold.
Restock a shirt function: Asks the user to input a shirt ID of the shirt being restocked, then finds the corresponding shirt object in the array (or reports “not found” to the user and exits the function). Gets user input of the number of shits to add. The shirt object accesses the class member function that modifies the quantity on hand (newQuan) by the number of shirts coming in. It outputs the new value of quantity on hand.
Print a list of all shirts function: Output the data values of all shirts in the array.
shirtType class functions: Note that you may modify the class definition of shirtType by inserting useful class functions as needed. For example, to search for a shirt ID in the array objects, you need a public function getShirtID which returns the shirtID data value. In the header file, put the function prototype in the class definition, and add the function definition below it.
Solution
Answered for getInput function,Menu function, Add shirt and Sell Shirt :
main (string args[])
{
getInput();
Menu();
}
void getInput::readFile(string filename)
{
ifstream myIn;
shirtType shirt[];
myIn.open(filename.c_str());
if (!myIn)
{
cerr<<\"Data file failed to open!\ \";
exit (0);
}
for (int i=0; i<500; i++)
{
while (myIn.peek() != EOF)
{
myIn>>shirt[i];
}
}
}
void Menu ()
{
char i ;
do{
cout << \"Please Enter you choice :\";
cout <<\"a - Add a shirt\ \";
cout <<\"s-Sell a shirt\ \";
cout <<\"r-Restock a shirt\ \";
cout <<\"p-Print a list of all shirts\ \";
cout <<\"q-Quit\ \";
cin >> i;
switch(i) {
case \'a\' :
addShirt();
break;
case \'s\' :
sellShirt();
break;
case \'r\' :
reStock();
break;
case \'p\' :
listAll();
break;
case \'q\' :
break;
}
}while(i!=\'q\')
addShirt()::shirtType()
{
cout<<\"Shirt ID: \"<<endl;
cin>>shirtID;
cout<<\"Color: \"<<endl;
cin>>color;
cout<<\"Size: \"<<endl;
cin>>size;
cout<<\"Price: $\"<<endl;
cin>>price;
cout<<\"Quantity: \"<<endl;
cin>>quantity;
shirts[]++;
}
sellShirt()::shirtType()
{
cout<<\"Enter ShirtID to be sold\";
cin>>shirtID;
getshirtID(shirtID);
}




