Archived

This topic is now archived and is closed to further replies.

pdstatha

I'm either blind or stupid!!!!!!!!

Recommended Posts

Or both maybe i''m not sure. Anyway i''m finally coming towards completion in porting my java vending machine to C++. It finally runs and does most of the stuff it''s supposed to. I can now add money and order a product. One problem tho it doesn''t seem to check if there is sufficient credit before I order a product, which I know I have clearly marked out and which works in java. The only real difference between the ports is that in the java version for interface3, I didn''t use the switch statement. Also if I order a product it will not update the credit and it will keep saying there are 19 products remaining in that tray, ie. initially each tray has 20 products, if I were to order a product from tray A1 and then to do the same again it should say there are now 18 products remaining, instead it gives me the result of 19 over and over. Here''s the full code for the main program, as far as I can see there shouldn''t be any reason why this won''t work. If you should wish to see the class implementations just ask.
  
void interface1(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad);
void interface2(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad);
void interface3(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad);
Tray *tray_prod_init();

void main() {

	//Create objects needed

	Tray* tList = tray_prod_init();    // use tList

	Bank* moneyBag = new Bank();
    Display* display = new Display();
    Keypad* keypad = new Keypad();

	//Run the interface menu

	interface1(tList,moneyBag,display,keypad);

	//Delete objects

    delete[] tList; // MAKE SURE YOU DO THIS WHEN YOU''RE DONE!!!

	delete moneyBag;
	delete display;
	delete keypad;

}

Tray *tray_prod_init() {

	Tray *t_array = new Tray[20];

    //Set up List of Products


    Product mars("Mars",40);
    Product Snickers("Snickers",40);
    Product Wispa("Wispa",35);
	Product Twirl("Twirl",35);
    Product Crunchie("Crunchie",35);
    Product Dairy_Milk("Dairy Milk",30);
	Product Fruity_Polos("Fruity Polos",25);
    Product Jelly_babys("Jelly babys",35);
    Product Kitkat("Kitkat",30);

    //Set up List of trays

    Tray A1("A1",mars);
    Tray A2("A2",Snickers);
    Tray A3("A3",Wispa);
	Tray B1("B1",Twirl);
    Tray B2("B2",Crunchie);
    Tray B3("B3",Dairy_Milk);
	Tray C1("C1",Fruity_Polos);
    Tray C2("C2",Jelly_babys);
    Tray C3("C3",Kitkat);

    //Assign position in array for each tray


    t_array[0] =  A1;
    t_array[1] =  A2;
    t_array[2] =  A3;
    t_array[3] =  B1;
	t_array[4] =  B2;
	t_array[5] =  B3;
	t_array[6] =  C1;
	t_array[7] =  C2;
	t_array[8] =  C3;

    return t_array;

}

void interface1(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad) {	
	
	int choice = 0;

	cout << "\n";
    cout << "################################################" << "\n";
    cout << "# 1: Insert Money" << "\n";
    cout << "# 2: Order Product" << "\n";
    cout << "# 3: Clear Display" << "\n";
    cout << "# 4: Return money" << "\n";
    cout << "################################################" << "\n";
    display->showDisplay("CREDIT::                   ");
	cout << moneyBag->getCred() << "\n";
    cout << "\n";

	cout << "PLEASE CHOOSE AN OPTION::";
	cin >> choice;

	switch (choice) {

          case 1:
              interface2(tList,moneyBag,display,keypad);
          break;

          case 2:
              interface3(tList,moneyBag,display,keypad);
          break;

          case 3:
              display->clear();
              interface1(tList,moneyBag,display,keypad);
          break;

          case 4:
              display->showDisplay("CHANGE GIVEN:: ");
			  cout << moneyBag->getCred() << "\n";
              moneyBag->giveChange(moneyBag->getCred());
              interface1(tList,moneyBag,display,keypad);
          break;

          default:
            cout << "\nNot a valid option.";
    }//end switch


}

void interface2(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad) {

	int choice = 0;

    cout << "\n";
    cout << "################################################" << "\n";
    cout << "# 1: 1p";
    cout << "\n# 2: 2p";
    cout << "\n# 3: 5p";
    cout << "\n# 4: 10p";
    cout << "\n# 5: 20p";
    cout << "\n# 6: 50p";
    cout << "\n# 7: 100p";
    cout << "\n# 8: 200p";
    cout << "\n# 9: Return to main menu";
    cout << "\n################################################" << "\n";
    display->showDisplay("CREDIT::                   ");
	cout << moneyBag->getCred() << "\n";
    cout << "\n";	

	while (choice != 9) {

		cout << "PLEASE CHOOSE AN OPTION::";
	    cin >> choice;

		switch (choice) {

              case 1:
                  moneyBag->addCred(1);
              break;

              case 2:
                  moneyBag->addCred(2);
              break;

              case 3:
                  moneyBag->addCred(5);
              break;

              case 4:
                  moneyBag->addCred(10);
              break;

              case 5:
                  moneyBag->addCred(20);
              break;

              case 6:
                  moneyBag->addCred(50);
              break;

              case 7:
                  moneyBag->addCred(100);
              break;

              case 8:
                  moneyBag->addCred(200);
              break;

              case 9:
                  interface1(tList,moneyBag,display,keypad);
              break;

              default:
                    cout << "\nNot a valid option.";
        }//end switch

    }//end while


}

void interface3(Tray tList[],Bank* moneyBag,Display* display,Keypad* keypad) {

	string keyCode;
	bool not_exist;
	int choice = 0;
	int quantity;
    int cred;
    int price, i;	

	Product prd("prd",0);
	Tray tmp("null", prd);

	cout << "\n";
	cout << "# A1:Mars         A2:Snickers    A3:Wispa";
	cout << "\n# B1:Twirl        B2:Crunchie    B3:Dairy milk";
	cout << "\n# C1:Fruity Polos C2:Jelly babys C3:Kitkat";

    cout << "\n";
    cout << "################################################" << "\n";
    cout << "# 1: Select a tray";
    cout << "\n# 2: Return to main menu";
	cout << "\n################################################" << "\n";
    display->showDisplay("CREDIT::                   ");
	cout << moneyBag->getCred() << "\n";
    cout << "\n";	


	cout << "PLEASE CHOOSE AN OPTION::";
	cin >> choice;

	switch (choice) {

    case 1:
	    cout << "\n";
        display->showDisplay("TRAY CODE::");
		keyCode = keypad->getCode();

		not_exist = false;

		for(i=0;i<sizeof(tList);i++) {
			if(tList[i].getCode() == keyCode) {
				not_exist = false;
				tmp = tList[i];
				break;
			}
			else {
				not_exist = true;
			}
		}

		if(not_exist) {
			display->showDisplay("INVALID TRAY CODE");
		}
	    else {
			quantity = tmp.getQuant();

            //Check quantity


		    if(quantity > 0) {
				cred = moneyBag->getCred();
				price = tmp.getProduct().getPrice();

				//Check sufficient credit


				if(price > cred) {
					display->showDisplay("");
					cout << cred;
					display->showDisplay("INSUFFICIENT CREDIT");
				}
                                //This is where I update stuff

				else if(cred >= price) {
					cred -= price;
					tmp.dispense();
					display->showDisplay("\nPRODUCT ORDERED:: ");
					cout << tmp.getProduct().getName() << "\n";					
					display->showDisplay("\nQUANTITY REMAINING::");
					cout << tmp.getQuant() << "\n";
					moneyBag->updateCred(cred);
					cred = moneyBag->getCred();					
					display->showDisplay("CREDIT::");
	                cout << moneyBag->getCred() << "\n";
				}
			}
			else {
				display->showDisplay("SELECT DIFFERENT ITEM");
			}

		}
		interface3(tList,moneyBag,display,keypad);

    break;

    case 2:
        interface1(tList,moneyBag,display,keypad);
    break;

	default:
        cout << "\nNot a valid option.\n";
	}	

}  

Share this post


Link to post
Share on other sites
I''m not sure about your problem but by looking at your code I noticed the use of sizeof over an array created by new. sizeof gives you the size on bytes of the variable. You can only use it if you create the array like this:
arrayType arr[size];
And to have the correct size of the array you must
sizeof (arr)/ sizeof (arrayType)
Your options are either use vector or other of the stl or create your own

Share this post


Link to post
Share on other sites
Nah it's not that, that part of the code checks to see if the tray code exists or not. If invalid it tells the user, which it does just fine. This where the code starts to go wrong.

    
quantity = tmp.getQuant(); //Check quantiy

if(quantity > 0) {
cred = moneyBag->getCred();
price = tmp.getProduct().getPrice();
//Check sufficient credit

if(price > cred) {
display->showDisplay("");
cout << cred;
display->showDisplay("
INSUFFICIENT CREDIT");
}
//This is where I update stuff

else if(cred >= price) {
cred -= price;
tmp.dispense();
display->showDisplay("
\nPRODUCT ORDERED:: ");
cout << tmp.getProduct().getName() << "
\n";
display->showDisplay("
\nQUANTITY REMAINING::");
cout << tmp.getQuant() << "
\n";
moneyBag->updateCred(cred);
cred = moneyBag->getCred();
display->showDisplay("
CREDIT::");
cout << moneyBag->getCred() << "
\n";
}
}
else {
display->showDisplay("
SELECT DIFFERENT ITEM");
}


Edited by - pdstatha on January 8, 2002 3:37:53 PM

Edited by - pdstatha on January 8, 2002 3:42:24 PM

Share this post


Link to post
Share on other sites
can you show your updateCred code of the class Bank? And anyway I''m quite sure about that part of your code, if it really works can somebody tell me why it does?

Share this post


Link to post
Share on other sites
hmm...
I think that the problem might be in interface3 when you dispense the item. I am assuming that the dispense() function takes care of subtracting the item from the tray. Which is good, but you are only calling it from the tmp tray that you made. Where are you updating the actual tray, which would be tList[ i ]?

I think this is your problem, but I might be way off. Let me know if it works.


DarkMonkey

    

else if(cred >= price) {
cred -= price;
tmp.dispense();
display->showDisplay("\nPRODUCT ORDERED:: ");
cout << tmp.getProduct().getName() << "\n";
display->showDisplay("\nQUANTITY REMAINING::");
cout << tmp.getQuant() << "\n";
moneyBag->updateCred(cred);
cred = moneyBag->getCred();
display->showDisplay("CREDIT::");
cout << moneyBag->getCred() << "\n";
}


Edited by - DarkMonkey on January 8, 2002 4:18:22 PM

Edited by - DarkMonkey on January 8, 2002 4:19:37 PM

Edited by - DarkMonkey on January 8, 2002 4:21:31 PM

Share this post


Link to post
Share on other sites
I noticed something...
About your 19 products. In c++ when you do
variable = variable2;
it copies the variable. In java it copies the reference.
Your tmp variable is storing a copy of your tray item, not your tray item. Change the variable to a pointer, and don''t inititialize it.
Change
Tray tmp (...
to
Tray* tmp;
and
tmp = tList;
to
tmp = &(tList[i]);
also change your tmp.method to tmp->method

Share this post


Link to post
Share on other sites
Thanks heway that did work, i''ve figured something out about the credit part of the program. The credit isn''t updating once a product has been ordered because it reckons that every products price is zero. I think this might have something to do with the default constructor i''ve written, but i''m not sure, neway here''s the Product class.

  
class Product{
public:
// default constructor (an added benefit is that it can

// be initalized with a string literal)

Product(const char* itsName = "", int itsPrice = 0):
name(itsName), price(0)
{}
// normal constructor

Product(const string& itsName, int itsPrice):
name(itsName), price(itsPrice) {}
// if your destructor doesn''t need to do anything,

// you don''t need to implement one

// since these functions don''t modify the object (and shouldn''t), mark them as const

const string& getName() const {return name;}
int getPrice() const {return price;}
private:
string name;
int price;
};

Share this post


Link to post
Share on other sites
DOH!!!!! Silly me no wonder it didn''t work, heres the original which initialises the product price to zero

  
Product(const char* itsName = "", int itsPrice = 0):
name(itsName), price(0)
{}


And here''s the one that works properly

  
Product(const char* itsName = "", int itsPrice = 0):
name(itsName), price(itsPrice)
{}


Finally complete, thanks to everyone who has helped me convert this program to C++. Now for something a bit more ambitious making this into a GUI interface, any tips???

Share this post


Link to post
Share on other sites