Building a vending machine using funcs

Aug 13, 2013 at 5:39am
Can any1 help me with my program of creating a vending machine.....
it is quarterly done and im new to this so I really need help...
we asked to work on the comments given
here's how the program looks like.

//need to decide which functions will be passed by ref,
//and which will be passed by value and i'm not allowed to use global variables.

#include <iostream>
#include <math.h>
#include <string>

using namespace std;
//func to display selection
void ShowMenu(){
cout <<"\n\tThe following selection is on offer:"
<<"\n\t1. Water - $0.75\n\t2. Coke - $1.20 "
<<"\n\t3. Diet Coke - $1.20\n\t4. Iced Tea - $1.00 "
<<"\n\t5. Swiss chocolate - $1.50\n\t6. Candy - $0.95"
<<"\n\t7. Chips - $1.10\n\t8. Bubble Gum - $0.50"
<<"\n\t9. Turkish delight - $1.20\n\t10. End of transaction."
<<endl<<endl;
}

void MakeSelection(){
int num;
cout <<"\n\tChoose your selection <1-10>: ";
cin >> num;
if(num < 1 || num > 10){
cout<<"\n\tEnter a valid selection <1 - 10>: ";
cin >> num;
}
cout <<endl<<endl;
}

int ReturnChange(int change,int Coins[],int NumCoins[]){

int i=0;
while (change !=0){


}


}

void DisplayErrorMessage(){


}

void PrintConfidentialInformation(int Denominations, int Items, int Coins[], int NumCoins[], int ItemPrice[] , int NumItems[])
{
//just to check if machine is working well
cout<<" coins"<<endl;
for(int i = 0; i < Denominations; i++){
cout<< Coins[i]<<" ";
}
cout<<endl;

for(int i = 0; i < Denominations; i++){
cout<< NumCoins[i]<<" ";
}
cout<<endl;

cout<<" Items"<<endl;

for(int i = 0; i < Items; i++){
cout<< ItemPrice[i]<<" ";
}
cout<<endl;

for(int i = 0; i < Items; i++){
cout<< NumItems[i]<<" ";
}
cout<<endl;

}

int main ()
{
//declare nd initialise the items and coin stacks

int Denominations = 5;
int Coins[] = {100, 50, 20, 10, 5};
int NumCoins[] = {10, 10, 10, 10, 10}; //assume we have 10 coins of each denomination
const int Items = 9;
int ItemPrice[ ] = { 75, 120, 120, 100, 150, 95, 110, 50, 120 }; //price in cents
int NumItems[ ] = { 10, 10, 10, 10, 10, 10, 10, 10, 10 };

ShowMenu();

//ask for input of coins -- you can ask the user to enter something like 2.75, and then you should assume that it has 2 1 dollar coins, 1 50 cents, 1 20 cents and 1 5 cents. Need to write a while loop and use % and / operators to find how many of those coins are present. Later you would need to increment the coins in NumCoins[]. You should also give appropriate error messages if the input is illegal.

//make selection() -- you will ask user for input, like if the user wants 2 x water and 4 x candy, then the user can enter "224444". call this function and pass this encoded information eg. "224444" (either string or int), then decode it- meaning that you will need to separate the values - calculate the total cost from ItemPrice[ ] and update your NumItems[ ].

//do the rest



//repeat

ReturnChange(155,Coins,NumCoins); //just checking the items and coins:_ Not needed in Asg, but I wanted to show you how to pass Arrays as Parameter to functions. Note that in C++, Arrays are passed by REFERENCE by default.

PrintConfidentialInformation(Denominations,Items, Coins, NumCoins, ItemPrice, NumItems); //see the style, how the arrays are passed as parameters...

cout <<endl <<endl;
system ("pause");
return 0;

}
Last edited on Aug 13, 2013 at 5:40am
Aug 13, 2013 at 6:13am
looks like u are dng well, you code it, have specific problems, we help u :).

Ps:please use code tags for code, also please make it clear where u need help, dont post problem statement and expect people to code it.
Aug 14, 2013 at 10:23pm
ok.....thanx 4 the tips..... heres my problem .... in my void MakeSelection(), we were supposed to decode some info i.e (users input of num of Items he would like to buy). meaning if user input '224444' then we have to decode it as 2 water needed each cost $0.75 nd 4 Iced tea needed costing $1.00 each.

how do we decode it (do we use loops and what do we have as its conditions)

hope u get what im trying to say.....

thanx !!!
Last edited on Aug 14, 2013 at 10:24pm
Aug 14, 2013 at 10:42pm
You can either convert the int to a string, or you can do something like
1
2
3
4
5
6
7
8
9
10
11
12
int selection;
cin >> selection;
int cost=0;
if (selection!=10)
{
    while (selection!=0)
    {
        NumItems[(selection-1)%10]--;
        cost+=ItemPrice[(selection-1)%10];
        selection/=10;
    }
}


That's assuming I understood the input right (isn't your 2nd option coke?)
Last edited on Aug 14, 2013 at 10:44pm
Topic archived. No new replies allowed.