写一个自动售货机界面的代码
时间: 2023-05-18 16:07:27 浏览: 116
非常感谢您的提问!以下是一个简单的自动售货机界面的代码示例:
```
#include <iostream>
using namespace std;
int main() {
int choice;
float money, price;
bool valid_choice = false;
cout << "Welcome to the vending machine!" << endl;
cout << "Please select an item:" << endl;
cout << "1. Coke - $1.50" << endl;
cout << "2. Chips - $1.00" << endl;
cout << "3. Candy - $0.75" << endl;
do {
cout << "Enter your choice (1-3): ";
cin >> choice;
switch (choice) {
case 1:
price = 1.5;
valid_choice = true;
break;
case 2:
price = 1.0;
valid_choice = true;
break;
case 3:
price = 0.75;
valid_choice = true;
break;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
} while (!valid_choice);
cout << "You have selected item " << choice << "." << endl;
cout << "Please insert money: $";
cin >> money;
if (money >= price) {
float change = money - price;
cout << "Thank you for your purchase!" << endl;
cout << "Your change is: $" << change << endl;
} else {
cout << "Insufficient funds. Please insert more money." << endl;
}
return 0;
}
```
这个程序会显示一个自动售货机的菜单,让用户选择一个商品。然后,它会要求用户输入足够的钱来购买所选商品。如果用户输入的钱足够,程序会输出一条感谢购买的消息,并显示找零的金额。如果用户输入的钱不够,程序会提示用户继续输入更多的钱。
阅读全文