8 Replies - 401 Views - Last Post: 10 November 2012 - 06:41 PM Rate Topic: -----

#1 slappy5star  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-November 12

array of structs as a class object

Posted 10 November 2012 - 07:42 AM

I have to create a drink machine simulator. I am getting the following error when I call sodas.buyDrink (soda);

"Argument of type 'DrinkInfo*' is incompatiable with paramater of type 'int*'"

Any ideas on what I am doing wrong here?

Thanks!


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int DRINKS = 5;

struct DrinkInfo
{
	string name;
	int numDrinks;
	double price;
};

class SodaMachine
{
	private:
		void inputMoney();
		void dailyReport();	
	public:
		void buyDrink(int []);
		void displayChoices();
		
};

int main ()
{

	SodaMachine sodas;

	int choice = 0;

	DrinkInfo soda[DRINKS] = {{"Cola", .75, 20}, {"Root Beer", .75, 20}, {"Orange Soda", .75, 20}, 
	                          {"Grape Soda", .75, 20}, {"Bottled Water", 1.00, 20}};

	do 
	{
		sodas.displayChoices();
		cin >> choice;

		sodas.buyDrink(soda);

	}while (choice != 6);

	return 0;
}

/*************SodaMachine::displayChoices************/
//This function displays a menu of drink choices
void SodaMachine::displayChoices()
{
	//outputs a menu
	cout << "*********************************\n";
	cout << "*    Drink Name            Cost *\n";
	cout << "*********************************\n";
	cout << " (1) Cola                  $0.75 \n";
	cout << " (2) Root Beer             $0.75 \n";
	cout << " (3) Orange Soda           $0.75 \n";
	cout << " (4) Grap Soda             $0.75 \n";
	cout << " (5) Bottled Water         $1.00 \n";
	cout << "_________________________________\n";
	cout << " \n (6) Quit                        \n";
	cout << endl;
	cout << "Please enter a selection: ";
}


/************SodaMachine::buyDrink***************/
//This function handles drink purchases
void SodaMachine::buyDrink(int soda[])
{
	char confirm;
	int choice;

	cout << "You chose " << soda[choice-1] << " would you like to purchase this drink?";
	cin >> confirm;

}

/***********SodaMachine::inputMoney**************/
//This function takes the users money for the 
//drink purchase
void SodaMachine::inputMoney()
{


}




Is This A Good Question/Topic? 0
  • +

Replies To: array of structs as a class object

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,234
  • Joined: 31-December 10

Re: array of structs as a class object

Posted 10 November 2012 - 08:19 AM

Your buyDrinks() function expects an integer array and you're passing it an array of type DrinkInfo.

*EDIT*: In my opinion, you might be better off by putting the DrinkInfo array inside the SodaMachine class, as a private member variable. This way the user of the SodaMachine class doesn't have to worry about any of the details of the DrinkInfo. This is one of the points of OOP, abstract away the details to make it easier for people to use the classes you design.

I also just realized that you're using the local choice variable inside the buyDrinks() function before it has been initialized so it contains garbage. If you're not getting any warnings about this from your compiler then you should look into increasing the level of warnings/errors to its highest possible value. You never want either warnings or errors. Sometimes it is hard to avoid warnings.

This post has been edited by vividexstance: 10 November 2012 - 08:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 slappy5star  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-November 12

Re: array of structs as a class object

Posted 10 November 2012 - 08:26 AM

View Postvividexstance, on 10 November 2012 - 08:19 AM, said:

Your buyDrinks() function expects an integer array and you're passing it an array of type DrinkInfo.

*EDIT*: In my opinion, you might be better off by putting the DrinkInfo array inside the SodaMachine class, as a private member variable. This way the user of the SodaMachine class doesn't have to worry about any of the details of the DrinkInfo. This is one of the points of OOP, abstract away the details to make it easier for people to use the classes you design.

I also just realized that you're using the local choice variable inside the buyDrinks() function before it has been initialized so it contains garbage. If you're not getting any warnings about this from your compiler then you should look into increasing the level of warnings/errors to its highest possible value. You never want either warnings or errors. Sometimes it is hard to avoid warnings.


Ok I moved that to the private section of my class. However it now says the "=" operand is not allowed there. Where would be the appropriate place to initialize the array after declaring it within the class?
Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1314
  • View blog
  • Posts: 4,514
  • Joined: 19-February 09

Re: array of structs as a class object

Posted 10 November 2012 - 09:54 AM

View Postslappy5star, on 10 November 2012 - 06:26 PM, said:

Ok I moved that to the private section of my class. However it now says the "=" operand is not allowed there. Where would be the appropriate place to initialize the array after declaring it within the class?
Thanks again!


You can initialize in/from the class constructor. Newer compilers (C++11) can use aggregate initialization.
Was This Post Helpful? 1
  • +
  • -

#5 slappy5star  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-November 12

Re: array of structs as a class object

Posted 10 November 2012 - 10:29 AM

Ok, I took the advice here and I think it looks about right. Now my question is...how do I pass choice to the buyDrink function and apply that value to a specific subscript and print its name?

Thanks again.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int DRINKS = 5;

struct DrinkInfo
{
	string name;
	int numDrinks;
	double price;
};

class SodaMachine
{
	private:
		void inputMoney();
		void dailyReport();	
	public:
		void buyDrink(int);
		void displayChoices();
		SodaMachine();
};

int main ()
{

	SodaMachine SodaMachine1;

	int choice = 0;

	do 
	{
		SodaMachine1.displayChoices();
		cin >> choice;

	}while (choice != 6);

	return 0;
}

/***************SodaMachine::SodaMachine*****************/
//Class constructor defines the soda strcut array
SodaMachine::SodaMachine()
{
	DrinkInfo drinks[DRINKS] = {{"Cola", .75, 20}, {"Root Beer", .75, 20}, {"Orange Soda", .75, 20}, 
	                            {"Grape Soda", .75, 20}, {"Bottled Water", .75, 20}};
}

/*************SodaMachine::displayChoices************/
//This function displays a menu of drink choices
void SodaMachine::displayChoices()
{
	//outputs a menu
	cout << "*********************************\n";
	cout << "*    Drink Name            Cost *\n";
	cout << "*********************************\n";
	cout << " (1) Cola                  $0.75 \n";
	cout << " (2) Root Beer             $0.75 \n";
	cout << " (3) Orange Soda           $0.75 \n";
	cout << " (4) Grap Soda             $0.75 \n";
	cout << " (5) Bottled Water         $1.00 \n";
	cout << "_________________________________\n";
	cout << " \n (6) Quit                        \n";
	cout << endl;
	cout << "Please enter a selection: ";
}


/************SodaMachine::buyDrink***************/
//This function handles drink purchases
void SodaMachine::buyDrink(int choice)
{
	
}

/***********SodaMachine::inputMoney**************/
//This function takes the users money for the 
//drink purchase
void SodaMachine::inputMoney()
{


}


Was This Post Helpful? 0
  • +
  • -

#6 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: array of structs as a class object

Posted 10 November 2012 - 03:43 PM

No it doesn't. In SodaMachine constructor you just create a local scoped array of DrinkInfo objects, and that's it - when the constructor finishes its job, that variable goes out of scope and ceases to exist. What good did that do?
Was This Post Helpful? 0
  • +
  • -

#7 slappy5star  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-November 12

Re: array of structs as a class object

Posted 10 November 2012 - 05:09 PM

View PostXupicor, on 10 November 2012 - 03:43 PM, said:

No it doesn't. In SodaMachine constructor you just create a local scoped array of DrinkInfo objects, and that's it - when the constructor finishes its job, that variable goes out of scope and ceases to exist. What good did that do?


Which would explain why I cannot access it in other class functions...

Any suggestions for fixing this?

I also updated my code, here are snippets for what changed:

Now when I attempt to initialize this in the contructor it doesnt seem to work...at the bracket in the second snippet I get this:

'Expected an expression'

class SodaMachine
{
	private:
		void inputMoney();
		void dailyReport();	
	public:
		void buyDrink(int);
		void displayChoices();
		SodaMachine();
		DrinkInfo drinks[];
};



/***************SodaMachine::SodaMachine*****************/
//Class constructor defines the soda struct array
SodaMachine::SodaMachine()
{
	drinks[DRINKS] = {"Cola", .75, 20};
}


Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: array of structs as a class object

Posted 10 November 2012 - 05:36 PM

A number of things. You've never noticed that your price and numDrinks are reversed in your declaration.

Your displayChoices merely spits out static text. Shouldn't the drink items reflect what your array contains. Perhaps display sold out where there are none available?

Make a constructor for your DrinkInfo. The initializer syntax you're stuck on is not always appropriate. In C++ particularly.

I'd go with something like:
class SodaMachine {
public:
	static const int DRINK_COUNT = 5;
	SodaMachine();
private:
	DrinkInfo drinks[DRINK_COUNT];
};

SodaMachine::SodaMachine() {
	// this assumes you've given DrinkInfo a constructor:
	drinks[0] = DrinkInfo("Cola", 0.75, 20);
	drinks[1] = DrinkInfo("Root Beer", 0.75, 20);
	//...
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 slappy5star  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 02-November 12

Re: array of structs as a class object

Posted 10 November 2012 - 06:41 PM

View Postbaavgai, on 10 November 2012 - 05:36 PM, said:

A number of things. You've never noticed that your price and numDrinks are reversed in your declaration.

Your displayChoices merely spits out static text. Shouldn't the drink items reflect what your array contains. Perhaps display sold out where there are none available?

Make a constructor for your DrinkInfo. The initializer syntax you're stuck on is not always appropriate. In C++ particularly.

I'd go with something like:
class SodaMachine {
public:
	static const int DRINK_COUNT = 5;
	SodaMachine();
private:
	DrinkInfo drinks[DRINK_COUNT];
};

SodaMachine::SodaMachine() {
	// this assumes you've given DrinkInfo a constructor:
	drinks[0] = DrinkInfo("Cola", 0.75, 20);
	drinks[1] = DrinkInfo("Root Beer", 0.75, 20);
	//...
}



Hope this helps.


I'll give this a try, I appreciate the feedback.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1