i was just wondering if anyone can give me an example of how to use a menu and parallel array together for a vending machine c++ code. Also when do you need to initialise all these things? before the main() or inside the main()?? pleaase helppp! thanks youu..

Welcome. What have you tried so far?

before the main() or inside the main()??

Normally variables are declared within main and passed into methods. Variables local to a particular function are declared within the body of that function.

#include<iostream>
void myfunc(int passedin)
{
        int madeinfunc = 0;
        madeinfunc = passedin+10;

        std::cout <<"The sum is "<< madeinfunc<<std::endl;
}

int main()
{
        int madeinmain = 6;
        myfunc(madeinmain);
       return 0;
}

So both variables are declared and initialized in their respective functions.

Edited 6 Years Ago by jonsca: n/a

ok thanks well ive been working on my code but it is absolutley terrible and its my first go at writing in c++. Its an assignment so ill give you the question and ill show you my code at the moment.

Question:
Your program should operate continuously with a list of drinks and their costs displayed on screen. there should also be a "shutdown" option with a password for a service person to come and re-stock the machine.
Once a user has selected their drink the system should allow them to enter the amount of money they are inserting into the machine. The program should then calculate the amount of change to be returned and subtract one from the number of the drink in the machine. If the user selects a drink which has sold out an appropriate message should be displayed. Some input validation should be carried out on the amount eg. no negative values and no amount larger than $10.
When the "Shutdown" option is chosen the system should report total turnover and number of each drink left in the machine. After a suitable input from the service person the system should then reset the number of drinks back to the initial values and resume normal operation.

Arrays must be used and an expected looping menu-structure.

the drinks are:
coca cola...$1.50....30 (in machine)
sprite........$1.60....30
fanta.........$1.70....30
ginger beer...$1.90.....30
powerade.....$3.50.....20


This is what ive done it is deffinatley wrong bcoz im not sure how to do it properly but could you please help me out? or maybe check mine and give me some pointers?

#include <iostream>
using namespace std;


int main ()
{
	const int SIZE = 6;    //Array size
	char DrinkChoice[SIZE] = { "Coca Cola", "Sprite", "Fanta", "Ginger Beer", "Powerade", "Shutdown"};  //Drink Names
	int NumberDrinks[SIZE] = { "30", "30", "30", "30", "20" };  //Number of drinks..other array value are 0. 
	double DrinksCost[SIZE] = { "1.50", "1.60", "1.70", "1.90", "3.50" };  //Cost of drink..other array value is 0.
	TotalMoney = 0.0;		//money count
	int password = 123

do  {
	//show user the drink machine menu
	cout << "Select choice of Drink" << endl; 
	cout << --"Drink Machine Menu"-- << endl;
	cout << --"1. Coca Cola"-- << endl;
	cout << --"2. Sprite"-- << endl;
	cout << --"3. Fanta"-- << endl;
	cout << --"4. Ginger Beer"-- << endl;
	cout << --"5. Powerade"-- << endl;
	cout << --"6. Shutdown"-- << endl;


	//get user response
	cin >> UserChoice
	cout << DrinksCost
	cin >> UserPayment
	cout << UserChange
		(UserChange = UserPayment - DrinksCost)
		( TotalMoney++; )
		if (UserPayment <0, >10)
		   cout << "This is an error payment.\n"
		   cout >> "Please enter payment correctly.\n"
		( NumberDrinks - 1 )
		if (NumberDrinks = 0)
		cout << "This drink is sold out.\n"
		cout << "Please pick another.\n"
	

} until "shutdown"
	
	cout << "password:"
	cin >> UserPassword
		if (UserPassword <3, >3)
		cout << "access denied.\n"
	cout << TurnoverReport
		(TurnoverReport = TotalMoney)
	cout << NumberDrinks
	cin >> "ResetMachine"
	
//show user the drink machine menu
	cout << "Select choice of Drink" << endl; 
	cout << --"Drink Machine Menu"-- << endl;
	cout << --"1. Coca Cola"-- << endl;
	cout << --"2. Sprite"-- << endl;
	cout << --"3. Fanta"-- << endl;
	cout << --"4. Ginger Beer"-- << endl;
	cout << --"5. Powerade"-- << endl;
	cout << --"6. Shutdown"-- << endl;


system [pause];
return 0;

}

ok thanks well ive been working on my code but it is absolutley terrible and its my first go at writing in c++. Its an assignment so ill give you the question and ill show you my code at the moment.

It's not bad. I'm sure you've used other languages so some of the syntax is colliding in your head.

This is what ive done it is deffinatley wrong bcoz im not sure how to do it properly but could you please help me out? or maybe check mine and give me some pointers?

I'll go through some of the errors in the code

#include <iostream>
using namespace std;


int main ()
{
	const int SIZE = 6;    //Array size
	
        //Rather than accounting for the menu choices why not make 
        //all of the arrays 5 and not have to deal with the empty slot in each
    
char DrinkChoice[SIZE] = { "Coca Cola", "Sprite", "Fanta", "Ginger Beer", "Powerade", "Shutdown"};  //Drink Names
	
         //You need to make this a string array.  You could make it a  
         //2D char array but strings are easier -- as it is you will only  
         //get 1 character out of each if it works at all

      int NumberDrinks[SIZE] = { "30", "30", "30", "30", "20" };  //Number of drinks..other array value are 0. 
	
           //strings belong in quotation marks, ints and doubles don't

double DrinksCost[SIZE] = { "1.50", "1.60", "1.70", "1.90", "3.50" };  //Cost of drink..other array value is 0.
	TotalMoney = 0.0;		//money count
	int password = 123
    //totalmoney needs to be declared
            
//You're right to put this in a do loop but do while are paired, do until is VB I think

do  {     
	//show user the drink machine menu
	cout << "Select choice of Drink" << endl; 
	cout << --"Drink Machine Menu"-- << endl;
	cout << --"1. Coca Cola"-- << endl;
	cout << --"2. Sprite"-- << endl;
	cout << --"3. Fanta"-- << endl;
	cout << --"4. Ginger Beer"-- << endl;
	cout << --"5. Powerade"-- << endl;
	cout << --"6. Shutdown"-- << endl;


	//get user response
	cin >> UserChoice    //need a separate variable to hold this
	cout << DrinksCost   //need to specify which element in the
                                          //array, get that from (userchoice-1) 
	cin >> UserPayment  //where is this declared?
	cout << UserChange
		(UserChange = UserPayment - DrinksCost)
		( TotalMoney++; )     //not adding 1 to it, add the cost
		if (UserPayment <0, >10)   //nonsense code
                                     // (userpayment < 0 || userpayment > 10)
		     //else statement needed for below
                   cout << "This is an error payment.\n"
		   cout >> "Please enter payment correctly.\n"
		( NumberDrinks - 1 )       //no magic syntax like this
		if (NumberDrinks = 0)
		cout << "This drink is sold out.\n"
		cout << "Please pick another.\n"
	

} until "shutdown"        //change this to while the menu choice not
                                         //equal to shutdown
	
	cout << "password:"
	cin >> UserPassword
		if (UserPassword <3, >3)   //another nonsense statement
		cout << "access denied.\n"
	cout << TurnoverReport
		(TurnoverReport = TotalMoney)
	cout << NumberDrinks
	cin >> "ResetMachine"
	
//show user the drink machine menu 
 
//rather than this make an outer loop that includes the option to quit from shutdown
	cout << "Select choice of Drink" << endl; 
	cout << --"Drink Machine Menu"-- << endl;
	cout << --"1. Coca Cola"-- << endl;
	cout << --"2. Sprite"-- << endl;
	cout << --"3. Fanta"-- << endl;
	cout << --"4. Ginger Beer"-- << endl;
	cout << --"5. Powerade"-- << endl;
	cout << --"6. Shutdown"-- << endl;


system [pause];     //wrong syntax but a bad habit anyway
                              //use cin.get() to have the user hit a key
return 0;

}

All right, that should give you some stuff to fix.

Edited 6 Years Ago by jonsca: n/a

thanks very much for your help =] i feel better about my code and thank you for pointing out the things i need to fix. apprecaited =]

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