fork(3) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.  
  6. // (1) define items:
  7. // 1. Coke ($35)
  8. // 2. Milk tea ($50)
  9. // 3. Black tea ($25)
  10. // (2) User input:
  11. // 1. make a choice: 1 ~ 3
  12. // 2. insert coins
  13. // (3) Conversion:
  14. // 1. choice -> price
  15. // 2. check if the inserted coins are enough
  16. // 3. if so, give him/her the bottle and return changes.
  17. // 4. otherwise (else), display "Not enough"
  18.  
  19. int choice, coins, price;
  20. cin >> choice >> coins;
  21.  
  22. switch (choice) {
  23. case 1:
  24. price = 35;
  25. break;
  26. case 2:
  27. price = 50;
  28. break;
  29. case 3:
  30. price = 25;
  31. break;
  32. default:
  33. cout << "沒有這個選項。" << endl;
  34. return -1; // abnormal exit
  35. }
  36.  
  37. int changes = coins - price;
  38. if (changes >= 0) {
  39. cout << "取回飲料並找零 " << changes << " 元。" << endl;
  40. } else {
  41. cout << "金額不足,請重新來過。" << endl;
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 5292KB
stdin
2 80
stdout
取回飲料並找零 30 元。