Automatic vending machine (Id-1143)

Just for Practice 3 Additional [8-Jan-2018 to 31-Mar-2018]

Program ID- 1143

An automatic vending machine has many snack items arranged in its shelves. Each item has an item code and a cost. A user can enter the amount and key-in the item code. If the itemcode matches an entry in the item list and amount entered by user is less than cost of the item, then the item will be dispensed. Develop an algorithm and write a C++ code to print name of the item if the amount entered is greater than the cost of the item and item code is valid. If the amount is less than the cost of the item then throw a double exception, if the item code entered is wrong then throw a integer exception and when the item entered is not in stock throw a string exception. Print appropriate messages such as “Insufficient amount”, “Wrong item code” or “Less stock”.

Input Format:

Enter the number of items in the vending machine


Cost of item1

stock in hand item1


Cost of item2

stock in hand item2


Cost of item1

stock in hand item3

Item code entered by user

Cost entered by user

Output Format:

Print either item name or “Insufficient amount” or “Wrong item code” or “Less Stock”

Boundary Conditions:

Number of Item  >1


Please Comment Working if the code worked to you

If you have other working codes please comment the codes enclosing with <pre> and </pre> 🙂

Example: <pre> Your Code </pre>

C++ Code [By Chir Patel]

Follow for important news/announcements in VIT
The Above Codes are for reference only. It doesn't mean everyone to directly copy/paste those codes.


Click Here to Leave a Comment Below 4 comments
bsvvvvdvdfv - March 14, 2018

neha - March 26, 2018

private test case failed


Abhinav Mohata - March 28, 2018

Its Working!!
Thank you so much!

Dark - April 7, 2020

Question says to use exceptions so we need to use ‘try’ ,’throw’ and ‘catch’ statements.


Leave a Reply:

This site uses cookies and by using the site you are consenting to this.