I am having trouble overloading my function JustSold() with the array for CartSales[x].Cart . I can't seem to get the array to properly import....any ideas?

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

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

static int HotDogCartSales;

int main()
{
		int numCarts;

		HotDogStand HotDogSales;

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

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

		HotDogSales.JustSold(HotDogSales.DogCounter, numCarts, HotDogStand[]);

		return 0;
}

void HotDogStand::JustSold(int DogCounter, int numCarts, int HotDogStand[])
{
	int Answer;
	int JustSoldCounter, x;

	while (true)
	{
		for (x=1;x<=5000;x++)
		{
		cout << "Did Your Carts # " << CartSales[x].Cart << " Sell a Hot Dog?" << endl;
		cout << "Enter 1 for Yes, or 2 for No" << endl;
		cin >> Answer;

		JustSoldCounter = 0;

		if (Answer == 1)
		{
			DogCounter += 1;
			cout << DogCounter;
			cout << endl;
		}
		else if (Answer == 2)
		{
			cout << DogCounter;
		}
		else
		{
			cout << "Error!" << endl;
			break;
		}
		}
	}
}

Recommended Answers

All 10 Replies

Hotdogstand is your class, so it's a type. It's not int Hotdogstand[] it's gotta be of type Hotdogstand[].

void HotDogStand::JustSold(int DogCounter, int numCarts, int HotDogStand[])

should be

void HotDogStand::JustSold(int DogCounter, int numCarts, HotDogStand CartSales[])

and you have to pass in CartSales (no brackets) when you call the method in main(). Note that the parameter of your method JustSold could have been HotDogStand myHotDogStand[] (any name here) it doesn't have to match with the variable in main, just the ones within JustSold itself.

Either what above is said or just use pointers :)

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

class HotDogStand
{
public:
		void JustSold(int DogCounter, int numCarts, HotDogStand *HDS); // Here
		int Cart;
		int IdNumber;
		int SalesToday;
		int TotalSales;
		int IncrementalSale;
		int DogCounter;
		static int HotDogCartSales;
};

static int HotDogCartSales;

int main()
{
		int numCarts;

		HotDogStand HotDogSales;

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

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

		HotDogSales.JustSold(HotDogSales.DogCounter, numCarts, CartSales);

		return 0;
}

void HotDogStand::JustSold(int DogCounter, int numCarts, HotDogStand *HDS) // Here
{
	int Answer;
	int JustSoldCounter, x;

	while (true)
	{
		for (x=0;x<=5000;x++)
		{
		cout << "Did Your Carts # " << HDS[x].Cart << " Sell a Hot Dog?" << endl;
		cout << "Enter 1 for Yes, or 2 for No" << endl;
		cin >> Answer;

		JustSoldCounter = 0;

		if (Answer == 1)
		{
			DogCounter += 1;
			cout << DogCounter;
			cout << endl;
		}
		else if (Answer == 2)
		{
			cout << DogCounter;
		}
		else
		{
			cout << "Error!" << endl;
			break;
		}
		}
	}
}

So in line 8 and 52 you change

int HotDogStand []

to

HotDogStand *VariableName

Everything is working so far, but if I wanted to import the values of CartSales[y].Cart to ....say....output the increased sales per cart, how would I do that?

I was thinking this, but it's not working:

void HotDogStand::JustSold(int DogCounter, int numCarts, HotDogStand CartSales[])
{
	int Answer;
	int x, y;

	while (true)
	{
		for (x=0;x<numCarts;x++)
		{
			cout << endl;
			cout << "Did Your Cart # " << x+1 << " Sell a Hot Dog?" << endl;
			cout << "Enter 1 for Yes, or 2 for No, or 0 to Exit" << endl;
			cin >> Answer;
			cout << endl;

			if (Answer == 1)
			{
				DogCounter += 1;
				CartSales[y].Cart += 1;
				cout << "New Total Sold is: " << DogCounter;
				cout << endl;
			}
			else if (Answer == 2)
			{
				cout << DogCounter;
				break;
			}
			else
			{
				cout << "Error!" << endl;
				break;
			}
		}
	}
}

Where is the value for y coming from in that method? You declare it but you don't assign any value to it.

Where is the value for y coming from in that method? You declare it but you don't assign any value to it.

I'm sorry...I didn't repost the whole code, in order to save some space, but the "y" comes into play in main function of the program and is initialized after CartSales[x].Cart. The output is something like:

"Enter Number of carts: ...
Enter ID for Cart #1: .... (which is the x value)
Enter the Hot Dogs Sold by Cart # 1: ... (which is the y value)"

this is repeated for the value of numCarts which is also presented in the main code. I'm trying to see if it's possible for the CartSales[y].Cart that are established in the main function are able to be brought down into the JustSold function, so that I can increment them by one (if appropriate).

Whew!....Hopefully that made sense....I think it sounded easier in my head :)

Change

void HotDogStand::JustSold(int DogCounter, int numCarts, HotDogStand CartSales[])

To

void HotDogStand::JustSold(int numCarts, HotDogStand CartSales[])

And with that you can make something like

CartSales[x].DogCounter ++;

If user as answered yes


Hope this is what u looking for.

I'm trying to see if it's possible for the CartSales[y].Cart that are established in the main function are able to be brought down into the JustSold function,

CartsSales is coming into the function. The y value in main is isolated from the method due to scope. If you want y in the method, pass it in as a parameter.

CartsSales is coming into the function. The y value in main is isolated from the method due to scope. If you want y in the method, pass it in as a parameter.

How can I do that if "HotDogStand CartSales" is already passed in though?

Name it whatever you'd like in the method. void HotDogStand::JustSold(int DogCounter, int numCarts, HotDogStand CartSales[],int yvaluefrommain) Now you can use it like a count (in other words you have y items). Looking back in main() it seems like that loop only goes 1 time anyway (on line 36 of Jiwe's post) so there may not be any point of passing it in anyway. My main point was you can't just say CartSales[y] in the method because y doesn't exist there.

Name it whatever you'd like in the method. void HotDogStand::JustSold(int DogCounter, int numCarts, HotDogStand CartSales[],int yvaluefrommain) Now you can use it like a count (in other words you have y items). Looking back in main() it seems like that loop only goes 1 time anyway (on line 36 of Jiwe's post) so there may not be any point of passing it in anyway. My main point was you can't just say CartSales[y] in the method because y doesn't exist there.

Ok....I thought my loop was creating a y-valued array that was storing the values, and that's what I was hoping to go off in the JustSold() function. I see what you are saying about the y-value not existing, I might have had it there just to show where I would've wanted it to go. I guess I'll have to take a look at my array ( CartSales[y].Cart ) to see if it is storing each individual entry of it's storing one entry over the other once it's added.


I think the loop should be:

for (y=0; y<numCarts;y++)
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.