Vending Machine

Alrite, Im trying to code a vending machine...

#include <iostream>
#include <stdlib.h>
#include <cstring>

using namespace std;

int main(){



return 0;
}

void ShowMenu(){

cout<<"1. Cheese $1.50\n";
cout<<"2. Butter $2.00\n";
cout<<"3. Bread $0.75\n";
cout<<"4. Jam $3.50\n";
cout<<"5. End of transaction\n";
cout<<endl;
}


void Choice(){

ShowMenu();

int Input;
int Initial;

cout<<"Please enter your initial amount in cents:";
cin>> Initial;
cout<<"Enter your choice:";
cin>>Input;



while ( !(Input >= 1 && Input <= 5) )

{
cout<<Input<< " is the Input you entered"<<endl;
cout<< "please enter again. Your input should be between 1 - 5 "<<endl;

cin>>Input;

}


}

if the user chooses more than one item eg:2 Bread and 2 Jams which will be 3355, how do I get each number and calculate the cost???
store the quantity for selected type in a variable, multiply the cost for selected type with quantity, and store it separately , repeat the same for all selected items.By the way how are you planning to display your menu, i dont see any function calls in your main function :) , if u place some function calls in main, make sure u either declare or define ur functions before main() :)
alrite, the functions and planning are already taken care of, could be please rephrase your response, I mean, is there another way of getting one value at a time and manipulating it, for example for the above problem 3 3 5 5, how do I get each number and reference it to the menu list...thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define cheese_cost 1.50
//define similar constants for others
int ShowMenu(){

int ch;
int icount_Cheese  =0;
int icount_Butter    =0;
int icount_Bread     =0;
int icount_Jam        =0;
int temp                 =0;
float total_cost       =0;
while(1)
{
 cout<<"1. Cheese $1.50\n";
 cout<<"2. Butter $2.00\n";
 cout<<"3. Bread $0.75\n";
 cout<<"4. Jam $3.50\n";
 cout<<"5. End of transaction\n";
 cout<<endl;
 cin>>ch;

 switch(ch)
{
   case 1: cout<<"enter the quantity for cheese";
               cin>>temp;
               icount_Cheese=temp+icount_Cheese;
               break;
  //similarly for other cases
    case 5:total_cost=(icount_Cheese*cheese_cost)+//similarly for others;
                 return total_cost;

  }
 //end of switch
}
//end of for
return 0;
}
//end of menu 

or you can calculate the total cost in a separate function to make it proper, just store the counts in an array or list , return the same.
Thanks that really helped alot,
Okay, what if instead of asking the user to enter the quantity for cheese, the program was changed

void ShowMenu(){

cout<<"1. Cheese $1.50\n";
cout<<"2. Butter $2.00\n";
cout<<"3. Bread $0.75\n";
cout<<"4. Jam $3.50\n";
cout<<"5. End of transaction\n";
cout<<endl;
}

the numbers 1,2,3,4 and 5 represents the different options, what if the user wanted Cheese and Bread and pressed 1 3 as their choice which represent Cheese and Bread, how would you calculate for bread and cheese....is it done separately or together...thanks
Oh and how would this problem be solved using arrays...???
Last edited on
How would u find out the quantity?? assuming u are not interested in quantity. u can either calculate independently or togather.
the code i gave u considers quantity as well, and calculates the whole cost
at once.
Okay, if the user enters 2233 that means that the user wants 2 Jams and 2 Bread, how would I calculate this...meaning how would I get the program to get the cost for jam(3.50 + 3.50) and the cost for bread(0.75 + 0.75). The value the user enters also acts as the quantity for the items. In the case as mentioned above the user wants 2 jams and 2 bread..makes sense??......thanks
Food for thoughts you can improve your code such that if the user input an amount of money that's greater than the initial amount he should be able to get some change. For instance if the cheese costs $5 and the user input $6.25 you code should be able to display the change($1.25) that has to be returned.
If the user enters multiple quantity then you should think about storing them into an array, if he wants 2233 then create an array to store them.

1
2
3
4
5
6
float array[4] = {1.50,2.00,0.75,3.5} 
cout <<"Enter your choice;  
for(int i = 0; i < 4; i++)
     cin   >> array[i];

int total_cost = array[0] + array[1]; 



Last edited on
Thanks...
but how do you actually get each value entered by the user and calculate the cost...I mean for 2233 how do I generate a code to go from 2 then to 2 then 3 and finally to 3, getting the relevant information and calculating the total.
Last edited on
Topic archived. No new replies allowed.