I'm trying to see if there is a better way to go about getting the number of carts that the user inputs, instead of mandating that they have exactly three. My assignment calls for the following:

"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 member variable for how many hot dogs the stand sold that day. Create a constructor that allows a user of the class to initialize both values. Also, create a function named JustSold that increments the number of hot dogs the stand has sold by one. This function will be invoked each time the stand sells a hot dog so that you can track the total number of hot dogs sold by the stand. Add another function that returns the number of hot dogs sold. Finally, add a static variable that tracks the total number of hot dogs sold by all hot dog stands an a static function that returns the value in this variable. Write a main function to test your class with at least three hot dog stands that each sell a variety of hot dogs."

My problem lies in the fact that the teacher might try to say that I own more than three stands, so then what happens!?!. I'm thinking that there might be a loop or array that would be a little better.

#include <iostream>
using namespace std;

class HotDogStand
{
public:
		void JustSold();
		int CartOne;
	    int CartTwo;
		int CartThree;
		int IdNumber;
		int SalesToday1;
		int SalesToday2;
		int SalesToday3;
		int TotalSales;
		int IncrementalSale;
		static int HotDogCartSales;

};

int main()
{
	HotDogStand CartSales;
	CartSales.SalesToday1 = 0;
	CartSales.SalesToday2 = 0;
	CartSales.SalesToday3 = 0;
	CartSales.TotalSales = 0;

	while (true)
	{
		cout << "Please Enter the Hot Dog Cart ID Numbers (Up to Three): " << endl;
		cin >> CartSales.CartOne >> CartSales.CartTwo >> CartSales.CartThree;
		cout << endl;
		cout << "How Many Hot Dogs Has Cart ID #: " << CartSales.CartOne << " Sold ?" << endl;
		cin >> CartSales.SalesToday1;
		cout << endl;
		cout << "How Many Hot Dogs Has Cart ID #: " << CartSales.CartTwo << " Sold ?" << endl;
		cin >> CartSales.SalesToday2;
		cout << endl;
		cout << "How Many Hot Dogs Has Cart ID #: " << CartSales.CartThree << " Sold ?" << endl;
		cin >> CartSales.SalesToday3;
		cout << endl;

		CartSales.JustSold();
		CartSales.IncrementalSales();
	}
	
}

void HotDogStand::JustSold()
{

	TotalSales += SalesToday1;
	TotalSales += SalesToday2;
	TotalSales += SalesToday3;

	cout << "Total Number of Hot Dogs Sold From All Stands Is: " << TotalSales << endl;
	cout << endl;

}

I dont know if this is what you want but..

You can ask the user how many carts there are and with that you can make a dynamic array which will allow you to have only one variable to store the carts ID.

So it would be something similar to this..

class HotDogStand
{
public:
		void JustSold();
		int Cart;
		int IdNumber;
		int SalesToday;
		int TotalSales;
		int IncrementalSale;
		static int HotDogCartSales;
};

int main()
{
		int numCarts;

		cout << "Number of Carts: ";
		cin >> numCarts;

		HotDogStand CartSales = new HotDogStand[numCarts];
		
		for(int x=0; x<numCarts; x++)
		{
				cout << "ID of Cart #" << x+1 << ": ";
				cin >> CartSales[x].Cart;
		}

		return 0;
}

And so on.. that's just a demo. Hope it helps.

Edited 6 Years Ago by Jiwe: n/a

I dont know if this is what you want but..

You can ask the user how many carts there are and with that you can make a dynamic array which will allow you to have only one variable to store the carts ID.

So it would be something similar to this..

class HotDogStand
{
public:
		void JustSold();
		int Cart;
		int IdNumber;
		int SalesToday;
		int TotalSales;
		int IncrementalSale;
		static int HotDogCartSales;
};

int main()
{
		int numCarts;

		cout << "Number of Carts: ";
		cin >> numCarts;

		HotDogStand CartSales = new HotDogStand[numCarts];
		
		for(int x=0; x<numCarts; x++)
		{
				cout << "ID of Cart #" << x+1 << ": ";
				cin >> CartSales[x].Cart;
		}

		return 0;
}

And so on.. that's just a demo. Hope it helps.

I'm trying to get your suggestion to work,m but I'm running into an error that says: " 'initializing' : cannot convert from 'HotDogStand*' to 'HotDogStand'

Should be HotDogStand * CartSales = new HotDogStand[numCarts]; . Any time you are using new in C++ the return value is a pointer to that type.

My problem lies in the fact that the teacher might try to say that I own more than three stands, so then what happens!?!. I'm thinking that there might be a loop or array that would be a little better.

Since the teacher mentions at least three, this would be a good guess - and an appropriate solution.

I was able to introduce a loop that also asked the user how many hot dogs the individual cart has sold, but I was to add those values into a counter type of variable (which will be used later)....I can't seem to get this to work though (the red portions)

#include <iostream>
#include <fstream>
using namespace std;

class HotDogStand
{
public:
		void JustSold();
		int Cart;
		int IdNumber;
		int SalesToday;
		int TotalSales;
		int IncrementalSale;
		int DogCounter;
		static int HotDogCartSales;
};

int main()
{
		int numCarts;

		cout << "Number of Carts: ";
		cin >> numCarts;

		HotDogStand * CartSales = new HotDogStand[numCarts];
		HotDogStand.DogCounter = 0;
		
		for(int x=0; x<numCarts; x++)
		{
				cout << "ID of Cart #" << x+1 << ": ";
				cin >> CartSales[x].Cart;
				for (int y=1;y<numCarts; y++)
				{
					cout << "How many Hot Dogs Did Cart # " << CartSales[x].Cart << " sell?"
						<< endl;
					cin >> CartSales[y].Cart;
					HotDogStand.DogCounter += CartSales[y].Cart;
				}
				cout << endl;
		}

		return 0;

I can make it work with the following code, but I was hoping to include it in the class:

#include <iostream>
#include <fstream>
using namespace std;

class HotDogStand
{
public:
		void JustSold();
		int Cart;
		int IdNumber;
		int SalesToday;
		int TotalSales;
		int IncrementalSale;
		static int DogCounter;
};

int main()
{
		int numCarts;
		static int DogCounter;

		cout << "Please Enter The Number of Carts: ";
		cin >> numCarts;

		HotDogStand * CartSales = new HotDogStand[numCarts];
		DogCounter = 0;
		
		for(int x=0; x<numCarts; x++)
		{
				cout << "ID of Cart #" << x+1 << ": ";
				cin >> CartSales[x].Cart;
				for (int y=1;y<numCarts; y++)
				{
					cout << "How many Hot Dogs Did Cart # " << CartSales[x].Cart << " sell?"
						<< endl;
					cin >> CartSales[y].Cart;
					DogCounter += CartSales[y].Cart;
				}
		}
		cout << endl;
		cout << "Total Hot Dogs Sold = " << DogCounter << endl;

		return 0;
}

Edited 6 Years Ago by PDB1982: n/a

HotDogStand.DogCounter = 0;

HotDogStand is a type, not an object, so use of the dot operator in this context is an error. On the other hand, each element of CartSales:

CartSales

is an object of type HotDogStand, so using the dot operator on each of these objects to allow you to manipulate member variables of each object is appropriate.

Then, if you initialize the value of HotDogCartSales to zero between the end of the class declaration and the beginning of main() you can change this:

for (int y=1;y<numCarts; y++)	
{			{
   cout << "How many Hot Dogs Did Cart # " << CartSales[x].Cart << " sell?"<< endl;			
   cin >> CartSales[x].Cart;			 
   DogCounter += CartSales[y].Cart;
   HotDogStand.DogCounter += CartSales[y].Cart;
}

to this:

cout << "How many Hot Dogs Did Cart # " << CartSales[x].Cart << " sell?" << endl;			
cin >> CartSales[x].DogCounter;			
HotDogStand::HotDogCartSales += CartSales[x].DogCounter;

Note: I've purposely not provided the code to initialize the static member variable. Please look up how to do it in your favorite reference/class notes. Also, for the best marks you should probably change the access of the data members of the class, including the static one, to private instead of public and create public member functions to access the data members as desired.

Edited 6 Years Ago by Lerner: n/a

Please consider better formatting. You don't have to TAB into the next county. What happens when you have 5 nested loops? Your tabbing will have the inside loop indented about 120 spaces.

4 spaces is more than enough for each indent level...

This article has been dead for over six months. Start a new discussion instead.