You operate several hot dog stands distributed throughout town. Define a class named HotDogStand that has a member variable for the hot dog stand's ID number and a member variable for how many hot dogs the stand has sold that day. Create a constructor that allows a user of the class to initialize both values.
Also create a method named justSold that increments the number of hot dogs the stand has sold by one. The idea is that this method will be invoked each time the stand sells a hot dog so that we can track the total number of hot dogs sold by the stand. Add another method that returns the number of hot dogs sold.

Finally, add a static variable that tracks the total number of hotdogs sold by all hot dog stands and a static method that returns the value in this variable.

Write a main method to test your class with at least three hot dog stands that each sell a variety of hot dogs.

so for I had this

class HotDogStand
{
public:
    HotDogStand();
    HotDogStand(int newID, int newNnumSold);
    int GetID();
    void SetID(int);
    void JustSold();
    void JustSold(int count){NSold += count;};
    int GetNumSold(){return NSold;};
    int GetTotalSold(){return TotalSold += NSold;};
private:
    int NID,NSold, TotalSold, count;

};

int main()
{



  HotDogStand s1(1,0);
  HotDogStand s2(2,0);
  HotDogStand s3(3,0);
  HotDogStand s4;


  s1.JustSold();
  s2.JustSold();
  s1.JustSold();
  s4.JustSold();


  cout << "Stand " << s1.GetID() << " sold " << s1.GetNumSold() << endl;
  cout << "Stand " << s2.GetID() << " sold " << s2.GetNumSold() << endl;
  cout << "Stand " << s3.GetID() << " sold " << s3.GetNumSold() << endl;
  cout << "Stand " << s4.GetID() << " sold " << s4.GetNumSold() << endl;

  cout << "Total sold = " << s1.GetTotalSold() << endl;
  cout << endl;


  s3.JustSold();
  s1.JustSold();
  s2.JustSold(5);
  s1.SetID (99);
  s4.SetID(4);

  cout << "Stand " << s1.GetID() << " sold " << s1.GetNumSold() << endl;
  cout << "Stand " << s2.GetID() << " sold " << s2.GetNumSold() << endl;
  cout << "Stand " << s3.GetID() << " sold " << s3.GetNumSold() << endl;
  cout << "Stand " << s4.GetID() << " sold " << s4.GetNumSold() << endl;

  cout << "Total sold = " << s1.GetTotalSold() << endl;
  cout << endl;


    system("pause");
    return 0;

}

int HotDogStand::GetID() {
    return NID;
}
void HotDogStand::SetID(int newID){
    NID= newID;
}


HotDogStand::HotDogStand()
{
    NID = 0;
    NSold = 0;
}

HotDogStand::HotDogStand(int newID, int newNnumSold)
{
   NID = newID;
   NSold = newNnumSold;
}
void HotDogStand::JustSold()
{
    NSold++;
}

output:
Stand 1 sold 2
Stand 2 sold 1
Stand 3 sold 0
Stand 0 sold 1
Total sold = -858993458

Stand 99 sold 3
Stand 2 sold 6
Stand 3 sold 1
Stand 4 sold 1
Total sold = -858993455

Press any key to continue . .

how can I fix the total stand

Recommended Answers

All 3 Replies

Hmmmm isn't TotalSold supposed to be static?

Forgive me if I'm wrong but it appears you have not declared TotalSold yet. If you haven't encountered a similar problem already, it typically results because int TotalSold is set to a random integer(483432) for instance before you set it. So just write TotalSold = 0; in your constructor and you should be good to go :D

-Cheers

I've made some changes to your code
(there were some logical bugs in your program)
I'm not pointing out them in this post, I just want to refer you to your own code and compare it with the (working(?)) code below:

#include <iostream>
#include <cstdlib>

using namespace std;

/******************** CLASS DECLARATION(S) ********************/
class HotDogStand
{

public:
    HotDogStand();
    HotDogStand(int newID, int newNnumSold);
    int GetID(){return NID;};
    int GetNumSold(){return NSold;};
    void SetID(int newID){NID = newID;};
    void JustSold();
    void JustSold(int count);
    int GetTotalSold(){return TotalSold;}
private:
    int NID, NSold, TotalSold, count;

};

HotDogStand::HotDogStand()
{
    NID = 0;
    NSold = 0;
    TotalSold = 0;
}

HotDogStand::HotDogStand(int newID, int newNnumSold)
{
   NSold = 0;
   TotalSold = 0;
   NID = newID;
   NSold = newNnumSold;
   TotalSold += newNnumSold;
}
void HotDogStand::JustSold()
{
    NSold = 1;
    TotalSold++;
}

void HotDogStand::JustSold(int count)
{
    TotalSold += count;
    NSold = count;
}
/**************************************************************/

/************************ MAIN PROGRAM ************************/
int main()
{

  HotDogStand s1(1,0);
  HotDogStand s2(2,0);
  HotDogStand s3(3,0);
  HotDogStand s4;


  s1.JustSold();
  s2.JustSold();
  s1.JustSold();
  s4.JustSold();


  cout << "Stand " << s1.GetID() << " sold " << s1.GetNumSold() << endl;
  cout << "Stand " << s2.GetID() << " sold " << s2.GetNumSold() << endl;
  cout << "Stand " << s3.GetID() << " sold " << s3.GetNumSold() << endl;
  cout << "Stand " << s4.GetID() << " sold " << s4.GetNumSold() << endl;

  cout << "Total sold = " << s1.GetTotalSold() << endl;
  cout << endl;


  s3.JustSold();
  s1.JustSold();
  s2.JustSold(5);
  s1.SetID (99);
  s4.SetID(4);

  cout << "Stand " << s1.GetID() << " sold " << s1.GetNumSold() << endl;
  cout << "Stand " << s2.GetID() << " sold " << s2.GetNumSold() << endl;
  cout << "Stand " << s3.GetID() << " sold " << s3.GetNumSold() << endl;
  cout << "Stand " << s4.GetID() << " sold " << s4.GetNumSold() << endl;

  cout << "Total sold = " << s1.GetTotalSold() << endl;
  cout << endl;


  system("pause");
  return 0;

}
/**************************************************************/

P.S.: Sorry if my English is bad ! My native language is Dutch, I also speak C++ ;)

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.