Hello, I am trying to get the stock count to print for each of the toys: ps3, elmo, and wii. I don't have any clue on how to do it. To print the stockcount, i created the function getStock to try to do the job. Can someone help me get the stock count to print for each toy? Thanks!


My .cpp and .h file is below

Here is the code:

.cpp file

#include <iostream>
#include <iomanip>
using namespace std;
#include "Unknown.h"

int main()
{
    
    toyType ps3("Playstation3",500,2000);
    toyType elmo;
    
    cout<<"Toyname"<<"      Cost "<<" Stock"<<endl;
    ps3.print();
    cout<<"Toyname"<<" Cost"<<" Stock"<<endl;
    elmo.print();
    
    elmo.modifyALL();
    ps3.sale(2);
    
    cout<<endl;
    cout<<"Toyname"<<" Cost"<<" Stock"<<endl;
    elmo.print();
    cout<<endl;
    cout<<"Toyname"<<"      Cost "<<" Stock"<<endl;
    ps3.print();
    cout<<endl;
    
    elmo.changeCost(100);
    elmo.add2stock(100);
    elmo.print();
    toyType Wii("Wii");
    cout<<endl;
    cout<<"There is a new toy called: "<<endl;
    Wii.print();
    
    
    elmo.getStock();
    ps3.getStock();
    Wii.getStock();
    getStock.print();
    
    
    system ("PAUSE");
    return 0;
}

.h file

class toyType
{
public:
       
       void print() const;
       void modifyALL();
       toyType(string m, double c, int stoc);
       toyType();
       void sale(int s);
       void changeCost(double cos);
       void add2stock(int s);
       toyType(string m);
       void getStock() const;
       
private:
             
       string toyname;
       double cost;
       int stockcount;
               
};

 void toyType::print()const
 {
   //   cout<<"Toyname"<<" Cost "<<"Stock"<<endl;
      cout<<toyname<<" $ "<<cost<<"  "<<stockcount<<endl;
 }
 
 
   toyType::toyType(string m, double c, int stoc)
{
     
     toyname = m;
     cost = c;
     stockcount = stoc;            
                 
}

  toyType::toyType()
{
  
  
  toyname = "Unknown";
  cost = 0;
  stockcount = 0;
                   
}
 
 
 
  void toyType::modifyALL()
{
  cout<<endl;
  cout<<"Enter new information below..."<<endl;
  
  cout<<"Enter Toy name: "<<endl;
  cin>>toyname;
  cout<<"Enter Cost of toy: "<<endl;
  cin>>cost;
  cout<<"Enter number in stock: "<<endl;
  cin>>stockcount;      
    
}

void toyType::sale(int s)
{
   cout<<endl;
   cout<<"Entering the sales section..."<<endl;
   cout<<"2 Playstation3(s) sold. There are now 1998 Playstation3(s) in stock"<<endl;
   cout<<endl;                   
   stockcount -= s;
   
}

void toyType::changeCost (double cos)
{
  
  cost += cos;

     
     
}

void toyType::add2stock(int s)
{
     cout<<"The cost and stock for Elmo has changed to..."<<endl;
     cout<<endl;
     
     stockcount+=s;

}

toyType::toyType(string m)
{
    
     toyname = m;
     cost = 0;
     stockcount = 0;
}

int toyType::getStock()
{
    
    cout<<"There are currently "<<getStock.print()<<" ps3s in stock."<<endl;
    cout<<endl;
    cout<<"There are currently "<<getStock.print()<<" wiis in stock."<<endl;
    cout<<endl;
    cout<<"There are currently "<<getStock.print()<<" elmos in stock."<<endl;
    cout<<endl;
    
}

Recommended Answers

All 8 Replies

My getStock function is at the bottom of main. Anyone knows the problem?

I don't understand your function. It seems as though in the cout line, that getstock is recursively (and endlessly) calling itself. It doesn't even return any value that could be displayed if the recursion wasn't endless! I think you are meaning to call some sort of string from a member (probably an array?). This seems impossible, since there is only 1 string/stockcount/price in the class, instead of an array of them. And since you are trying to display 3 toys in the function, don't you think you should have an array of at least 3 of each member? Have a look over your code and try to implement these sugguestions, and post the code/parts you are having problems with.

Hmmm i never realised that the class only represents 1 toy. Ignore the part I said about arrays, but still fix the recursion issue.

Sorry if I wasn't clear enough.

Here are the 2 things I'm trying to do:

- add a new toy "Wii" and we only know the name is "Wii"...I tried to put it by itself, but it would only work if I set stock and cost to 0

- I want to get the number of toys in stock which will be a value returning function.

I believe I covered the first step, but I'm having a hard time returning the value of stock for each toy. Can you help me with this?

Oh, I have 3 toys: Ps3, Wii, and Elmo.

I'm trying to get the stock number of the toys to print out by itself. Anyone know how?

I'm trying to figure out how to return the stockcount from the toys: elmo, ps3, and Wii. My program currently runs and works, but I did not return the value correctly. Can someone show me how? I really need assistance. I have a picture attached of the output that I currently have. Here is the functions that I'm working while my whole code below:

int toyType::getStock(int st)
{
    
    stockcount = st;
    
    return st;
    
}
elmo.getStock(350);
    ps3.getStock(2000);
    Wii.getStock(0);
    
    
    cout<<"There are currently "<<ps3.getStock(1998)<<" ps3s in stock."<<endl;
    cout<<endl;
    cout<<"There are currently "<<Wii.getStock(0)<<" wiis in stock."<<endl;
    cout<<endl;
    cout<<"There are currently "<<elmo.getStock(450)<<" elmos in stock."<<endl;
    cout<<endl;

Here is my code:

.cpp

#include <iostream>
#include <iomanip>
using namespace std;
#include "Unknown.h"

int main()
{
    
    toyType ps3("Playstation3",500,2000);
    toyType elmo;
    
    cout<<"Toyname"<<"      Cost "<<" Stock"<<endl;
    ps3.print();
    cout<<"Toyname"<<" Cost"<<" Stock"<<endl;
    elmo.print();
    
    elmo.modifyALL();
    ps3.sale(2);
    
    cout<<endl;
    cout<<"Toyname"<<" Cost"<<" Stock"<<endl;
    elmo.print();
    cout<<endl;
    cout<<"Toyname"<<"      Cost "<<" Stock"<<endl;
    ps3.print();
    cout<<endl;
    
    elmo.changeCost(100);
    elmo.add2stock(100);
    elmo.print();
    toyType Wii("Wii");
    cout<<endl;
    cout<<"There is a new toy called: "<<endl;
    Wii.print();
    cout<<endl;
    
    elmo.getStock(350);
    ps3.getStock(2000);
    Wii.getStock(0);
    
    
    cout<<"There are currently "<<ps3.getStock(1998)<<" ps3s in stock."<<endl;
    cout<<endl;
    cout<<"There are currently "<<Wii.getStock(0)<<" wiis in stock."<<endl;
    cout<<endl;
    cout<<"There are currently "<<elmo.getStock(450)<<" elmos in stock."<<endl;
    cout<<endl;
    
    
    system ("PAUSE");
    return 0;
}

.h file

class toyType
{
public:
       
       void print() const;
       void modifyALL();
       toyType(string m, double c, int stoc);
       toyType();
       void sale(int s);
       void changeCost(double cos);
       void add2stock(int s);
       toyType(string m);
       int getStock(int st);
       
private:
             
       string toyname;
       double cost;
       int stockcount;
               
};

 void toyType::print()const
 {
   //   cout<<"Toyname"<<" Cost "<<"Stock"<<endl;
      cout<<toyname<<" $ "<<cost<<"  "<<stockcount<<endl;
 }
 
 
   toyType::toyType(string m, double c, int stoc)
{
     
     toyname = m;
     cost = c;
     stockcount = stoc;            
                 
}

  toyType::toyType()
{
  
  
  toyname = "Unknown";
  cost = 0;
  stockcount = 0;
                   
}
 
 
 
  void toyType::modifyALL()
{
  cout<<endl;
  cout<<"Enter new information below..."<<endl;
  
  cout<<"Enter Toy name: "<<endl;
  cin>>toyname;
  cout<<"Enter Cost of toy: "<<endl;
  cin>>cost;
  cout<<"Enter number in stock: "<<endl;
  cin>>stockcount;      
    
}

void toyType::sale(int s)
{
   cout<<endl;
   cout<<"Entering the sales section..."<<endl;
   cout<<"2 Playstation3(s) sold. There are now 1998 Playstation3(s) in stock"<<endl;
   cout<<endl;                   
   stockcount -= s;
   
}

void toyType::changeCost (double cos)
{
  
  cost += cos;

     
     
}

void toyType::add2stock(int s)
{
     cout<<"The cost and stock for Elmo has changed to..."<<endl;
     cout<<endl;
     
     stockcount+=s;

}

toyType::toyType(string m)
{
    
     toyname = m;
     cost = 0;
     stockcount = 0;
}

int toyType::getStock(int st)
{
    
    stockcount = st;
    
    return st;
    
}

My guess is that you are supposed to have two functions, not one here:

int toyType::getStock(int st)
{
     stockcount = st;
     return st;
}

You are simply returning the value you passed to the function here. Thus you really aren't "getting" anything. There's no new information. You are passing something to the function and returning what you passed. You are "setting" stockcount to that value. Usually you have two functions: a "set" function that is a void function and "sets" the value of a class data member and a "get" function that returns that value and takes no parameters. You're doing both in one function. You're changing a value and returning a value. You probably want something like this:

int toyType::getStock ()
{
     return stockcount;
}

void toyType::setStock (int count)
{
     stockcount = count;
}

Thank you SOOOO much!!! Solved! =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.