Vending machine-Vending Machine

Source: Internet
Author: User
Tags money back

Vending machine-vending machine: enter money every time until the money is enough, and then return the excess money.

// Vending machine-vending machine # include <iostream> const double apply_money = 3.5; double total_money; double accept_money (); double compute_change (double total_money); int main () {using namespace STD; double money_back; total_money = accept_money (); money_back = compute_change (total_money); cout <"enjoy you drink, and money back" <money_back <Endl; return 0;} double accept_money () {using namespace STD; char ans; Total_money = 0; do {cout <"Enter the money :( D-dollar (1), Q-quarter (0.25), D-Dime (0.10 ), n-Nickel (0.05) \ n "; cout <" The apply money is 3.5, the inserted money is "<total_money <Endl; CIN> ans; switch (ANS) {Case 'D': total_money + = 1; break; Case 'q': total_money + = 0.25; break; Case 'D': total_money + = 0.10; break; Case 'N': total_money + = 0.05; break; default: cout <"error input! "<Endl ;}}while (total_money <apply_money); Return total_money;} double compute_change (double total_money) {return (total_money-apply_money );}

Result:

Enter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 0DEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1dEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1.1qEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1.35qEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 1.6DEnter the money:(D-dollar(1),q-quarter(0.25),d-dime(0.10),n-nickel(0.05))The apply money is 3.5,the inserted money is 2.6DEnjoy you drink,and money back 0.1


Vending machine-Vending Machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.