7 Replies - 3420 Views - Last Post: 29 June 2012 - 06:08 PM Rate Topic: -----

#1 dinoprogram   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Drink Machine Simulator, calling inside a destructor

Posted 28 June 2012 - 11:53 PM

First time posting!

I am having issue with the destructor and using the dailyreport in tandem with it.... I have a feeling that I am missing something very fundamental on this assignment rather then how I am using dailyreport and the destructor.


------------------ Here is the problem itself ----------------------



Create a class that simulates and manages a soft drink machine. The class should use a structure that stores the following information:

Drink name
Drink price
Number of drinks in machine

The class should have an array of five of these structures. The elements should be initialized with the following data

drink name/cost/number in machine
cola/.75/20
root beer/.75/20
orange soda/.75/20
grape soda/.75/20
bottled water/1.00/20

The class should have two public member functions, displayChoices (which display menu of drink names and prices) and buyDrink (which handles a sale). The class should also have at least two private member functions, inputMoney, which is called by buyDrink to accept, validate, and return (to buyDrink) the amount of money input, and dailyReport which is called by the destructor to report how many of each drink type remain in the machine at the end of the day and how much money was collected. You may want to use additional functions to make the program more modular.

The client program that uses the class should have a main processing loop which calls the displayChoices class member function and allows the patron to either pick a drink or quit the program. If the patron selects a drink, the buyDrink class member function called to handle the actual sale. This function should be passed the patron's drink choice Here is what the buyDrink function should do:

1) Call the inputMoney function, passing it the patron's dirnk choice.
2) If the patron no longer wishes to make the purchase, return all input money.
3) if the machine is out of the requested soda, display an appropriate "sold out" message and return all input money.
4) If the machine has the soda and enough money was entered, complete the sale by updating the quantity on hand and money collected information, calculating any change due to be returned to the patron, and delevering the soda. This last action can be simulated by printing an appropriate "here is your beverage" message.

Input Validation: only accept valid menu choices. Do not deliver a beverage if the money inserted is less than the price of the selected drink


(this program is not completely finished, I am missing few minor things)

--------------------------------------------------------

---------------------------- Here is my main.cpp --------------------

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



int main()
{

	drinkinfo Drinklist[5];

	Drinklist[0].drinkname = "Cola";
	Drinklist[0].drinkprice = .75;
	Drinklist[0].numcurrent =20;

	Drinklist[1].drinkname = "Root Beer";
	Drinklist[1].drinkprice = .75;
	Drinklist[1].numcurrent =20;

	Drinklist[2].drinkname = "Orange Soda";
	Drinklist[2].drinkprice = .75;
	Drinklist[2].numcurrent =20;

	Drinklist[3].drinkname = "Grape Soda";
	Drinklist[3].drinkprice = .75;
	Drinklist[3].numcurrent =20;

	Drinklist[4].drinkname = "Bottled Water";
	Drinklist[4].drinkprice = .75;
	Drinklist[4].numcurrent =20;

	char choice;
	double grandtotal = 0;

	softmachine machine;


	machine.displayChoices();
	cout << "Please enter choice(upper case): ";
	cin >> choice;
		while (choice != 'F')
		{
		double totalmoney = 0;
		totalmoney = machine.buyDrink(choice, Drinklist, totalmoney);
		cout << " " << endl;
		machine.displayChoices();
		cout << " " << endl;
		grandtotal = totalmoney + grandtotal;
		cout << "Please Enter Choice: ";
		cin >> choice;

		}

		machine.~softmachine();

	
	system("pause");
		return 0;
}




--------------------------------------------------------

---------------------------- Here is my class.h --------------------

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

#ifndef soft_class
#define soft_class

	struct drinkinfo
{
	string drinkname;
	double drinkprice;
	int numcurrent;


};




class softmachine
{
private:

	double inputMoney(char choice)
	{
		double money;
		double valmissing;
		double momoney;
		char choicea;

		cout << "Please insert Money: ";
		cin >> money; 



		if (choice == 'E' && money < 1.00)
		{
			valmissing = 1.00 - money;
			cout << "More money needed (" << valmissing << " needed): ";
			cin >> momoney;
			money = momoney + money;
		}
		else if (money < .75)
		{
			valmissing = .75 - money;
			cout << "More money needed (" << valmissing << " needed): ";
			cin >> momoney;
			money = momoney + money;
		}


		money = change(money, choice);

		cout << "You agree to this amount?(Y/N): " << money << endl;
		cin >> choicea;

		if (choicea == 'N')
		{
			money = 0;
			cout << "Your money has been refunded." << endl;
		}


		return money;
	}

	double change(double money, char choice)
	{
		double change;

		if (choice == 'E' && money > 1.00)
		{
			change = money - 1.00;
			cout << "Your change is: " << change << endl;
		}
		else if (money > .75)
		{
			change = money - .75;
			cout << "Your change is: " << change << endl;
		}
		return money;
	}

		void dailyreport (drinkinfo Drinklist[4], double grand)
	{
		cout << Drinklist[0].drinkname << " cans left: " << Drinklist[0].numcurrent << endl;
		cout << Drinklist[1].drinkname << " cans left: " << Drinklist[1].numcurrent << endl;
		cout << Drinklist[2].drinkname << " cans left: " << Drinklist[2].numcurrent << endl;
		cout << Drinklist[3].drinkname << " cans left: " << Drinklist[3].numcurrent << endl;
		cout << Drinklist[4].drinkname << " cans left: " << Drinklist[4].numcurrent << endl;
		cout << "Total Sales: " << grand << endl;
	}

	

public:

	double buyDrink(char choice,drinkinfo Drinklist[4], double totalmoney);

	void displayChoices();


	~softmachine()
	{
                dailyreport (Drinklist,grand);
		cout << "End" << endl;
	}

};

#endif




--------------------------------------------------------

---------------------------- Here is my class.cpp --------------------

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


void softmachine::displayChoices()
	{

	cout << "a. Cola: $.75" << endl;
	cout << "b. Root Beer: $.75" << endl;
	cout << "c. Orange Soda: $.75" << endl;
	cout << "d. Grape Soda: $.75" << endl;
	cout << "e. Bottled Water: $1.00" << endl;
	cout << "f. Exit" << endl;
	cout << " " << endl;


	}

	double softmachine::buyDrink(char choice,drinkinfo Drinklist[4], double totalmoney)
	{
		double money;
		char choicea;

		cout << "Would you still like to make your purchase?(Y/N): ";
		cin >> choicea;

		if (choicea != 'N')
		{

		if (choice == 'A'&& Drinklist[0].numcurrent != 0)
		{
		 cout << "Your total is .75" << endl;
		 money = inputMoney(choice);
		 totalmoney = totalmoney + money;
		 Drinklist[0].numcurrent--;

		}

		else if(choice == 'B' && Drinklist[1].numcurrent != 0)
		{
			cout << "Your total is .75" << endl;
		 money = inputMoney(choice);
		 totalmoney = totalmoney + money;
		 Drinklist[1].numcurrent--;
		 cout << "Here is your beverage." << endl;
		}

		else if (choice == 'C' && Drinklist[2].numcurrent != 0)
		{
		cout << "Your total is .75" << endl;
		 money = inputMoney(choice);
		 totalmoney = totalmoney + money;
		 Drinklist[2].numcurrent--;
		 cout << "Here is your beverage." << endl;
		}
		else if (choice == 'D' && Drinklist[3].numcurrent != 0)
		{
		cout << "Your total is .75" << endl;
		 money = inputMoney(choice);
		 totalmoney = totalmoney + money;
		 Drinklist[3].numcurrent--;
		 cout << "Here is your beverage." << endl;
		}
		else if (choice == 'E' && Drinklist[4].numcurrent != 0 )
		{
		cout << "Your total is 1.00" << endl;
		 money = inputMoney(choice);
		 totalmoney = totalmoney + money;
		 Drinklist[4].numcurrent--;
		 cout << "Here is your beverage." << endl;
		}
		else
		{
			cout << "Sold out, please choose again." << endl;
		}

		}
		return totalmoney;
	}





---------------------------------------------


Now my issues are with calling the dailyreport in my destructor at the end of main. I have tried just about everything including rewriting most of my program.

When attempting to run it my error is an undeclared identifier on both the Drinklist and Grand. I know that if i call dailyreport without any parameters it will run but I cannot figure out a way to allow all the information to be displayed without the parameters I included.

If you figure out how, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Drink Machine Simulator, calling inside a destructor

#2 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Drink Machine Simulator, calling inside a destructor

Posted 29 June 2012 - 12:25 AM

~softmachine()
{
    dailyreport (Drinklist,grand);
    cout << "End" << endl;
}


"Drinklist" and "grand" haven't been declared in that scope.

machine.~softmachine();


Some may say that calling destructors explicitly is a bad idea in many instances, and in this case I would say that it's needless. Instead of calling the destructor, why not just call "dailyreport" and allow the destructor to be called automatically?

This post has been edited by Aphex19: 29 June 2012 - 12:31 AM

Was This Post Helpful? 1
  • +
  • -

#3 dinoprogram   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Re: Drink Machine Simulator, calling inside a destructor

Posted 29 June 2012 - 12:38 AM

View PostAphex19, on 29 June 2012 - 12:25 AM, said:

~softmachine()
{
    dailyreport (Drinklist,grand);
    cout << "End" << endl;
}


"Drinklist" and "grand" haven't been declared in that scope.

machine.~softmachine();


Some may say that calling destructors explicitly is a bad idea, and in this case I would say that it's needless. Instead of calling the destructor, why not just call "dailyreport" and allow the destructor to be called automatically?


Thanks for answering so quick!

While learning about destructors/searching for the answer on the internet, thats pretty much what everything has said about them! I find that this problem that I have been tasked with has some strange things that it requires....

Anyways that was one of the ways that I was testing to make sure it was just a problem with parameters being undeclared inside of the destructor. I was having the dailyreport be public (as I tried calling it in main while private and I cant do that) and call it before calling the destructor. However, I cant do that as its not what the problems asking for... Unless I am not understanding what your saying. (which is entirely possible on my end!)

Thanks though!
Was This Post Helpful? 0
  • +
  • -

#4 Aphex19   User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Drink Machine Simulator, calling inside a destructor

Posted 29 June 2012 - 12:51 AM

Perhaps "softmachine" should contain the array of "drinkinfo". That would solve the problem.

This post has been edited by Aphex19: 29 June 2012 - 01:25 AM

Was This Post Helpful? 0
  • +
  • -

#5 dinoprogram   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Re: Drink Machine Simulator, calling inside a destructor

Posted 29 June 2012 - 01:08 AM

I am not entirely sure how I would code that(and cant compile it right now, can in the morning)... But would it look similar to this? Or would I have to declare it in a function or in some other way?

struct drinkinfo

{
    string drinkname;
    double drinkprice;
    int numcurrent;

       drinkinfo()
      {
    drinkinfo Drinklist[5];


    Drinklist[0].drinkname = "Cola";
    Drinklist[0].drinkprice = .75;
    Drinklist[0].numcurrent =20;

 
    Drinklist[1].drinkname = "Root Beer";
    Drinklist[1].drinkprice = .75;
    Drinklist[1].numcurrent =20;


    Drinklist[2].drinkname = "Orange Soda";
    Drinklist[2].drinkprice = .75;
    Drinklist[2].numcurrent =20;


    Drinklist[3].drinkname = "Grape Soda";
    Drinklist[3].drinkprice = .75;
    Drinklist[3].numcurrent =20;


    Drinklist[4].drinkname = "Bottled Water";
    Drinklist[4].drinkprice = .75;
    Drinklist[4].numcurrent =20;


      }

};


Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6293
  • View blog
  • Posts: 21,632
  • Joined: 05-May 12

Re: Drink Machine Simulator, calling inside a destructor

Posted 29 June 2012 - 01:22 AM

What aphex19 meant was for drinkinfo DrinkList[5] be declared in the softmachine class as private (or public) variable.

This post has been edited by Skydiver: 29 June 2012 - 01:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 dinoprogram   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-June 12

Re: Drink Machine Simulator, calling inside a destructor

Posted 29 June 2012 - 10:10 AM

View PostSkydiver, on 29 June 2012 - 01:22 AM, said:

What aphex19 meant was for drinkinfo DrinkList[5] be declared in the softmachine class as private (or public) variable.


I attempted to declare it in my class as private (and public) however when calling it in the destructor I still get an undeclared identifier on Drinklist. (I am testing this without the double parameter and just the array)
Was This Post Helpful? 0
  • +
  • -

#8 kidicarus   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: Drink Machine Simulator, calling inside a destructor

Posted 29 June 2012 - 06:08 PM

After reviewing this thread I still don't think you need to call the destructor explicitly. The compiler generally handles a call to a destructor. When you create a static storage class object, its destructor is called when the program exists. If you create an automatic storage class object, as this example demonstrates, its destructor is called automatically when the program exists the block of code in which the object is defined.

Since the program suggests that the "dailyReport" function is "called by the destructor...at the end of the day" can't you just remove that line? It should still work.

It should call it automatically. Even still, I'm not sure why the prof would suggest that, except to help you understand when a destructor is called.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1