Last active
September 10, 2024 16:27
-
-
Save Red0214/7028acd4d98362a64a5a5a3d7d7a8633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string_view> | |
| #include <string> | |
| #include <cstdlib> | |
| #include <limits> | |
| // Clear the console. | |
| void clearScreen() | |
| { | |
| system("cls"); | |
| } | |
| // Gives time to user to read the console. | |
| void pauseScreen() | |
| { | |
| std::cout << "Press Enter to continue..."; | |
| std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); | |
| std::cin.get(); | |
| } | |
| // Get value user from console. | |
| double getValue(std::string_view str) | |
| { | |
| std::cout << str; | |
| double x{}; | |
| std::cin >> x; | |
| return x; | |
| } | |
| // Calculates discount based on a maximum and minimum. | |
| double calculateDiscount(double& x, const double& discount) | |
| { | |
| x -= (x * discount / 100); | |
| return x; | |
| } | |
| // Validates that the user input is a valid value. | |
| double validatePrice(double& x, const double& max, const double& min) | |
| { | |
| // If the input isn't a valid value, prompt the user to enter it again. | |
| while (x < min || x >= max) | |
| { | |
| clearScreen(); | |
| x = { getValue("Invalid price!, insert a valid price.\n\nEnter the total purchase: ") }; | |
| } | |
| return x; | |
| } | |
| int main() | |
| { | |
| char option{}; | |
| do | |
| { | |
| std::cout << "--- MAIN MENU ---\n\n"; | |
| std::cout << "1. Purchase less than $100.\n"; | |
| std::cout << "2. Purchase between $100 and $299.\n"; | |
| std::cout << "3. Purchase greater than or equal $300.\n"; | |
| std::cout << "4. Exit\n\n"; | |
| std::cout << "Insert an option: "; | |
| std::cin >> option; | |
| clearScreen(); | |
| switch (option) | |
| { | |
| case '1': | |
| { | |
| double val{ getValue("Enter the total purchase: ") }; | |
| val = { validatePrice(val, 100, 1) }; | |
| std::cout << "Your purchase is less than $100, there aren't discount...\n\n"; | |
| pauseScreen(); | |
| clearScreen(); | |
| option = 'x'; | |
| break; | |
| } | |
| case '2': | |
| { | |
| double val{ getValue("Enter the total purchase: ") }; | |
| val = { validatePrice(val, 300, 100) }; | |
| std::cout << "Your discount is 10%. Total price is: " << calculateDiscount(val, 10) << "\n\n"; | |
| pauseScreen(); | |
| clearScreen(); | |
| option = 'x'; | |
| break; | |
| } | |
| case '3': | |
| { | |
| double val{ getValue("Enter the total purchase: ") }; | |
| val = { validatePrice(val, std::numeric_limits<double>::max(), 300) }; | |
| std::cout << "Your discount is 20%. Total price is: " << calculateDiscount(val, 20) << "\n\n"; | |
| pauseScreen(); | |
| clearScreen(); | |
| option = 'x'; | |
| break; | |
| } | |
| case '4': | |
| { | |
| clearScreen(); | |
| std::cout << "See you later!\n"; | |
| break; | |
| } | |
| default: | |
| std::cout << "Insert a valid value!\n\n"; | |
| pauseScreen(); | |
| clearScreen(); | |
| option = 'x'; | |
| break; | |
| } | |
| } while (option == 'x'); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment