C: Calculate Order Price Based On User's Input.
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_3/exercise2_3.c
// Exercise 2-3 // Daniele Grech Pereira 22 November 2022 // You’re selling a product that’s available in two versions: type 1 is a // standard version priced at $3.50, and type 2 is a deluxe version priced at $5.50. // Write a program using only what you’ve learned up to now that prompts for the user to // enter the product type and a quantity, and then calculates and outputs the price for // the quantity entered. #include <stdio.h> #include <ctype.h> // toupper #include <limits.h> // INT_MAX // FIXED PRODUCT PRICES: #define TYPE1 3.5f // Type1 price #define TYPE2 5.5f // Type2 price // A function that returns the calculated the price. double calculatePrice(int input, int quantity); // A function that validates a user's input. int checkInput(int input, int min, int max); // A function that returns the user's validated input. int getInput(char* msg, int min, int max); int main(int argc, char* argv[]) { // Set and initialize vars for main program: int type = 0, quantity = 0; // Vars for storing user input. double price = 0; // Stores calculated results. char repeat = 'Y'; // Loop var for main program to repeat. // Main program loop: while (toupper(repeat) == 'Y') { // Get type from user, can be either 1 or 2: type = getInput("Select product type (1 or 2): ", 1, 2); // Get quantity from user, can be from 1 to largest possible INT value: quantity = getInput("Select quantity: ", 1, INT_MAX); // Display user's selection before calculating: printf("\nProduct type %d selected @ %d units.", type, quantity); // Calculate price based on user's input: price = calculatePrice(type, quantity); // Display final results: printf("\n\nRESULTS:\nProduct type: %d, quantity: %d, total: $%.2f\n", type, quantity, price); // Ask if user wants to repeat the main program: printf("\nAgain? (Y to repeat/any other key to quit) "); scanf(" %c", &repeat); }; return 0; } int getInput(char* msg, int min, int max) { int var = 0; printf("%s", msg); while(var == 0) { scanf(" %d", &var); var = checkInput(var, min, max); }; // Loops until input is valid and not == 0 return var; } double calculatePrice(int input, int quantity) { double unit_price = 0; if (input == 1) unit_price = TYPE1; if (input == 2) unit_price = TYPE2; return unit_price * (double)quantity; } int checkInput(int input, int min, int max) { int result = 0; // Return 0 if input is invalid. if (input < min || input > max) // out of bounds, invalid! printf("\nEnter a number between %d or %d!\n", min, max); if (input >= min && input <= max) // inbounds, validate! result = input; return result; }
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/exercises/2_3/exercise2_3.txt
==662== Memcheck, a memory error detector ==662== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==662== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==662== Command: ./exercise2_3 ==662== Select product type (1 or 2): 0 Enter a number between 1 or 2! 3 Enter a number between 1 or 2! 1 Select quantity: -1 Enter a number between 1 or 2147483647! 0 Enter a number between 1 or 2147483647! 2147483648 Enter a number between 1 or 2147483647! 1000 Product type 1 selected @ 1000 units. RESULTS: Product type: 1, quantity: 1000, total: $3500.00 ==662== ==662== HEAP SUMMARY: ==662== in use at exit: 0 bytes in 0 blocks ==662== total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated ==662== ==662== All heap blocks were freed -- no leaks are possible ==662== ==662== For lists of detected and suppressed errors, rerun with: -s ==662== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)