From 52c071c46c311cdee47f78e5b6787c7ae52ecb39 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Sat, 20 Jul 2024 14:58:27 +0300 Subject: [PATCH] Fix Lesson 2 Task 1 --- Lesson_2/Task_1/main.cpp | 47 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/Lesson_2/Task_1/main.cpp b/Lesson_2/Task_1/main.cpp index 553edde..d6e6bd3 100644 --- a/Lesson_2/Task_1/main.cpp +++ b/Lesson_2/Task_1/main.cpp @@ -15,32 +15,41 @@ int main() cout << "Enter action (1 - add money, 2 - remove money, 3 - display all balances): "; cin >> action; - if (action == 1 || action == 2) { + if (action < 1 || action > 3) { + cout << "Invalid action" << endl; + continue; + } + + if (action != 3) { int number = 0; int money = 0; cout << "Enter account number: "; cin >> number; - if (number >= 0 && number < 10) { - cout << "Enter the amount of money: "; - cin >> money; - if (money > 0) { - if (action == 1) { - accounts[number] += money; - cout << "Money added." << endl; - } else if (accounts[number] >= money) { - accounts[number] -= money; - cout << "Money removed." << endl; - } else - cout << "Not enough money." << endl; - } else - cout << "The amount must be positive." << endl; - } else + + if (number < 0 || number > 10) { cout << "Invalid account number." << endl; - } else if (action == 3) { + continue; + } + + cout << "Enter the amount of money: "; + cin >> money; + + if (money <= 0) { + cout << "The amount must be positive." << endl; + continue; + } + + if (action == 1) { + accounts[number] += money; + cout << "Money added." << endl; + } else if (accounts[number] >= money) { + accounts[number] -= money; + cout << "Money removed." << endl; + } else + cout << "Not enough money." << endl; + } else for (int account = 0; account < 10; ++account) cout << "Account " << account << ": " << accounts[account] << endl; - } else - cout << "Invalid action." << endl; }